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

Free Support Forum - aspose.com

How to create link to attachment using Aspose.Pdf.Generator

Hello,

Is there any way to insert a link to attached pdf file using Aspose.Pdf.Generator? I tried to use Hyparlink but it seems that it does not work with such type of content. Here is my code:

    Dim pdf1 As New Aspose.Pdf.Generator.Pdf()

    Dim Attachment As New Aspose.Pdf.Generator.DocumentAttachment
    Attachment.DocumentAttachmentFile = "C:\Temp\AttachedDocument.pdf"
    Attachment.ID = "1234567890"
    pdf1.DocumentAttachments.Add(Attachment)

    Dim section1 As Aspose.Pdf.Generator.Section = pdf1.Sections.Add()
    Dim text1 As New Aspose.Pdf.Generator.Text(section1)
    section1.Paragraphs.Add(text1)
    Dim segment1 As New Aspose.Pdf.Generator.Segment()
    segment1 = text1.Segments.Add("this is a link")
    segment1.TextInfo.IsUnderline = True
    segment1.Hyperlink.LinkType = Aspose.Pdf.Generator.HyperlinkType.Local
    segment1.Hyperlink.TargetID = "1234567890"

   pdf1.Save("C:\Temp\GeneratedDocument.pdf")

@tak,
Kindly send us your source PDF and let us know which Aspose.Pdf for .NET API you are using. Aspose.Pdf.Generator namespace is an old legacy approach and we recommend our clients to migrate from Aspose.Pdf.Generator approach to Aspose.Pdf for .NET DOM approach. Kindly see developer’s guide section about the DOM approach: Working with Aspose.Pdf

You can create a link to a file which is already attached in a PDF document with the new DOM approach. Please check second section in following documentation link for sample code snippet and details.

Best Regards,
Imran Rafique

I am using Aspose.Pdf.dll version 17.2.0.0. I do not have source PDF at all. As you can see from my example, my intention is to generate a completely new pdf from scratch. What I need to do:
1 - create a new pdf
2 - add an attachment to this pdf
3 - insert into a newly created pdf link to the attachment with text “details” and this link should be located inside existing text
Is it possible to create such think using any approach - old or new? Your suggestion to use methods from “Create PDF Document Link in a PDF file” is not what I need as it creates annotation and locates it using fixed coordinates.

@tak,
Thank you for the inquiry. The Document class instance, presents a PDF document and you can create a new PDF with the following code snippet:

[C#]

Aspose.Pdf.Document doc = new Aspose.Pdf.Document();
doc.Pages.Add().Paragraphs.Add(new Aspose.Pdf.Text.TextFragment("Hello World"));
doc.Save(@"c:\temp\HelloWorld.pdf");

You can link an attachment using fixed coordinates, and API shows an icon in place of the specified location.

[C#]

// Open document
Document document = new Document();
// add page
Aspose.Pdf.Page page = document.Pages.Add();
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification(@"C:\Pdf\test155\test.txt", "Enter file name");

// Add attachment to document's attachment collection
document.EmbeddedFiles.Add(fileSpecification);

// Create attachment annoation
FileAttachmentAnnotation fileAttachment = new FileAttachmentAnnotation(page, new Aspose.Pdf.Rectangle(0, 0, 16, 16), document.EmbeddedFiles[1]);
// Add attachment to first page of PDF
page.Annotations.Add(fileAttachment);
// Set title for attachment
fileAttachment.Title = "details";
            
// Save the PDF file
document.Save(@"C:\Pdf\test155\CreateDocumentLink.pdf");

In order to fulfill your requirement, we have logged an investigation under the ticket ID PDFNET-43059 in our issue tracking system. We have linked your post to this ticket and will keep you informed regarding any available updates.

Best Regards,
Imran Rafique