C# tutorial: Add annotations to an existing PDF


Add annotations to an existing PDF

In the previous page, you learnt how to add text annotations to a new created PDF document. In this tutorial, you will learn how to add the text and link annotations to an existing PDF document. To add the text annotation to the PDF document, you need to create an instance of PdfReader class to read pages from the PDF source file. Then create an instance of the PdfStamper class. Then use the AddAnnotation method of the PdfStamper class. This method has two arguments: the PdfAnnotation object and page number. The example code below reads all pages from a PDF source file called oop-software-development.pdf located in drive D. A text annotation (Note) is added to the bottom-left corner of every page of the PDF document.

//path to source file
String source="d:/oop-software-development.pdf";
//create PdfReader object to read the source file
PdfReader reader=new PdfReader(source);
//create PdfStamper object to modify the content of the PDF
PdfStamper stamp=new PdfStamper(reader, new FileStream("d:/outfile.pdf",FileMode.Create));
//create a Rectangle object to deine the size and location of the annotation
Rectangle rect = new Rectangle(10,30,50,50);
//create a PdfAnnotation object
PdfAnnotation annotation = PdfAnnotation.CreateText(stamp.Writer, rect, "Note", "This document is written by Dara Yuk", false, "Note");
//loop through the document and add the annotation to every page
for(int page=1;page<=reader.NumberOfPages;page++)
stamp.AddAnnotation(annotation,page);
stamp.Close();
System.Diagnostics.Process.Start("d:/outfile.pdf");
Console.Read();

add annotation to an existing pdf file

Add clickable area to the existing document

Besides adding a text annotation to every page of the PDF document, you also can add a clickable area ( link annotation ) to the pages. This task can be done by using the CreateLink method of the PdfAnnotation class. Suppose that you want to make every page of your existing PDF document clickable. It will link to a URL when the user clicks a location on every page. To achieve the goal, you need to create a Rectangle object that is a clickable area applied to every page. This Rectangle object must entirely covers the page. Then use the CreateLink method to create a clickable area. The CreateLink method has four arguments: a PdfWriter object, a Rectangle object, a PdfName highlight, and a PdfAction object. The third argument should be one of the following values:
- HIGHLIGHT_NONE—No highlighting (the default). The links created with Anchor, Chunk.setAnchor(), or Chunk.setAction() aren't highlighted when you click them.
- HIGHLIGHT_INVERT—Inverts the content of the annotation square when clicked. That's what is used in listing 7.23. If you click a movie block, the colors will be inverted.
- HIGHLIGHT_OUTLINE—Inverts the annotation border when clicked.
- HIGHLIGHT_PUSH—Displays the annotation as if it were being pushed below the surface of the page.

The destination link of the clickable area is defined by a PdfAction that is the forth argument.

//create a Rectangle object to deine the size and location of the annotation
Rectangle rect = new Rectangle(0, 0, reader.GetPageSize(1).Width, reader.GetPageSize(1).Height);
//create a PdfAnnotation object
PdfAnnotation annotation = PdfAnnotation.CreateLink(stamp.Writer, rect, PdfAnnotation.HIGHLIGHT_INVERT, new PdfAction("www.worldbestlearningcenter.com"));
annotation.Color = BaseColor.BLUE;
//loop through the document and add the annotation to every page
for(int page=1;page<=reader.NumberOfPages;page++)
stamp.AddAnnotation(annotation,page);
stamp.Close();

 


Comments

Peter comment

 Peter

This is rubish!
It doesn't add annotation to an existing document. It opens one document adds annotations to it and saves it as a different document.


2016-11-23
lorretadt comment

 lorretadt

The first and easiest way to kown vb.net add annotations to pdf is to use the rasteredage page http://www.rasteredge.com/how-to/vb-net-imaging/pdf-annotate-text-box/


2016-05-26
lorretadt comment

 lorretadt

vb.net acrobat print pdf can be download on rasteredge and create the best product for your product, try this vb.net acrobat print pdf with comments free on page rasteredge http://www.rasteredge.com/how-to/vb-net-imaging/pdf-annotate-sticky-note/


2016-05-25
lorretadt comment

 lorretadt

rasteredge provide the fast speed c# a drawing error occurred pdf printing can add annotations to pdf on this page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-drawing-markups/


2016-05-03
lorretadt comment

 lorretadt

download c# pdf highlight existing fields and try this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text-highlight/


2016-05-03
lorretadt comment

 lorretadt

download c# pdf highlight existing fields and try this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text-highlight/


2016-05-03
lorretadt comment

 lorretadt

download c# pdf highlight existing fields and try this high quality software on rasteredge, get the free trail and vip service on page http://www.rasteredge.com/how-to/csharp-imaging/pdf-annotate-text-highlight/


2016-05-03



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.