How do I add annotations to an existing PDF file?

Hey howzit,

I have been searching the net for ways to adding annotations (sticky notes) to PDF files programmatically, I have found one library on sourceforge.net called ITextSharp, but it creates a new PDF file (see code below) and then allows one to add annotations, I want to add annotations to an existing PDF file that is already filled with data and text.

 

 

// step 1: creation of a document-object

iTextSharp.text.Document document = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 50, 50, 50, 50);

// step 2:

// we create a writer that listens to the document

iTextSharp.text.pdf.PdfWriter writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new FileStream("dout.pdf", FileMode.Create));

// step 3: we Open the document

document.Open();

// step 4: we Add some content

document.Add(new Annotation("annotation", "This annotation is placed on an position", 100f, 500f, 200f, 600f));

// step 5: we close the document

document.Close();

 




Answer this question

How do I add annotations to an existing PDF file?

  • devgrp

    I created a little test and as far as i can see you forgot to add ta IDocListener to your document.
    Here is my test code:


    using System;
    using System.IO;
    using iTextSharp.text;
    using iTextSharp.text.pdf;

    namespace PDFTest
    {
    class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    Document doc = new Document( PageSize.A4, 50, 50, 50, 50 );
    PdfWriter writer = PdfWriter.GetInstance( doc, new FileStream("test.pfd", FileMode.OpenOrCreate) );
    doc.AddDocListener( writer );
    doc.Open();

    doc.Add( new Annotation("annotation", "This annotation is placed on an position.", 100f, 500f, 200f, 600f) );
    doc.Close();

    Console.WriterLine("Finished...");
    Console.ReadLine();
    }
    }
    }




  • _jesse

    When I changed the FileMode flag to open, it does nothing to the PDF, it does not add any annotations!

  • AL-Wakeel Adnan

    Can you post the code


  • The D

    Can you provide use the exact link to the library you use


  • KZoli

    Adding a new Annotation will add a new Annotation at the end of the collections as far as i can see in the doc's.

    You say you want to add annotaions to an existing PDF but you create a FileStream with the FileMode flag set to Create. This will truncate the existing file when it exists. So you loose all you existing data. Try to specify the FileMode flag with Open.


  • Alan Grosz

    http://prdownloads.sourceforge.net/itextsharp/iTextSharp.tutorial.01.zip download

    Open the project and go to Chap0303.cs, this is where you will find the code that I am using. The .dll file is in the debug folder



  • Keith714

    Hey, aparently you can use the pdfstamper class which you find on this site: http://itextdocs.lowagie.com/tutorial/general/copystamp/#pdfwriter

    But it seems to be the java version of the itext, because iTextSharp is a java ported lib. Perhaps you could have a look at it and figure out how to use it in C#



  • How do I add annotations to an existing PDF file?