1

I want to add a PDF file using iTextSharp but if PDF file contains bookmarks then they should also be added.

Currently I'm using following code

Document document = new Document();
//Step 2: we create a writer that listens to the document
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(outputFileName, FileMode.Create));
writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
//Step 3: Open the document
document.Open();

PdfContentByte cb = writer.DirectContent;

//The current file path
string filename = "D:\\rtf\\2.pdf";

// we create a reader for the document
PdfReader reader = new PdfReader(filename);

//Chapter ch = new Chapter("", 1);

for (int pageNumber = 1; pageNumber < reader.NumberOfPages + 1; pageNumber++)
{
    document.SetPageSize(reader.GetPageSizeWithRotation(1));
    document.NewPage();

    // Insert to Destination on the first page
    if (pageNumber == 1)
    {
        Chunk fileRef = new Chunk(" ");
        fileRef.SetLocalDestination(filename);
        document.Add(fileRef);
    }

    PdfImportedPage page = writer.GetImportedPage(reader, pageNumber);
    int rotation = reader.GetPageRotation(pageNumber);
    if (rotation == 90 || rotation == 270)
    {
        cb.Add(page);
    }
    else
    {
        cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
    }
}

document.Close();
Bobrovsky
  • 13,789
  • 19
  • 80
  • 130
mihir patel
  • 181
  • 1
  • 6
  • Have you considered using a PdfStamper working on that original PDF with outlines to add your additional contents? A PdfStamper keeps all outlines and all interactive features of the original... – mkl Feb 06 '13 at 13:07
  • have a look at this link http://www.mikesdotnetting.com/Article/84/iTextSharp-Links-and-Bookmarks – Rachel Gallen Feb 06 '13 at 13:16
  • what language are you programming in? - c sharp? – Rachel Gallen Feb 06 '13 at 13:18
  • it might be worth looking at this too http://stackoverflow.com/questions/8140339/using-itextsharp-to-extract-and-update-links-in-an-existing-pdf/8141831#8141831 – Rachel Gallen Feb 06 '13 at 13:20
  • this is an example of using pdfstamper http://wskidmore.com/2011/04/exporting-and-importing-pdf-bookmarks-itextsharp/ – Rachel Gallen Feb 06 '13 at 13:24

1 Answers1

1

Please read Chapter 6 of my book. In table 6.1, you'll read:

Can import pages from other PDF documents. The major downside is that all interactive features of the imported page (annotations, bookmarks, fields, and so forth) are lost in the process.

This is exactly what you experience. However, if you look at the other classes listed in that table, you'll discover PdfStamper, PdfCopy, etc... which are classes that do preserve interactive features.

PdfStamper will keep the bookmarks. If you want to use PdfCopy (or PdfSmartCopy), you need to read chapter 7 to find out how to keep them. Chapter 7 isn't available for free, but you can consult the examples here: Java / C#. You need the ConcatenateBookmarks example.

Note that you're code currently looks convoluted because you're not using the correct classes. Using PdfStamper should significantly reduce the number of lines of code.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165