How to add file link annotation in PDF using C# and VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can add file link annotation in PDF document in ASP.NET Web Forms.
Steps to add file link annotation in PDF programmatically:
- Create a new ASP.NET Web application project.
- Install the Syncfusion.Pdf.AspNet NuGet package as reference to your .NET Framework applications from NuGet.org.
- Add a new Web Form in ASP.NET project. Right-click the project and select Add > New Item and add a Web Form from the list. Name it as MainPage.
- Add a new button in the MainPage.aspx as follows.
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Add File Link Annotation" OnClick="OnButtonClicked" /> </div> </form> </body> </html>
- Include the following namespaces in your MainPage.aspx.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Interactive; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Interactive Imports System.Drawing
- Include the following code snippet in the click event of the button in MainPage.aspx.cs to create the PDF file and download it.
C#
//Load an existing PDF document PdfLoadedDocument ldoc = new PdfLoadedDocument(Server.MapPath("~/data/input.pdf")); //Get the first page from the PDF document PdfLoadedPage lpage = ldoc.Pages[0] as PdfLoadedPage; //Creates a new rectangle RectangleF rectangle = new RectangleF(40, 40, 125, 16); //Creates a new pdf file link annotation PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(rectangle, Server.MapPath("~/data/Attachment.pdf")); //Adds this annotation to a new page lpage.Annotations.Add(fileLinkAnnotation); //Open the document in browser after saving it ldoc.Save(Server.MapPath("FileLinkAnnotation.pdf")); ldoc.Close(true);
VB.NET
'Load an existing PDF document Dim ldoc As New PdfLoadedDocument(Server.MapPath("~/data/input.pdf")) 'Get the first page from the PDF document Dim lpage As PdfLoadedPage = TryCast(ldoc.Pages(0), PdfLoadedPage) 'Creates a new rectangle Dim rectangle As New RectangleF(40, 40, 125, 16) 'Creates a new PDF file link annotation Dim fileLinkAnnotation As New PdfFileLinkAnnotation(rectangle, Server.MapPath("~/data/Attachment.pdf")) 'Adds this annotation to a new page lpage.Annotations.Add(fileLinkAnnotation) 'Open the document in browser after saving it ldoc.Save(Server.MapPath("FileLinkAnnotation.pdf")) ldoc.Close(True)
A complete working sample can be downloaded from AddFileLinkAnnotationInPDF.zip
By executing the program, you will get the PDF document as follows.
By clicking the annotation, you can open the linked file as follows.
Take a moment to peruse the documentation for working with annotations, where you can find other options like add, delete, and modify the annotation from the PDF document.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to adding various types of annotation to PDF document.
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.