Пример #1
0
 public static void printBookmark(PDOutlineNode bookmark, String indentation) throws IOException {
   PDOutlineItem current = bookmark.getFirstChild();
   while (current != null) {
     System.out.println(indentation + current.getTitle());
     PDDestination destination = current.getDestination();
     System.out.println("+ " + destination);
     printBookmark(current, indentation + "    ");
     current = current.getNextSibling();
   }
 }
Пример #2
0
 void extractBookmarkText(PDOutlineNode bookmark) throws SAXException {
   PDOutlineItem current = bookmark.getFirstChild();
   if (current != null) {
     xhtml.startElement("ul");
     while (current != null) {
       xhtml.startElement("li");
       xhtml.characters(current.getTitle());
       xhtml.endElement("li");
       // Recurse:
       extractBookmarkText(current);
       current = current.getNextSibling();
     }
     xhtml.endElement("ul");
   }
 }
Пример #3
0
  /**
   * 目次情報をPDFに挿入する。
   *
   * @param chapterList 目次情報の配列
   * @param destinationFileName 挿入先のPDFのファイル名
   * @throws Exception
   */
  public void createIndex(List<ChapterModel> chapterList, String destinationFileName)
      throws Exception {
    PDDocument document = PDDocument.load(destinationFileName);
    try {
      PDDocumentOutline outline = new PDDocumentOutline();
      document.getDocumentCatalog().setDocumentOutline(outline);
      PDOutlineItem pagesOutline = new PDOutlineItem();
      pagesOutline.setTitle("All Pages");
      outline.appendChild(pagesOutline);
      List pages = document.getDocumentCatalog().getAllPages();
      for (int i = 0; i < pages.size(); i++) {
        for (ChapterModel model : chapterList) {
          if (i == model.getPageNum()) {
            PDPage page = (PDPage) pages.get(i);
            PDPageFitWidthDestination dest = new PDPageFitWidthDestination();
            dest.setPage(page);
            PDOutlineItem bookmark = new PDOutlineItem();
            bookmark.setDestination(dest);
            bookmark.setTitle(model.getTitle());
            pagesOutline.appendChild(bookmark);
          }
        }
      }
      pagesOutline.openNode();
      outline.openNode();

      document.save(destinationFileName);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      document.close();
    }
  }