Link to a file attachment

I’m converting a Aspose.Pdf.Generator project over to the new Aspose.pdf I have problem position text next to File Attachment Annotation, and i also whant to creat al link to the embedded pdf documnet whne the user click som text in the document.



Aspose.Pdf.Page pdfPage = pdfDocument.Pages.Add();



List pdfAtt = PdfData.GetPdfAttachement(Session[“RapportID”].ToString());



Aspose.Pdf.FloatingBox floatBox = new Aspose.Pdf.FloatingBox();

floatBox.Width = 200;

floatBox.BackgroundColor = Color.Aqua;

floatBox.Padding.Left = 20;

floatBox.Padding.Top = 10;

double fromX = 745, heigthY = 20;



foreach (PdfAttachment att in pdfAtt)

{

Aspose.Pdf.Text.TextFragment fragment = new TextFragment(“Inline Text”);

fragment.TextState.BackgroundColor = Color.Blue;

fragment.Margin.Top = 5f;

floatBox.Paragraphs.Add(fragment);



FileSpecification fileSpecification = new FileSpecification(PdfData.OpenDocument(att.Documentid), att.Name, “Test”);



FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(pdfPage, new Rectangle(

45, fromX, 55, fromX + 15), fileSpecification);



fileAttachment.Icon = FileIcon.Paperclip;

pdfPage.Annotations.Add(fileAttachment);

fileAttachment.Title = fileAttachment.RichText = att.Name;



fromX = fromX - 20;

heigthY = heigthY + 15;



}



floatBox.Height = heigthY;

pdfPage.Paragraphs.Add(floatBox);

Hi Jorn,

Thanks for contacting support.

As per my understanding, you want to add file attachment, as well as a link to the file with some text in the PDF document. Please check following code snippet where I have added a file attachment and a link over the text as well, which will open the target file.

Document pdfDocument = new Document();

pdfDocument.Pages.Add();

pdfDocument.Pages[1].Paragraphs.Add(new TextFragment("Click Here to Get Attachment."));

pdfDocument.Save(new MemoryStream());

TextFragmentAbsorber absorber = new TextFragmentAbsorber("Click Here to Get Attachment.");

pdfDocument.Pages[1].Accept(absorber);

foreach (TextFragment tf in absorber.TextFragments)

{

    FileSpecification fs = new FileSpecification(dataDir + "target.pdf", "This is attachment.");

    FileAttachmentAnnotation file = new FileAttachmentAnnotation(tf.Page, new Aspose.Pdf.Rectangle(40, 800, 60, 820), fs);

    file.Color = Color.Transparent;

    file.Hyperlink = new FileHyperlink(dataDir + "target.pdf");

    file.Contents = "This is Attachment.";

    file.Icon = FileIcon.Paperclip;

    tf.Page.Annotations.Add(file);

    LinkAnnotation link = new LinkAnnotation(tf.Page, tf.Rectangle);

    link.Action = new GoToRemoteAction(dataDir + "target.pdf", 1);

    tf.Page.Annotations.Add(link);

}

pdfDocument.Save(dataDir + "output.pdf");

In case if my assumptions are different than what you require or you face any issue, please share input documents along with some more details of requirement, so that we can test the scenario in our environment and address it accordingly.

Best Regards,

Save Editcancel

Tanks, but this create a link to an external document. I want to open an embedded/attachment.

Hi,

Thanks for sharing the details.

In order to accomplish this requirement, please try using following code snippet.

[C#]

// load PDF file with attachment
Document doc = new Document("c:/pdftest/output_attach.pdf");

// create FileAttachment Annotation isntace
Aspose.Pdf.Annotations.FileAttachmentAnnotation fileAttachment = new Aspose.Pdf.Annotations.FileAttachmentAnnotation(doc.Pages[1], new Aspose.Pdf.Rectangle(0, 0, 16, 16), doc.EmbeddedFiles[1]);

// add FileAttachment Annotation to Annotations collection of first page
doc.Pages[1].Annotations.Add(fileAttachment);

// set Rectangular location of Attachment icon
fileAttachment.Rect = new Aspose.Pdf.Rectangle(100, 100, 120, 120);

// set attachmetn icon as PaperClip
fileAttachment.Icon = Aspose.Pdf.Annotations.FileIcon.Paperclip;
fileAttachment.Title = "Attached file";

// save updated file with link to attachment file
doc.Save("c:/pdftest/Link_to_AttachedFile.pdf");

Thanks, I have already tried this code. The problem is that there is noe text/filename behind the paperclip icon. I see that the fileAttachment.Title ends up in the author field.

Hi Jorn,


Thanks for sharing the details.

During my testing, I used an input document which already had a file added as embedded resource and in earlier shared code snippet, I referenced it behind the Paperclip icon.

Can you please share your input PDF file, so that we can further look into this matter.

Hi,

I have now created a sample document and attached the generated document. There are multiple problems. Behind the paperclip there are no name / title. Also look at the TOC that appear twice, because pdfDocument.ProcessParagraphs(). But I have to run that code before PageNumberStamp.

pdfDocument.Pages[1].Paragraphs.Add(table);



FileSpecification fileSpecification = new FileSpecification(“C:\Test\pdf - test.pdf”, “This is attachment.”);

pdfDocument.EmbeddedFiles.Add(fileSpecification);



Aspose.Pdf.Annotations.FileAttachmentAnnotation fileAttachment = new Aspose.Pdf.Annotations.FileAttachmentAnnotation(pdfPage, new Aspose.Pdf.Rectangle(0, 0, 16, 16), pdfDocument.EmbeddedFiles[1]);



fileAttachment.Rect = new Aspose.Pdf.Rectangle(100, 100, 120, 120);



fileAttachment.Icon = Aspose.Pdf.Annotations.FileIcon.Paperclip;



fileAttachment.Title = “Attached file”;



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



pdfPage = pdfDocument.Pages.Add();

pdfPage.Paragraphs.Add(new TextFragment(“Page 2”));

pdfPage = pdfDocument.Pages.Add();

pdfPage.Paragraphs.Add(new TextFragment(“Page 3”));

pdfPage = pdfDocument.Pages.Add();

pdfPage.Paragraphs.Add(new TextFragment(“Page 4”));

pdfPage = pdfDocument.Pages.Add();

pdfPage.Paragraphs.Add(new TextFragment(“Page 5”));





Aspose.Pdf.Page tocPage = pdfDocument.Pages.Insert(1);



// Create object to represent TOC information

TocInfo tocInfo = new TocInfo();

TextFragment title = new TextFragment(“Table Of Contents”);

title.TextState.FontSize = 20;

title.TextState.FontStyle = FontStyles.Bold;



// Set the title for TOC

tocInfo.Title = title;

tocPage.TocInfo = tocInfo;



// Create string objects which will be used as TOC elements

string[] titles = new string[4];

titles[0] = “First page”;

titles[1] = “Second page”;

titles[2] = “Third page”;

titles[3] = “Fourth page”;

for (int i = 0; i < 4; i++)

{

    // Create Heading object

    Aspose.Pdf.Heading heading2 = new Aspose.Pdf.Heading(1);

    TextSegment segment2 = new TextSegment();

    heading2.TocPage = tocPage;

    heading2.Segments.Add(segment2);



    heading2.DestinationPage = pdfDocument.Pages[i + 2];



    heading2.Top = pdfDocument.Pages[i + 2].Rect.Height;



    segment2.Text = titles[i];



    tocPage.Paragraphs.Add(heading2);

}



pdfDocument.ProcessParagraphs(); //Then the title for TOC displays twice.







PageNumberStamp pageNumberStamp = new PageNumberStamp();

pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;



foreach (Aspose.Pdf.Page page in pdfDocument.Pages)

{

    // put page numbering style stamp

    pageNumberStamp.Put(page);

}



pdfDocument.Save(“C:\Test\output.pdf”);

jhe-1:
Behind the paperclip there are no name / title.
Hi Jorn,

Thanks for sharing the details.

I have tested the scenario using Aspose.Pdf for .NET 17.6 in Visual Studio 2010 project running over Windows 7 and as per observations, when providing correct path for FileSpecification instance, the file is properly opening when we double click the paperclip icon. However I have observed that Attachment Title information appears as Author information under General tab over File Attachment properties. I have logged this problem as PDFNET-42917 in our issue tracking system.

For your reference, I have also attached the output generated over my end.

jhe-1:
Also look at the TOC that appear twice, because pdfDocument.ProcessParagraphs(). But I have to run that code before PageNumberStamp.
I have tested the scenario and have managed to reproduce same problem. For the sake of correction, I have logged it as PDFNET-42916 in our issue tracking system. We will further look into the details of this problem and will keep you posted on the status of correction. Please be patient and spare us little time. We are sorry for this inconvenience.

Hi
Is there any progress on this, nearly a year ago.

@jhe-1,

We are afraid that the linked ticket IDs PDFNET-42916 and PDFNET-42917 are pending for the analysis and not resolved yet. We will investigate as per the development schedules and notify you once these are fixed. Besides this, we recommend our clients to post their critical issues (or ticket IDs) in the paid support forum. Please refer to this helping link: Aspose support options