List of usage examples for com.lowagie.text.pdf PdfWriter getRootOutline
public PdfOutline getRootOutline()
From source file:com.amphisoft.epub2pdf.Converter.java
License:Open Source License
public void convert(String epubPath) throws IOException, DocumentException { File epubFile = new File(epubPath); if (!(epubFile.canRead())) { throw new IOException("Could not read " + epubPath); } else {/* w w w . j a va 2 s. c o m*/ System.err.println("Converting " + epubFile.getAbsolutePath()); } String epubFilename = epubFile.getName(); String epubFilenameBase = epubFilename.substring(0, epubFilename.length() - 5); String pdfFilename = epubFilenameBase + ".pdf"; File outputFile = new File(outputDir.getAbsolutePath() + File.separator + pdfFilename); epubIn = Epub.fromFile(epubPath); XhtmlHandler.setSourceEpub(epubIn); Opf opf = epubIn.getOpf(); List<String> contentPaths = opf.spineHrefs(); List<File> contentFiles = new ArrayList<File>(); for (String path : contentPaths) { contentFiles.add(new File(epubIn.getContentRoot(), path)); } Ncx ncx = epubIn.getNcx(); List<NavPoint> ncxToc = new ArrayList<NavPoint>(); if (ncx != null) { ncxToc.addAll(ncx.getNavPointsFlat()); } Tree<TocTreeNode> tocTree = TocTreeNode.buildTocTree(ncx); XhtmlHandler.setTocTree(tocTree); Document doc = new Document(); boolean pageSizeOK = doc.setPageSize(pageSize); boolean marginsOK = doc.setMargins(marginLeftPt, marginRightPt, marginTopPt, marginBottomPt); System.err.println("Writing PDF to " + outputFile.getAbsolutePath()); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(outputFile)); writer.setStrictImageSequence(true); PdfOutline bookmarkRoot = null; if (!(pageSizeOK && marginsOK)) { throw new RuntimeException("Failed to set PDF page size a/o margins"); } int fileCount = contentFiles.size(); printlnerr("Processing " + fileCount + " HTML file(s): "); int currentFile = 0; for (File file : contentFiles) { currentFile++; char progressChar; int mod10 = currentFile % 10; if (mod10 == 5) progressChar = '5'; else if (mod10 == 0) progressChar = '0'; else progressChar = '.'; printerr(progressChar); if (!(doc.isOpen())) { doc.open(); doc.newPage(); bookmarkRoot = writer.getRootOutline(); XhtmlHandler.setBookmarkRoot(bookmarkRoot); } NavPoint fileLevelNP = Ncx.findNavPoint(ncxToc, file.getName()); TreeNode<TocTreeNode> npNode = TocTreeNode.findInTreeByNavPoint(tocTree, fileLevelNP); if (fileLevelNP != null) { doc.newPage(); PdfOutline pdfOutlineParent = bookmarkRoot; if (npNode != null) { TreeNode<TocTreeNode> parent = npNode.getParent(); if (parent != null) { TocTreeNode parentTTN = parent.getValue(); if (parentTTN != null && parentTTN.getPdfOutline() != null) { pdfOutlineParent = parentTTN.getPdfOutline(); } } } PdfDestination here = new PdfDestination(PdfDestination.FIT); PdfOutline pdfTocEntry = new PdfOutline(pdfOutlineParent, here, fileLevelNP.getNavLabelText()); if (npNode != null) { npNode.getValue().setPdfDestination(here); npNode.getValue().setPdfOutline(pdfTocEntry); } } XhtmlHandler.process(file.getCanonicalPath(), doc); } printlnerr(); doc.close(); System.err.println("PDF written to " + outputFile.getAbsolutePath()); epubIn.cleanup(); }
From source file:docet.engine.PDFDocumentHandler.java
License:Apache License
private void writeTOCBookmarks(Collection<DocumentPart> documents, PdfWriter writer) { writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); PdfOutline root = writer.getRootOutline(); Map<DocumentPart, PdfOutline> outlines = new HashMap<>(); for (DocumentPart document : documents) { PdfOutline parent = outlines.getOrDefault(document.parent, root); PageBox page = document.pages.get(0); // PdfDestination dest = new PdfDestination(PdfDestination.XYZ, 0, 0, 0); PdfDestination dest = new PdfDestination(PdfDestination.FIT); LOGGER.log(Level.FINE, "Writing bookmark {0} - {1} to page {2}", new Object[] { title, document.name, document.startPageNo + page.getPageNo() }); dest.addPage(writer.getPageReference(document.startPageNo + page.getPageNo())); PdfOutline outline = new PdfOutline(parent, dest, document.name, true); outlines.put(document, outline); }//from w w w . j a va2 s . c o m }
From source file:org.areasy.common.doclet.document.Bookmarks.java
License:Open Source License
/** * Initializes the bookmarks creation./* w w w . j a va2 s .co m*/ */ public static void init() { PdfWriter writer = Document.getWriter(); rootOutline = writer.getRootOutline(); rootEntry = new BookmarkEntry(); }