Linking to embedded files in PDF

Hi,
I’m able to embed files into the PDF successfully (confirmed this is working in Acrobat), but I’m struggling to find a way to link to these embedded files. Ideally the user would be able to click a hyperlink in the PDF to open the file.

I’m embedding the files with:

var spec = document.EmbeddedFiles.Add(filepath).Value;
spec.Description = "filename.txt";

When I try to add any sort of link it tries to direct to a file on my system rather than in the PDF.

var link = page.Annotations.AddLink(x, y, w, ht);
link.Actions.AddOpenFile("fileName.txt");

I also tried with page.Annotations.AddFileAttachment but ran into the same issue.

The demos I’ve seen seem to embed or open a file, but not both. Is it possible? What am I missing?

Thanks

Hi,

It seems that Adobe Reader doesn’t support link annotations that link to an embedded file.

An alternative is to use the file attachment annotation with some additional modifications so that it simulates the link annotation, like in the following code snippet:

var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

using (var document = new PdfDocument())
{
    var filename = "filename.txt";

    var page = document.Pages.Add();

    double x = 100, y = page.CropBox.Top - 100, width, height;
    using (var formattedText = new PdfFormattedText())
    {
        formattedText.FontSize = 24;
        formattedText.Color = PdfColors.Blue;
        formattedText.Underline = new PdfTextDecoration();
        formattedText.Append(filename);

        width = formattedText.Width;
        height = formattedText.Height;
        y -= height;

        page.Content.DrawText(formattedText, new PdfPoint(x, y));
    }

    var fileAttachment = page.Annotations.AddFileAttachment(x, y, Path.Combine(desktop, filename));

    // Update the annotation bounds so that it encompasses the wanted text.
    // Changing the annotation bounds will invalidate the appearance so
    // use BeginInit - EndInit so that the appearance is updated on EndInit and not when saving the document.
    fileAttachment.Appearance.BeginInit();
    fileAttachment.SetBounds(width, height);
    fileAttachment.Appearance.EndInit();

    // Use low-level API to make further modifications.
    // For specification see:
    // https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=391
    // https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=393
    // https://opensource.adobe.com/dc-acrobat-sdk-docs/standards/pdfstandards/pdf/PDF32000_2008.pdf#page=414
    var fileAttachmentDictionary = fileAttachment.GetDictionary();
    // Remove the annotation flags. File attachment annotation by default has Print | NoZoom | NoRotate flags.
    fileAttachmentDictionary.Remove(PdfName.Create("F"));
    // Set the file attachment icon to some unsupported value so that the Adobe Reader doesn't automatically update the annotation appearance.
    fileAttachmentDictionary[PdfName.Create("Name")] = PdfName.Create("None");

    // Set the annotation appearance to an empty form so that the Adobe Reader doesn't automatically create the annotation appearance.
    fileAttachment.Appearance.Set(new PdfForm(document, new PdfSize(width, height)));

    document.Save(Path.Combine(desktop, "LinkToEmbedded.pdf"));
}

I hope this helps!

Regards,
Stipo

Thanks for the response Stipo. Just tested it, and it works very well as a workaround!