0

I am writing a utility to collapse the bookmarks an exisiting PDF, and save the file as a new PDF. The platform is Java using the iText API.

The following code retrieves the existing bookmarks and recursively calls a method to close them.

/* Retrieve the bookmarks: */
List<HashMap<String, Object>> bookmarks = SimpleBookmark.getBookmark(originalPdf);
/* Now we recursively close all the bookmarks.*/
if (bookmarks != null ) {
  for (HashMap temp : bookmarks) {
    closeBookmark(originalPdf, temp, "false");
  }
}

private static void closeBookmark(PdfReader originalPdf, HashMap localBookmark, String bookmarkState) {
  localBookmark.put("Open", bookmarkState);
/* If the current bookmark has kids (lower-level bookmarks), then recursively close them as well. */
  if (localBookmark.containsKey("Kids")) {
    ArrayList<HashMap> kidMap = (ArrayList) localBookmark.get("Kids");
      for (HashMap temp : kidMap) {
        closeBookmark(originalPdf, temp, bookmarkState);
      }
    }
}

This works on PDFs created by Word, OpenOffice, and XSL-FO, but not FrameMaker or InDesign. In the latter cases, I get the collapsed bookmarks, but clicking on the bookmarks does not scroll the PDF to the destination. It seems as if the destinations are not present in either the bookmarks or the PDF body. Any suggestions?

9-Pin
  • 143
  • 8
  • 1
    It's hard to comment on this without seeing the PDF. You say it works on PDFs created by Word, OpenOffice and XSL-FO, but not when bookmarks are created using FrameMaker or InDesign. InDesign often stores the document content twice inside a `.pdf` document. Maybe you're changing the bookmarks in one representation of the document, but not in the other. In this case, it would be sufficient to throw away the redundant document content. Please share a PDF that can be used to reproduce the problem. – Bruno Lowagie Oct 05 '15 at 00:04
  • (Was on vacation, couldn't answer earlier.) For an example of a PDF from InDesign that has the symptom I describe, see https://www.census.gov/content/dam/Census/library/publications/2015/econ/g13-aspef.pdf. – 9-Pin Oct 07 '15 at 12:45

0 Answers0