Search code examples
c#pdfcommand-linebookmarks

How to read PDF bookmarks programmatically


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?


Solution

  • 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.