How to Programmatically Mark up Text in an Existing PDF Document in C#

Users often face the situation that you may need to emphasize the most important information from all text on a PDF page. Highlight text is such a functionality which allow users to give prominence to key words or sentence. In this tip, I will introduce how to mark up text in an existing PDF document programmatically in C#.

This scenario depends on .NET PDF component.  Please download it and add Spire.Pdf.dll as reference in your project. Following code snippets demonstrate how to accomplish this task using the classes provided by this component.

Step 1: Load the sample PDF document.

PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
PdfPageBase page = doc.Pages[0];

Step 2: Find the text (words or sentence) that you want to mark up.

PdfTextFind ptf = page.FindText("Highlight text").Finds[0];

Step 3: Create a Markup Annotation based on the text.

Font font = ptf.Font;
string markupText = "Higlight text";
PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Adminstrator", "Text Markup Annotation", markupText, ptf.Position, new PdfTrueTypeFont(font));
annotation.Border = new PdfAnnotationBorder(0.75f);
annotation.TextMarkupColor = Color.Yellow;

Step 4: Add the annotation to PDF page.

page.AnnotationsWidget.Add(annotation);
doc.SaveToFile("Markup.pdf", FileFormat.PDF);

 Screenshot of Result

gorge_636052500447758198_2016-07-26_151655

 

Advertisements

Author: janewdaisy

.NET Program Beginner. Share methods about how to use C#/VB.NET to export data, operate Word, Excel, PDF and other useful skills.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s