I have a PDF in which there are few file attachments. For each attachment (embedded file) in PDF multiple Hyperlink needs to be created. How can I do it?
If I use FileAttachmentAnnotation, it creates multiple file copy in attachment (for each link creation it add one copy in embedded file list).
A similar case has already been logged in our issue management system for adding “GoToEmbeddedAction” property in Hyperlinks. Nevertheless, please share a sample PDF along with sample code snippet that you are already using. We will test the scenario at our end and log another ticket for your case.
This code adds one embedded file to PDF each it gets executed. IT increases the file size. We need only one embedded file and multiple hyperlink/ annotations for the same file. Below is the code:
Aspose.Pdf.Annotations.FileAttachmentAnnotation fileAttachment = new Aspose.Pdf.Annotations.FileAttachmentAnnotation(pdfDocument.Pages[textFragment.Page.Number], textFragment.Rectangle, pdfDocument.EmbeddedFiles[pdfDocument.EmbeddedFiles.Count]);
fileAttachment.Icon = FileIcon.Paperclip;
// add FileAttachment Annotation to Annotations collection of first page
fileAttachment.Opacity = 0;
//fileAttachment.File.IncludeContents = false;
pdfDocument.Pages[textFragment.Page.Number].Annotations.Add(fileAttachment);
We also noticed that multiple attachments were being added in the PDF document using the code below to add FileAttachmentAnnotation. We have create a new issue as PDFNET-50083 in our issue tracking system so that we can rectify this issue by adding an enhancement to the API. We will let you know once the ticket is resolved. We apologize for the inconvenience.
Document doc = new Document(dataDir + "sample.pdf");
var page = doc.Pages[1];
FileSpecification fs = new FileSpecification(dataDir + "input.pdf");
doc.EmbeddedFiles.Add(fs);
var absorber = new TextFragmentAbsorber();
page.Accept(absorber);
foreach(var fragment in absorber.TextFragments)
{
FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(page, fragment.Rectangle, doc.EmbeddedFiles[doc.EmbeddedFiles.Count]);
fileAttachment.Icon = FileIcon.Paperclip;
// add FileAttachment Annotation to Annotations collection of first page
fileAttachment.Opacity = 0;
fileAttachment.File.IncludeContents = false;
page.Annotations.Add(fileAttachment);
}
doc.Save(dataDir + "linktofile.pdf");
Furthermore, if you want to add link to external file instead of embedded file, you can do it using JavaScript:
Document doc = new Document(dataDir + "sample.pdf");
TextFragmentAbsorber absorber = new TextFragmentAbsorber("Simple");
doc.Pages[1].Accept(absorber);
Page page = doc.Pages[1];
// Add button
Field field = new ButtonField(page, absorber.TextFragments[1].Rectangle);
doc.Form.Add(field);
// Set ButtonField actions
string fieldName = field.PartialName;
string opendocscript = "app.openDoc(\"/D/TestDocument2.pdf\");";
field.Actions.OnPressMouseBtn = new JavascriptAction(opendocscript);
// Save document
doc.Save(dataDir + "OpenPDFFile.pdf");
Can I use JavaScript to click an hyperlink or annotation?
Can you please share a sample expected output PDF document for our reference? Also, please share which steps your performed in Adobe Reader to generate it. We will investigate further on how to achieve similar functionality using the API and share our feedback with you.