Hello,

I have run into an issue when adding bookmarks to an existing PDF. This
particular code has been working for some time so I¹m not sure what is
different in this case. The PDF file is attached.

The document in this case is a single page. The bookmarks are added as shown
below. When close() is called on the PdfStamper I get the following
exception:

Exception in thread "main" java.lang.ClassCastException:
com.itextpdf.text.pdf.PdfDictionary cannot be cast to
com.itextpdf.text.pdf.PRIndirectReference
    at 
com.itextpdf.text.pdf.PdfStamperImp.deleteOutlines(PdfStamperImp.java:1291)
    at 
com.itextpdf.text.pdf.PdfStamperImp.setOutlines(PdfStamperImp.java:1362)
    at com.itextpdf.text.pdf.PdfStamperImp.close(PdfStamperImp.java:181)
    at com.itextpdf.text.pdf.PdfStamper.close(PdfStamper.java:189)

Here is the code. The method buildPDFBookmarks was adapted from iText In
Action.

%< --------------------------------------------------------

  List bookmarks = SimpleBookmark.getBookmark(reader);
  if (bookmarks == null || bookmarks.size() == 0)
  {
    bookmarks = buildPDFBookmarks(reader, "Test Document");
  }
  stamper.setOutlines(bookmarks);

----------------------------------------------------------- >%

 /**
  * Build the PDF bookmarks for a given document.
  */

 private static List buildPDFBookmarks(PdfReader reader, String name)
 {
    List<HashMap<String, Object>> bookmarks =
                                 new ArrayList<HashMap<String, Object>>();
    List<HashMap<String, Object>> pages =
                                 new ArrayList<HashMap<String, Object>>();
    HashMap<String, Object> titlepage = new HashMap<String, Object>();

    int height = (int) reader.getPageSize(1).getHeight();
    titlepage.put("Title", name);
    titlepage.put("Action", "GoTo");
    titlepage.put("Page", String.format("%d XYZ %d", 1, height));


    for (int i = 1; i <= reader.getNumberOfPages(); i++)
    {
      HashMap<String, Object> page = new HashMap<String, Object>();
      page.put("Title", String.format("Page %d", i));
      page.put("Action", "GoTo");
      page.put("Page", String.format("%d XYZ %d", i, height));
      pages.add(page);
    }
      titlepage.put("Kids", pages);
      bookmarks.add(titlepage);

      return bookmarks;
 }

Attachment: ClassCastException.pdf
Description: Binary data

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure contains a
definitive record of customers, application performance, security
threats, fraudulent activity and more. Splunk takes this data and makes
sense of it. Business sense. IT sense. Common sense.
http://p.sf.net/sfu/splunk-d2dcopy1
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to