C#: Add a Stamp to a PDF Document
Adding a stamp to a PDF document can enhance its visual appeal and convey important information, such as approval, confidentiality, or urgency. This simple process allows users to mark documents with customized images or text, making it easier to communicate key messages. Whether for professional use or personal projects, stamping PDFs ensures clarity and adds a professional touch.
In this article, you will learn how to add a stamp to a PDF document using C# with Spire.PDF for .NET.
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Prerequisite Knowledge
In a PDF, a stamp is an annotation or graphical element that adds supplementary information to a document. Spire.PDF for .NET includes the PdfRubberStampAnnotation class, which represents a rubber stamp. To create the appearance for the rubber stamp, you can use the PdfTemplate class, which serves as a canvas for drawing text, images, shapes, and date/time elements.
Add a Dynamic Stamp to PDF in C#
A dynamic stamp is a customizable annotation added to a PDF document to convey specific statuses, approvals, or other information. Unlike static stamps, dynamic stamps include variables or fields that can be updated in real time, such as the current date, time, username, or other custom data.
Here are the steps to add a dynamic stamp to a PDF using Spire.PDF for .NET:
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Create a PdfTemplate object with the desired size.
- Draw strings, including dynamic information like date and time, on the template using PdfTemplate.Graphics.DrawString().
- Create a PdfRubberStampAnnotation object and set the template as its appearance.
- Add the stamp to a specific PDF page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using System.Drawing; using Spire.Pdf; using Spire.Pdf.Graphics; using Spire.Pdf.Annotations; using Spire.Pdf.Annotations.Appearance; namespace AddDynamicStamp { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf"); // Get a specific page PdfPageBase page = doc.Pages[1]; // Create a PdfTemplate object PdfTemplate template = new PdfTemplate(220, 50); // Create two fonts PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Elephant", 16f, FontStyle.Bold),true); PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Times New Roman", 10f, FontStyle.Bold),true); // Create a solid brush and a gradient brush PdfSolidBrush solidBrush = new PdfSolidBrush(Color.Blue); RectangleF rectangle1 = new RectangleF(new PointF(0, 0), template.Size); PdfLinearGradientBrush linearGradientBrush = new PdfLinearGradientBrush(rectangle1, new PdfRGBColor(Color.White), new PdfRGBColor(Color.Blue), PdfLinearGradientMode.Horizontal); // Create a pen PdfPen pen = new PdfPen(solidBrush); // Create a rounded rectangle path int CornerRadius = 10; PdfPath path = new PdfPath(); path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180, 90); path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270, 90); path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0, 90); path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90, 90); path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / 2); // Draw path on the template template.Graphics.DrawPath(pen, path); template.Graphics.DrawPath(linearGradientBrush, path); // Draw text on the template String string1 = "APPROVED\n"; String string2 = "By Marketing Manager at " + DateTime.Now.ToString("HH:mm, MMM dd, yyyy"); template.Graphics.DrawString(string1, font1, solidBrush, new PointF(5, 5)); template.Graphics.DrawString(string2, font2, solidBrush, new PointF(2, 28)); // Create a rubber stamp, specifying its size and location RectangleF rectangle2 = new RectangleF(55, page.ActualSize.Height - 55 - 70, 240, 55); PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rectangle2); // Create a PdfAppearance object and apply the template as its normal state PdfAppearance apprearance = new PdfAppearance(stamp); apprearance.Normal = template; // Apply the appearance to stamp stamp.Appearance = apprearance; // Add the stamp annotation to annotation collection page.Annotations.Add(stamp); // Save the file doc.SaveToFile("DynamicStamp.pdf", FileFormat.PDF); // Dispose resources doc.Dispose(); } } }
Add an Image Stamp to PDF in C#
An image stamp in a PDF is a graphical element that is added to a document as an annotation or overlay. This stamp typically consists of an image that can be positioned at a specific location on a PDF page.
Here are the steps to add an image stamp to a PDF using Spire.PDF for .NET:
- Create a PdfDocument object.
- Load a PDF file using PdfDocument.LoadFromFile() method.
- Create a PdfTemplate object with the desired size.
- Draw strings, including dynamic information like date and time, on the template using PdfTemplate.Graphics.DrawString().
- Create a PdfRubberStampAnnotation object and set the template as its appearance.
- Add the stamp to a specific PDF page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Annotations.Appearance; using Spire.Pdf.Graphics; using System.Drawing; namespace AddImageStamp { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF document doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pdf"); // Get a specific page PdfPageBase page = doc.Pages[1]; // Load an image file PdfImage image = PdfImage.FromFile("C:\\Users\\Administrator\\Desktop\\confidential.png"); // Get the width and height of the image int width = image.Width; int height = image.Height; // Create a PdfTemplate object based on the size of the image PdfTemplate template = new PdfTemplate(width, height, true); // Draw image on the template template.Graphics.DrawImage(image, 0, 0, width, height); // Create a rubber stamp annotation, specifying its location and position RectangleF rect = new RectangleF(page.ActualSize.Width - width - 50, page.ActualSize.Height - height - 50, width, height); PdfRubberStampAnnotation stamp = new PdfRubberStampAnnotation(rect); // Create a PdfAppearance object PdfAppearance pdfAppearance = new PdfAppearance(stamp); // Set the template as the normal state of the appearance pdfAppearance.Normal = template; // Apply the appearance to the stamp stamp.Appearance = pdfAppearance; // Add the stamp annotation to PDF page.Annotations.Add(stamp); // Save the file doc.SaveToFile("ImageStamp.pdf"); // Dispose resources doc.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.
Modify and Format Annotation in PDF in C#/VB.NET
Text annotation is frequently used in PDF for creating a comment or explaining more about an item. Using Spire.PDF you can create a text mark-up annotation, edit an existing annotation and delete specified or all annotations. This article is aimed to demonstrate how to modify and format an existing text annotation in a PDF document.
Test File:
Changes we want to make:
- Modify the annotation text.
- Change the color of the annotation icon.
- Fix the annotation flag including its position, color, and type.
Code Snippet
Step 1: Create a new PDF document and load the test file.
PdfDocument pdf = new PdfDocument("test.pdf");
Step 2: Get the annotation from the document.
PdfAnnotation annotation = pdf.Pages[0].Annotations[0];
Step 3: Set the text, border, color of the annotation and lock the annotation flag.
annotation.Text = "Revised annotation"; annotation.Border = new PdfAnnotationBorder(0.75f); annotation.Color = new PdfRGBColor(Color.White); annotation.Flags = PdfAnnotationFlags.Locked;
Step 4: Save and launch the file.
pdf.SaveToFile("result.pdf"); Process.Start("result.pdf");
Target Effect:
Full Code:
using Spire.Pdf; using Spire.Pdf.Annotations; using Spire.Pdf.Graphics; using System.Diagnostics; using System.Drawing; namespace ModifyAndFormatAnnotation { class Program { static void Main(string[] args) { PdfDocument pdf = new PdfDocument("test.pdf"); PdfAnnotation annotation = pdf.Pages[0].Annotations[0]; annotation.Text = "Revised annotation"; annotation.Border = new PdfAnnotationBorder(0.75f); annotation.Color = new PdfRGBColor(Color.White); annotation.Flags = PdfAnnotationFlags.Locked; pdf.SaveToFile("result.pdf"); Process.Start("result.pdf"); } } }
Imports Spire.Pdf Imports Spire.Pdf.Annotations Imports Spire.Pdf.Graphics Imports System.Diagnostics Imports System.Drawing Namespace ModifyAndFormatAnnotation Class Program Private Shared Sub Main(args As String()) Dim pdf As New PdfDocument("test.pdf") Dim annotation As PdfAnnotation = pdf.Pages(0).Annotations(0) annotation.Text = "Revised annotation" annotation.Border = New PdfAnnotationBorder(0.75F) annotation.Color = New PdfRGBColor(Color.White) annotation.Flags = PdfAnnotationFlags.Locked pdf.SaveToFile("result.pdf") Process.Start("result.pdf") End Sub End Class End Namespace
Delete Annotation from PDF files in C#
Text annotation is often used in PDF file to show readers the extra information of the item. By using Spire.PDF you can not only add a new annotation, edit an existing annotation, but also delete annotations from PDF file. In this article, we will introduce you how to delete a particular annotation and delete all the annotation at one time.
First, check the PDF document with annotations.
Here comes to the steps of how to remove the bookmarks in C#.
- Download Spire.PDF for .NET and install it correctly. The Spire.PDF installation is clean, professional and wrapped up in a MSI installer.
- Add Spire.Pdf.dll as reference in the downloaded Bin folder though the below path: "..\Spire.Pdf\Bin\NET4.0\ Spire.Pdf.dll".
- Check the code snippet of how to delete the annotations. With Spire.PDF.NET, we can remove both the particular annotation and remove all the annotations.
The code snippet of remove the first annotation from PDF file:
PdfDocument document = new PdfDocument(); //load the pdf file document.LoadFromFile("E-iceblue.pdf"); //remove the first annotation document.Pages[0].Annotations.RemoveAt(1); document.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
The effective screenshot of remove the first annotation:
The code snippet of remove all annotations from PDF file:
PdfDocument document = new PdfDocument(); //load the pdf file document.LoadFromFile("E-iceblue.pdf"); //remove all annotations document.Pages[0].Annotations.Clear(); document.SaveToFile("result.pdf"); System.Diagnostics.Process.Start("result.pdf");
The effective screenshot of remove all the annotations:
C#: Add Various Types of Annotations to PDF
PDF annotation tools allow you to highlight text, add sticky notes, draw shapes, and insert comments directly on PDF documents. This can be useful for providing feedback, taking notes, marking up designs, and collaborating on documents. Mastering PDF annotation features can streamline workflows and improve productivity when working with PDF files.
In this article, you will learn how to programmatically add various types of annotations to a PDF document using Spire.PDF for .NET in C#.
- Add a Markup Annotation to PDF
- Add a Free Text Annotation to PDF
- Add a Popup Annotation to PDF
- Add a Shape Annotation to PDF
- Add a Web Link Annotation to PDF
- Add a File Link Annotation to PDF
- Add a Document Link Annotation to PDF
Install Spire.PDF for .NET
To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.
PM> Install-Package Spire.PDF
Add a Markup Annotation to PDF in C#
Markup annotation in PDF enables users to select and apply a colored background to emphasize or draw attention to specific text within the document.
Spire.PDF provides the PdfTextMarkupAnnotation class to work with this type of annotation. To add a markup annotation to PDF using Spire.PDF in C#, follow these steps.
- Create a PdfDocument object.
- Load a PDF document from the specified file path.
- Get a specific page from the document.
- Find the desired text from the page using the methods provided by the PdfTextFinder class.
- Create a PdfTextMarkupAnnotation object based on the text found.
- Add the annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.Annotations; using Spire.Pdf; using Spire.Pdf.Texts; using System.Drawing; namespace AddMarkupAnnotation { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Get a specific page PdfPageBase page = doc.Pages[0]; // Create a PdfTextFinder object based on the page PdfTextFinder finder = new PdfTextFinder(page); // Set the find options finder.Options.Parameter = TextFindParameter.WholeWord; finder.Options.Parameter = TextFindParameter.IgnoreCase; // Find the instances of the specified text List<PdfTextFragment> fragments = finder.Find("In order to help make the Site a secure environment" + " for the purchase and sale of Marketplace Offerings, all users are required to accept and " + "comply with these Terms of Service."); // Get the first instance PdfTextFragment textFragment = fragments[0]; // Specify annotation text String text = "There is a markup annotation added by Spire.PDF for .NET."; // Iterate through the bounds of the found text for (int i = 0; i < textFragment.Bounds.Length; i++) { // Get a specific bound RectangleF rect = textFragment.Bounds[i]; // Create a text markup annotation PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Administrator", text, rect); // Set the markup color annotation.TextMarkupColor = Color.Green; // Add the annotation to the collection of the annotations page.Annotations.Add(annotation); } // Save result to file doc.SaveToFile("AddMarkups.pdf"); // Dispose resources doc.Dispose(); } } }
Add a Free Text Annotation to PDF in C#
Free Text Annotation in PDF files enables users to add handwritten-like or typed text directly onto the document, similar to taking notes on a printed document.
Spire.PDF provides the PdfFreeTextAnnotation to work with the free text annotations in PDF. Here is how you can use to create one in a PDF document.
- Create a PdfDocument object.
- Load a PDF document from the specified file path.
- Get a specific page from the document.
- Find the desired text from the page using the methods provided by the PdfTextFinder class.
- Specify the x and y coordinates to add annotation.
- Create a PdfFreeTextAnnotation object, and set its properties like text, font, border and color.
- Add the annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.Annotations; using Spire.Pdf.Texts; using Spire.Pdf; using Spire.Pdf.Graphics; using System.Drawing; namespace AddFreeTextAnnotation { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Get a specific page PdfPageBase page = doc.Pages[0]; // Create a PdfTextFinder object based on the page PdfTextFinder finder = new PdfTextFinder(page); // Set the find options finder.Options.Parameter = TextFindParameter.WholeWord; finder.Options.Parameter = TextFindParameter.IgnoreCase; // Find the instances of the specified text List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings"); // Get the first instance PdfTextFragment textFragment = fragments[0]; // Get the text bound RectangleF rect = textFragment.Bounds[0]; // Get the x and y coordinates to add annotation float x = rect.Right; float y = rect.Bottom; // Create a free text annotation RectangleF rectangle = new RectangleF(x, y, 130, 30); PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rectangle); // Set the content of the annotation textAnnotation.Text = "There is a free text annotation\radded by Spire.PDF for .NET."; // Set other properties of annotation PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10f, PdfFontStyle.Bold); PdfAnnotationBorder border = new PdfAnnotationBorder(1f); textAnnotation.Font = font; textAnnotation.Border = border; textAnnotation.BorderColor = Color.Purple; textAnnotation.Color = Color.Green; textAnnotation.Opacity = 1.0f; // Add the annotation to the collection of the annotations page.Annotations.Add(textAnnotation); // Save result to file doc.SaveToFile("FreeTextAnnotation.pdf"); // Dispose resources doc.Dispose(); } } }
Add a Popup Annotation to PDF in C#
Popup Annotation in PDF files allows users to attach a small label or comment that pops up when clicked, revealing additional information or a short message.
Spire.PDF offers the PdfPopupAnnotation class to work with the popup annotation in PDF. The following are the steps to add a popup annotation to PDF using it.
- Create a PdfDocument object.
- Load a PDF document from the specified file path.
- Get a specific page from the document.
- Find the desired text from the page using the methods provided by the PdfTextFinder class.
- Specify the x and y coordinates to add annotation.
- Create a PdfPopupAnnotation object, and set its properties like text, icon and color.
- Add the annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.Annotations; using Spire.Pdf.Texts; using Spire.Pdf; using System.Drawing; namespace AddPopupAnnotation { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Get a specific page PdfPageBase page = doc.Pages[0]; // Create a PdfTextFinder object based on the page PdfTextFinder finder = new PdfTextFinder(page); // Set the find options finder.Options.Parameter = TextFindParameter.WholeWord; finder.Options.Parameter = TextFindParameter.IgnoreCase; // Find the instances of the specified text List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings"); // Get the first instance PdfTextFragment textFragment = fragments[0]; // Get the text bound RectangleF textBound = textFragment.Bounds[0]; // Get the x and y coordinates to add annotation float x = textBound.Right + 5; float y = textBound.Top - 15; // Create a popup annotation RectangleF rectangle = new RectangleF(x, y, 30, 30); PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rectangle); // Set the annotation text popupAnnotation.Text = "There is a popup annotation\radded by Spire.PDF for .NET."; // Set the icon and color of the annotation popupAnnotation.Icon = PdfPopupIcon.Comment; popupAnnotation.Color = Color.Red; // Add the annotation to the collection of the annotations page.Annotations.Add(popupAnnotation); // Save result to file doc.SaveToFile("PopupAnnotation.pdf"); // Dispose resources doc.Dispose(); } } }
Add a Shape Annotation to PDF in C#
Shape Annotation in PDF refers to the ability to add graphical shapes such as rectangles, circles, lines, or arrows onto a PDF document to highlight or provide additional information.
Spire.PDF offers the PdfPolyLineAnnotation class that allows the user to create a custom shape annotation in a PDF document. Here are the detailed steps.
- Create a PdfDocument object.
- Load a PDF document from the specified file path.
- Get a specific page from the document.
- Find the desired text from the page using the methods provided by the PdfTextFinder class.
- Specify the coordinates to add annotation.
- Create a PdfPloyLineAnnotation object, and set the text of the annotation.
- Add the annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.Annotations; using Spire.Pdf.Texts; using Spire.Pdf; using System.Drawing; namespace AddShapeAnnotation { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Get a specific page PdfPageBase page = doc.Pages[0]; // Create a PdfTextFinder object based on the page PdfTextFinder finder = new PdfTextFinder(page); // Set the find options finder.Options.Parameter = TextFindParameter.WholeWord; finder.Options.Parameter = TextFindParameter.IgnoreCase; // Find the instances of the specified text List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings"); // Get the first instance PdfTextFragment textFragment = fragments[0]; // Get the text bound RectangleF textBound = textFragment.Bounds[0]; // Get the coordinates to add annotation float left = textBound.Left; float top = textBound.Top; float right = textBound.Right; float bottom = textBound.Bottom; // Create a shape nnotation PdfPolyLineAnnotation polyLineAnnotation = new PdfPolyLineAnnotation(page, new PointF[] { new PointF(left, top), new PointF(right, top), new PointF(right - 5, bottom), new PointF(left - 5, bottom), new PointF(left, top) }); // Set the annotation text polyLineAnnotation.Text = "There is a shape annotation\radded by Spire.PDF for .NET."; // Add the annotation to the collection of the annotations page.Annotations.Add(polyLineAnnotation); // Save result to file doc.SaveToFile("ShapeAnnotation.pdf"); // Dispose resources doc.Dispose(); } } }
Add a Web Link Annotation to PDF in C#
Web Link Annotation in PDF documents allows users to embed hyperlinks that direct readers to websites when clicked.
Spire.PDF provides the PdfUrlAnnotation class to represent a web link annotation. The following are the steps to add a web link annotation using it.
- Create a PdfDocument object.
- Load a PDF document from the specified file path.
- Get a specific page from the document.
- Find the desired text from the page using the methods provided by the PdfTextFinder class.
- Create a PdfUrlAnnotation object based on the text found.
- Add the annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.Annotations; using Spire.Pdf.Texts; using Spire.Pdf; using System.Drawing; namespace AddUrlAnnotation { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Get a specific page PdfPageBase page = doc.Pages[0]; // Create a PdfTextFinder object based on the page PdfTextFinder finder = new PdfTextFinder(page); // Set the find options finder.Options.Parameter = TextFindParameter.WholeWord; finder.Options.Parameter = TextFindParameter.IgnoreCase; // Find the instances of the specified text List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings"); // Get the first instance PdfTextFragment textFragment = fragments[0]; // Get the text bound RectangleF textBound = textFragment.Bounds[0]; // Create a Url annotation PdfUriAnnotation urlAnnotation = new PdfUriAnnotation(textBound, "https:\\\\www.e-iceblue.com\\"); // Add the annotation to the collection of the annotations page.Annotations.Add(urlAnnotation); // Save result to file doc.SaveToFile("UrlAnnotation.pdf"); // Dispose resources doc.Dispose(); } } }
Add a File Link Annotation to PDF in C#
File link annotation in PDF documents refers to interactive links that allow users to navigate to external files directly from the PDF.
Spire.PDF offers the PdfFileLinkAnnotation class to work with the file link annotation. Here are the steps to add a file link annotation to a PDF document using it.
- Create a PdfDocument object.
- Load a PDF document from the specified file path.
- Get a specific page from the document.
- Find the desired text from the page using the methods provided by the PdfTextFinder class.
- Create a PdfFileLinkAnnotation object based on the text found.
- Add the annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.Annotations; using Spire.Pdf.Texts; using Spire.Pdf; using System.Drawing; namespace AddFileLinkAnnotation { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Get a specific page PdfPageBase page = doc.Pages[0]; // Create a PdfTextFinder object based on the page PdfTextFinder finder = new PdfTextFinder(page); // Set the find options finder.Options.Parameter = TextFindParameter.WholeWord; finder.Options.Parameter = TextFindParameter.IgnoreCase; // Find the instances of the specified text List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings"); // Get the first instance PdfTextFragment textFragment = fragments[0]; // Get the text bound RectangleF textBound = textFragment.Bounds[0]; // Create a file link annotation PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(textBound, "C:\\Users\\Administrator\\Desktop\\Report.docx"); // Add the annotation to the collection of the annotations page.Annotations.Add(fileLinkAnnotation); // Save result to file doc.SaveToFile("FileLinkAnnotation.pdf"); // Dispose resources doc.Dispose(); } } }
Add a Document Link Annotation to PDF in C#
Document link annotation in PDF files refers to hyperlinks that allow users to navigate between different pages or sections within the same PDF document.
Spire.PDF offers the PdfDocumentLinkAnnotation class to work with the document link annotation. Here are the steps to add a document link annotation to a PDF document using it.
- Create a PdfDocument object.
- Load a PDF document from the specified file path.
- Get a specific page from the document.
- Find the desired text from the page using the methods provided by the PdfTextFinder class.
- Create a PdfDocumentLinkAnnotation object based on the text found.
- Add the annotation to the page using PdfPageBase.Annotations.Add() method.
- Save the document to a different PDF file.
- C#
using Spire.Pdf.Annotations; using Spire.Pdf.Texts; using Spire.Pdf; using Spire.Pdf.General; using System.Drawing; namespace AddDocumentLinkAnnotation { class Program { static void Main(string[] args) { // Create a PdfDocument object PdfDocument doc = new PdfDocument(); // Load a PDF file doc.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Terms of service.pdf"); // Get a specific page PdfPageBase page = doc.Pages[0]; // Create a PdfTextFinder object based on the page PdfTextFinder finder = new PdfTextFinder(page); // Set the find options finder.Options.Parameter = TextFindParameter.WholeWord; finder.Options.Parameter = TextFindParameter.IgnoreCase; // Find the instances of the specified text List<PdfTextFragment> fragments = finder.Find("Marketplace Offerings"); // Get the first instance PdfTextFragment textFragment = fragments[0]; // Get the text bound RectangleF textBound = textFragment.Bounds[0]; // Create a document link annotation PdfDocumentLinkAnnotation documentLinkAnnotation = new PdfDocumentLinkAnnotation(textBound); // Set the destination of the annotation documentLinkAnnotation.Destination = new PdfDestination(doc.Pages[1]); // Add the annotation to the collection of the annotations page.Annotations.Add(documentLinkAnnotation); // Save result to file doc.SaveToFile("DocumentLinkAnnotation.pdf"); // Dispose resources doc.Dispose(); } } }
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.