C# tutorial: PDF Annotations


PDF Annotations

In PDF, an annotation can be a note, a movie, a sound, a field, or a shape. In this tutorial, I am going to show you how to add a note or text annotation to a PDF document. You can create a note by using Annotation class. Here is the constructor of the Annotation class that can be used to create a note to add to the PDF document:

Annotation(String title, String text)

The title parameter is a string that is used as the title of the node. The text argument is the description text of the note. The example code below reads information of books from TblBook table in BookStoreDB database of SQL Server and write IDs of books to a PDF document. A note is displayed next to each ID. The title of the node is the title of the book. Its description text is the concatenation of book's author and supplier id.

//create DataContext object to get data from SQL Server database
DataContext db = new DataContext("Server=ASPIREV5\\SQLEXPRESS; Initial Catalog=BookStoreDB; Integrated Security=SSPI");
//create a document object
var doc = new Document(PageSize.A4);
//get the current directory
string path = Environment.CurrentDirectory;
string fullpath=path + "/pdfannotation.pdf";
//create PdfWriter object
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(fullpath, FileMode.Create));
//open the document for writing

doc.Open();
var books = from book in db.GetTable<TblBook>()
select book;

foreach (var book in books)
{
Paragraph pa = new Paragraph();
Chunk cid = new Chunk(book.BID);
Annotation an = new Annotation(book.Title, book.Author);
pa.Add(cid);
pa.Add(an);
doc.Add(pa);
doc.Add(Chunk.NEWLINE);

}
doc.Close();
//view the result pdf file
System.Diagnostics.Process.Start(fullpath);

pdf annotation

PdfAnnotation

PdfAnnotation class has different methods to allow you to create different types of annotation. The CreateText method is used to create a note or text annotation. Here is the general form of the CreateText method:

CreateText(PdfWriter write, Rectangle rect,String title, String contents, boolean open, String icon)

- PdfWriter writer—An instance of PdfWriter; if you're using PdfStamper, you
can use the stamper.getWriter()method to obtain a writerobject.
- Rectangle rect—The rectangle where you want the annotation to appear.
- String title—A title for the annotations.
- String contents—The content of the text annotation.
- boolean open—A Boolean value indicating whether the annotation should be open (true) or closed (false).
- String icon—The icon that should be used. Possible values are "Comment", "Key", "Note", "Help", "NewParagraph", "Paragraph", and "Insert".

The example code below adds different text annotations to the PDF document.

foreach (var book in books)
{
Chunk c = new Chunk("\u00a0\u00a0\u00a0");
c.SetAnnotation(PdfAnnotation.CreateText(writer, null, book.Title, book.Author, false, "Comment"));
Chunk k = new Chunk("\u00a0\u00a0\u00a0");
k.SetAnnotation(PdfAnnotation.CreateText(writer, null, book.Title, book.Author, false, "Key"));
Chunk n = new Chunk("\u00a0\u00a0\u00a0");
n.SetAnnotation(PdfAnnotation.CreateText(writer, null, book.Title, book.Author, false, "Note"));
Chunk i = new Chunk("\u00a0\u00a0\u00a0");
i.SetAnnotation(PdfAnnotation.CreateText(writer, null, book.Title, book.Author, false, "Insert"));
Chunk h = new Chunk("\u00a0\u00a0\u00a0");
h.SetAnnotation(PdfAnnotation.CreateText(writer, null, book.Title, book.Author, false, "Help"));
Chunk p = new Chunk("\u00a0\u00a0\u00a0");
p.SetAnnotation(PdfAnnotation.CreateText(writer, null, book.Title, book.Author, false, "Paragraph"));

Paragraph pa = new Paragraph();
Chunk cid = new Chunk(book.BID);
pa.Add(cid);
pa.Add(c);
pa.Add(k);
pa.Add(i);
pa.Add(h);
pa.Add(p);
doc.Add(pa);
doc.Add(Chunk.NEWLINE);

}

different annotations



Comments

how to import fdf annotate to  comment

 how to import fdf annotate to

how to import fdf annotate to pdf?


2017-07-06



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.