We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Link to a file that is already attached (embedded) to a pdf

Using Aspose, I have already attached multiple PDFs to my main PDF. What I am trying to do is to create a link in the main PDF to one of those attached PDFs.

Here’s the pseudo-code:


string[] PDFarry; //already pre-populated with a list of file paths to attach
foreach (string PDF in PDFarry)
{
Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber(curFileName);
pdfDocument.Pages.Accept(textFragmentAbsorber);
Aspose.Pdf.Text.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;
foreach (Aspose.Pdf.Text.TextFragment textFragment in textFragmentCollection)
{
foreach (Aspose.Pdf.Text.TextSegment textSegment in textFragment.Segments)
{
int curPage = textFragment.Page.Number;
Page curPageNum = pdfDocument.Pages[curPage];
Aspose.Pdf.Rectangle recTangle = textFragment.Rectangle;
string attPath = PDF;
editor.CreateFileAttachment(recTangle.ToRect(), “”, attPath, curPage, “Paperclip”);

}
}

This code runs through the array of paths, and for each path it searches for the “filename.pdf” part of the path within the main PDF (the filename is already in the main PDF) All of that works thus far. But, instead of the paperclip, I am trying to make a rectangle around the text and basically make an “invisible button” to go over each of the filenames in the main PDF.

So, I guess that’s the bottom line. I want to draw a rectangle around some text and then turn that rectangle into a link that will open an attached PDF.


There are some things missing in the code above, but it works on my end. I just threw it in there to show what I have tried.

Hi there,


Thanks for your inquiry. As per my understanding, your source document contains file names and you want to add a link annotation to these files with transparent rectangle. Please check hopefully it will serve the purpose. Please let me know if there is any difference in your requirements and my understanding.

Document document = new Document(myDir + “input.pdf”);
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(“Test”);
document.Pages[1].Accept(textFragmentAbsorber);
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

foreach (TextFragment textFragment in textFragmentCollection)
{
//create link
Page page = document.Pages[1];
LinkAnnotation link = new LinkAnnotation(page, new Aspose.Pdf.Rectangle((float)textFragment.Position.XIndent,
(float)textFragment.Position.YIndent,(float)textFragment.Position.XIndent+(float)textFragment.Rectangle.Width,
(float)textFragment.Position.YIndent+(float)textFragment.Rectangle.Height));
// set color accordingly
link.Color = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Transparent);
link.Action = new Aspose.Pdf.InteractiveFeatures.GoToURIAction(“C:/Temp/TestPdf1.pdf”);
page.Annotations.Add(link);
}
//save updated document
document.Save(myDir + “output.pdf”);


Please feel free to contact us for any further assistance.

Best Regards,

Thanks for the reply. That takes care of getting rid of the paperclip but, correct me if I’m wrong, the Page parameter of the LinkAnnotation takes an Aspose.PDF.Page variable, right? I would use the LinkAnnotation if I were trying to create a link from one part of a PDF to another part of the same PDF.

This is what I’m trying to do:
LinkAnnotation link = new LinkAnnotation(“alreadyattachedPDF.pdf”, Rect);

I would use CreateFileAttachment but the resulting link is an annotation that can be moved/deleted by the end user after the process is complete…I do not want this.

I could live with the paperclip if it meant that the link annotations were locked in place.

I have this functionality built into a LiveCycle form and I’m trying to recreate it using Aspose.

Hi Charlie,


Thanks for your feedback. you can use LinkAnnotation to create local link within document or PDF document link as suggested above using action property.

Moreover, you can set privileges on PDF document to protect your document, i.e. use AllowModifyAnnotations property of DocumentPrivileges object for the purpose. Please check following documentation link for details.


Please feel free to contact us for any further assistance.

Best Regards,

I don’t know if I’m making it clear:

link.Action = new Aspose.Pdf.InteractiveFeatures.GoToURIAction(“C:/Temp/TestPdf1.pdf”);
won’t open a file that is attached to my PDF already, will it? As I understand it, GoToURIAction only works OUTSIDE the current PDF.

Hi Charlie,


Please accept my apologies for the confusion. You are right GoToURIAction only reference to files outside the PDF document. I’m afraid currently Aspose.Pdf doesn’t provide the feature to link an existing attachment within PDF document . However, I’ve logged a feature request as PDFNEWNET-35794 in our issue tracking system for the purpose. We will notify you via this forum thread as soon as its resolved.

We are sorry for the inconvenience faced.

Best Regards,

Hi Charlie,


Thanks for your patience. You above reported issue PDFNEWNET-35974 has been fixed in latest release of Aspose.Pdf for .NET. Please download Aspose.Pdf for .NET 9.6.0 and use following code snippet for creating an internal link to file that is already attached (embedded) to a PDF.

Document doc = new
Document(myDir+“input.pdf”);<o:p></o:p>

FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(doc.Pages[1], new Rectangle(0, 0, 16, 16), doc.EmbeddedFiles[1]);

doc.Pages[1].Annotations.Add(fileAttachment);

fileAttachment.Title = "Attached file";

doc.Save(myDir+"35794.pdf");

Please feel free to contact us for any further assistance.


Best Regards,

The issues you have found earlier (filed as PDFNEWNET-35794) have been fixed in Aspose.Pdf for .NET 9.7.0.


This message was posted using Notification2Forum from Downloads module by Aspose Notifier.