How to read PDF bookmarks programmatically

Robin Rodricks picture Robin Rodricks · Mar 27, 2012 · Viewed 10.2k times · Source

I'm using a PDF converter to access the graphical data within a PDF. Everything works fine, except that I don't get a list of the bookmarks. Is there a command-line app or a C# component that can read a PDF's bookmarks? I found the iText and SharpPDF libraries and I'm currently looking through them. Have you ever done such a thing?

Answer

chidambaram picture chidambaram · May 4, 2012

Try the following code

PdfReader pdfReader = new PdfReader(filename);

IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(pdfReader);

for(int i=0;i<bookmarks.Count;i++)
{
    MessageBox.Show(bookmarks[i].Values.ToArray().GetValue(0).ToString());

    if (bookmarks[i].Count > 3)
    {
        MessageBox.Show(bookmarks[i].ToList().Count.ToString());
    }
}

Note: Don't forget to add iTextSharp DLL to your project.