Hi, I'm a newbie to itext. I'm trying to merge a few pdf generated by
itext. Some of them have bookmarks with kids (2nd level), I do not want
to loose this in the merge so here's what I want to do:
 
public static ByteArrayOutputStream mergeReports(
   ByteArrayOutputStream ... reports) {
  
  ByteArrayOutputStream mergedReport = new ByteArrayOutputStream();
  
  try {
  
   Document document = new Document();
   PdfCopy copy = new PdfCopy(document, mergedReport);
   document.open();
   PdfOutline root = copy.getRootOutline();
   copy.setViewerPreferences(PdfWriter.PageModeUseOutlines);
  
   for(ByteArrayOutputStream report : reports) {
    
    if (report != null && report.size() > 0) {
    
     // Create a PDF Reader for each report
     PdfReader reader = new PdfReader(report.toByteArray());
     int noOfPages = reader.getNumberOfPages();
 
     // Create the outline
     @SuppressWarnings("unchecked")
     List<HashMap<String, Object>> list =
SimpleBookmark.getBookmark(reader);
     
     PdfOutline outTop = null;
     HashMap<String, Object> bookmark = null;
     Map<Integer, String> kidBookmarks = new HashMap<Integer, String>();
     if (list != null) {
      bookmark = list.get(0);
      String bookmarkTitle = (String) bookmark.get("Title");
      PdfDestination dest = new PdfDestination(PdfDestination.FIT);
      outTop = new PdfOutline(root, dest, bookmarkTitle);
      List<HashMap<String, String>> kids = (List<HashMap<String,
String>>)bookmark.get("Kids");
      if(kids != null) {
       // convert this a map where the key is the page number and the
value is the bookmark title
       for(HashMap<String, String> kid : kids) {
        String title = kid.get("Title");
        String page = StringUtils.replace(kid.get("Page"), " Fit", "");
        kidBookmarks.put(new Integer(page), title);
       }
      }
     }
     
     for(int i = 1; i <= noOfPages; i++) {
      PdfImportedPage importPage = copy.getImportedPage(reader, i);
      String bookmarkTitle = kidBookmarks.get(Integer.valueOf(i));
      if(StringUtils.isNotBlank(bookmarkTitle)) {
       PdfOutline thisOutline = new PdfOutline(outTop, new
PdfDestination(PdfDestination.FIT), bookmarkTitle);
       copy.add(thisOutline);
      }
      copy.addPage(importPage);
     }
     
     reader.close();
    
    }
    
   }
   
   document.close();
   
  } catch(Exception ex) {
   throw new RuntimeException(ex);
  }
   
  return mergedReport;
 }
 
Unfortunately the child outlines DO NOT show up when I look for the kids
in the reader bookmark.
 
I tested without the kids logic and it works just fine i.e.:
 
for(int i = 1; i <= noOfPages; i++) {
      PdfImportedPage importPage = copy.getImportedPage(reader, i);
      PdfDestination dest = new PdfDestination(PdfDestination.FIT);
      PdfOutline thisOutline = new PdfOutline(outTop, dest, "test"+i);
// <-- this will show up
      copy.add(thisOutline);
      copy.addPage(importPage);
     }
 
 
Then I added something like
 
for(int i = 1; i <= noOfPages; i++) {
      PdfImportedPage importPage = copy.getImportedPage(reader, i);
    if(kids != null)
      PdfDestination dest = new PdfDestination(PdfDestination.FIT);
      PdfOutline thisOutline = new PdfOutline(outTop, dest, "test"+i);
// <-- this now NO LONGER shows up even tho it does stop at my break pt
on this line
      copy.add(thisOutline);
    }
      copy.addPage(importPage);
     }
 
Immediately the 2nd level bookmarks stopped showing up. I do not
understand just by calling bookmark.get("Kids") would have such an
effect? 
 
Maybe I'm seriously missing something here?
 
Please help. This is driving me crazy for the last four hours :)
 
Thank you!
------------------------------------------------------------------------------
Register Now & Save for Velocity, the Web Performance & Operations 
Conference from O'Reilly Media. Velocity features a full day of 
expert-led, hands-on workshops and two days of sessions from industry 
leaders in dedicated Performance & Operations tracks. Use code vel09scf 
and Save an extra 15% before 5/3. http://p.sf.net/sfu/velocityconf
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to