.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
Kaviya Balasubramanian
Imran Ghani

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Adding Annotations to PDF in C#

Posted By:gorge       Posted Date: July 27, 2016    Points: 25    Category: .Net Framework    URL: http://www.dotnetspark.com  

This article introduces how to add several kinds of annotations including markup text, note, free text annotation, link annotation into PDF document in C#.
 

Introduction

Increasingly, busy professionals receive various types of written content in the form of Portable Document Format (PDF) files. This content might be in the form of contracts, financial statements, reports, and even manuals. Often, recipients of this content would like to add notes, highlights, comments, and other markups to these PDF documents. In this tip, you will learn how to markup and add annotations to PDF documents using .NET PDF component with C#.

Add Necessary Namespaces

This scenario requires PDF component to be installed on system. After installation, we should reference the DLL files from the bin folder into our C# console application and add following namespaces at the beginning.
 

using System.Drawing;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;

Following section demonstrates how to add various kinds of annotations to PDF using code.


Markup Text


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

//Locate the text that you want to markup
PdfTextFind ptf =page.FindText("Highlight text").Finds[0];
//Create a Text Markup annotation
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;   

//Add annotation to PDF page    
page.AnnotationsWidget.Add(annotation);
doc.SaveToFile("Annotation.pdf", FileFormat.PDF);



Add a Popup Note

//Initialize a PdfPopupAnnotation
RectangleF rect = new RectangleF(100, 100, 20, 20);
PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rect);
//Set the properties
popupAnnotation.Text = "Message pops when moving cursor over the Annotation icon.";
popupAnnotation.Icon = PdfPopupIcon.Comment;
popupAnnotation.Color = Color.Yellow;
page.AnnotationsWidget.Add(popupAnnotation);


Add a Free Text Annotation

//Initialize a PdfFreeTextAnnotation
RectangleF rect = new RectangleF(0, 40, 150, 50);
PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

//Set properties of annotation
textAnnotation.Text = "Free Text Annotation";
PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 10);
PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
textAnnotation.Font = font;
textAnnotation.Border = border;
textAnnotation.BorderColor = Color.Purple;
textAnnotation.Color = Color.Green;
textAnnotation.Opacity = 0.8f;
page.AnnotationsWidget.Add(textAnnotation);



Add a Link Annotation

//Create a File Link annotation
RectangleF rect = new RectangleF(100, 100, 120, 35);
string filePath = @"C:\Users\Administrator\Desktop\sample.pdf";
PdfFileLinkAnnotation link = new PdfFileLinkAnnotation(rect, filePath);
page.AnnotationsWidget.Add(link);

//Create a Free Text annotation on the same location to display text
PdfFreeTextAnnotation text = new PdfFreeTextAnnotation(rect);
text.Text = "Click here! This is a file link annotation.";
PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 10);
text.Font = font;
page.AnnotationsWidget.Add(text);



Summary

This tool can perform various PDF annotation works in very easy ways. Apart from adding a note or highlight, we could also annotate a PDF document by attaching files or adding stamps using this component. If interested, you can try them out by yourself.


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend