List of usage examples for org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline PDDocumentOutline PDDocumentOutline
public PDDocumentOutline()
From source file:com.github.joemcintyre.pdffinish.PDFFinish.java
License:Open Source License
/** * Update table of contents in destination document. * /* ww w. j a va 2s . c om*/ * @param document PDF document to update. */ private void updateTOC(PDDocument document) { PDDocumentOutline outline = new PDDocumentOutline(); document.getDocumentCatalog().setDocumentOutline(outline); PDOutlineItem topItem = new PDOutlineItem(); topItem.setTitle(title); outline.appendChild(topItem); try { PDFTextFinder finder = new PDFTextFinder(fontList); List<PDFTextFinder.PDFText> headings = finder.getTextList(document); PDOutlineItem level[] = { topItem, null, null, null }; for (PDFTextFinder.PDFText heading : headings) { PDPageXYZDestination dest = new PDPageXYZDestination(); dest.setPage(heading.page); PDOutlineItem bookmark = new PDOutlineItem(); bookmark.setDestination(dest); bookmark.setTitle(heading.text); level[heading.tag - 1].appendChild(bookmark); level[heading.tag] = bookmark; } } catch (IOException e) { System.out.println("Error :" + e); } topItem.openNode(); outline.openNode(); }
From source file:com.jt.tool.pdf.CreateBookmarks.java
License:Apache License
public static void createBookmark(String srcFile, String targetFile, String reg) throws Exception { PDDocument document = null;/*from ww w.j a v a2 s . c o m*/ try { document = PDDocument.load(new File(srcFile)); if (document.isEncrypted()) { System.err.println("Error: Cannot add bookmarks to encrypted document."); System.exit(1); } PDDocumentOutline outline = new PDDocumentOutline(); document.getDocumentCatalog().setDocumentOutline(outline); PDOutlineItem pagesOutline = new PDOutlineItem(); pagesOutline.setTitle("All Pages"); // outline.appendChild(pagesOutline); List pages = new ArrayList(); // document.getDocumentCatalog().getAllPages(); for (int i = 12; i < pages.size(); i++) { String pageText = getPageText(document, i + 1, 0); String[] strings = matchTitle(pageText, reg); if (makeBookmark(strings)) { PDPage page = (PDPage) pages.get(i); PDPageFitWidthDestination dest = new PDPageFitWidthDestination(); dest.setPage(page); PDOutlineItem bookmark = new PDOutlineItem(); bookmark.setDestination(dest); bookmark.setTitle(strings[0]); // pagesOutline.appendChild(bookmark); System.out.println("add " + strings[0]); } } pagesOutline.openNode(); outline.openNode(); document.save(targetFile); } finally { if (document != null) { document.close(); } } }
From source file:com.quanticate.opensource.pdftkbox.Bookmarks.java
License:Apache License
public void importBookmarks(BufferedReader bookmarkText, File output) throws IOException { // Process the wanted bookmarks text List<PDFBookmark> bookmarks = parseBookmarks(bookmarkText); // Prepare for the new bookmarks PDDocumentOutline outline = new PDDocumentOutline(); document.getDocumentCatalog().setDocumentOutline(outline); // Import with recursive descent boolean valid = importAllBookmarks(bookmarks, outline); // Save the new version, if appropriate if (valid) {/*from ww w . j av a 2 s . c om*/ document.save(output); } }
From source file:de.berber.kindle.annotator.lib.PDFAnnotator.java
License:Apache License
@SuppressWarnings("unchecked") public boolean run() { // read all annotations final List<Annotation> annotations = new KindleAnnotationReader(cc, pdfFile).read(); if (annotations.size() == 0) { return true; }/*w w w .j a v a2 s . c o m*/ PDDocument document = null; // annotate pdf try { document = PDDocument.load(pdfFile); //inDocument.decrypt(pass); // get outline for bookmarks PDDocumentOutline documentOutline = document.getDocumentCatalog().getDocumentOutline(); if (documentOutline == null) { // if there is no document outline we have to create a new one. documentOutline = new PDDocumentOutline(); document.getDocumentCatalog().setDocumentOutline(documentOutline); } assert documentOutline != null; // convert annotations for each page int pageNumber = 0; for (PDPage page : (List<PDPage>) document.getDocumentCatalog().getAllPages()) { for (final Annotation dxAnn : annotations) { dxAnn.toPDAnnotation(pageNumber, documentOutline, page); } pageNumber++; } //inDocument.setAllSecurityToBeRemoved(true); document.save(outFile.toString()); } catch (FileNotFoundException e) { LOG.error("Could not find input file " + pdfFile); return false; } catch (IOException e) { LOG.error("IOError while writing result file " + outFile); return false; } catch (COSVisitorException e) { LOG.error("PDFBox error while storing result file " + outFile); return false; } finally { if (document != null) { try { document.close(); } catch (IOException e) { LOG.error("Error while closing PDF document " + pdfFile); } } } return true; }
From source file:org.pdfgal.pdfgal.pdfgal.impl.PDFGalImpl.java
License:Open Source License
@Override public void addBookmarks(final String inputUri, final String outputUri, final String title, final List<PDFGalBookmark> pdfGalBookmarksList) throws IOException, COSVisitorException { if (StringUtils.isNotBlank(inputUri) && StringUtils.isNotBlank(outputUri) && StringUtils.isNotEmpty(title) && CollectionUtils.isNotEmpty(pdfGalBookmarksList)) { final PDDocument doc = PDDocument.load(inputUri); final PDDocumentOutline outline = new PDDocumentOutline(); doc.getDocumentCatalog().setDocumentOutline(outline); final PDOutlineItem pagesOutline = new PDOutlineItem(); pagesOutline.setTitle(title);/*from ww w .j a v a2 s .c o m*/ @SuppressWarnings("unchecked") final List<PDPage> pages = doc.getDocumentCatalog().getAllPages(); outline.appendChild(pagesOutline); for (final PDFGalBookmark pdfGalBookmark : pdfGalBookmarksList) { if (pdfGalBookmark != null && pdfGalBookmark.isInitializated()) { final PDPage page = pages.get(pdfGalBookmark.getPage() - 1); final PDPageFitWidthDestination dest = new PDPageFitWidthDestination(); dest.setPage(page); final PDOutlineItem bookmark = new PDOutlineItem(); bookmark.setDestination(dest); bookmark.setTitle(pdfGalBookmark.getText()); pagesOutline.appendChild(bookmark); } } pagesOutline.openNode(); outline.openNode(); doc.save(outputUri); doc.close(); } else { throw new IllegalArgumentException(Constants.ILLEGAL_ARGUMENT_EXCEPTION_MESSAGE); } }
From source file:org.pdfmetamodifier.OutlineHelper.java
License:Apache License
/** * Convert list of lines to Outlines (bookmarks) object. * //from w w w . j a v a2s .c o m * @param lineList * Source list of lines with Outlines (bookmarks) representation. * @return Outlines (bookmarks) object. */ public static PDDocumentOutline lineListToOutlines(final PDPageTree pages, final List<String> lineList) { final PDDocumentOutline outlines = new PDDocumentOutline(); if (lineList != null) { final List<Integer> shifts = new ArrayList<>(); final Map<String, PDOutlineItem> linesToBookmarks = new HashMap<>(); for (int i = 0; i < lineList.size(); ++i) { final String line = lineList.get(i); // Parse string. final Matcher matcher = OUTLINE_LINE_PATTERN.matcher(line); if (matcher.matches()) { final int shift = matcher.group("shift").length(); final String title = matcher.group("title"); PDOutlineItem outlineItem = null; final int separatorLastIndex = title.lastIndexOf("|"); if (separatorLastIndex >= 0) { try { // Set title. final String correctTitle = title.substring(0, separatorLastIndex); // Set page destination. final int pageNumber = Integer.parseInt(title.substring(separatorLastIndex + 1)); // Create Outline (bookmark) with page number. outlineItem = createOutlineItem(correctTitle, pageNumber, pages); } catch (NumberFormatException e) { // Ignore: we have Outline (bookmark) without page number. } } if (outlineItem == null) { // Create Outline (bookmark) without page number. outlineItem = createOutlineItem(title); } // Map lines to generated Outline (bookmark). linesToBookmarks.put(line, outlineItem); // Remember level of Outline (bookmark). shifts.add(shift); // Find parent position. int parentPosition = i - 1; while (parentPosition >= 0 && shifts.get(parentPosition) >= shift) { --parentPosition; } if (parentPosition >= 0) { final String parentLine = lineList.get(parentPosition); final PDOutlineItem parentBookmark = linesToBookmarks.get(parentLine); parentBookmark.addLast(outlineItem); } else { outlines.addLast(outlineItem); } } else { shifts.add(Integer.MAX_VALUE); // Ignore wrong Outline (bookmark) lines, but print error message into console. System.err.println(String.format("Outline (bookmark) have a wrong format: '%s'!", line)); } } } return outlines; }
From source file:org.pdfsam.pdfbox.component.PagesExtractor.java
License:Open Source License
private void init() { this.outlineMerger = new OutlineMerger(originalDocument); this.outline = new PDDocumentOutline(); this.destinationDocument = new PDDocumentHandler(); this.destinationDocument.setDocumentInformation(originalDocument.getDocumentInformation()); this.destinationDocument.setViewerPreferences(originalDocument.getDocumentCatalog().getViewerPreferences()); this.destinationDocument.setPageLayout(originalDocument.getDocumentCatalog().getPageLayout()); this.destinationDocument.setPageMode(originalDocument.getDocumentCatalog().getPageMode()); }
From source file:org.pdfsam.pdfbox.component.PDDocumentHandlerTest.java
License:Open Source License
@Test public void setDocumentOutline() { PDDocumentOutline outline = new PDDocumentOutline(); victim.setDocumentOutline(outline); verify(catalog).setDocumentOutline(outline); }
From source file:uia.pdf.PDFMaker.java
License:Apache License
/** * Constructor./*from w w w .ja v a 2s .c o m*/ * @param font Font. * @throws IOException */ public PDFMaker(PDFont font) throws IOException { this.temp = new ArrayList<PDOutlineItem>(); this.doc = new PDDocument(); this.font = font; this.factory = new ValueParserFactory(); this.docOutline = new PDDocumentOutline(); this.doc.getDocumentCatalog().setDocumentOutline(this.docOutline); PDOutlineItem rootOI = new PDOutlineItem(); rootOI.setTitle("All"); this.docOutline.addLast(rootOI); this.hierarchyOI = new ArrayDeque<PDOutlineItem>(); this.hierarchyOI.push(rootOI); this.bookmarkPages = new ArrayList<BookmarkPage>(); }
From source file:uia.pdf.PDFMaker.java
License:Apache License
/** * Constructor./*from ww w . j av a2 s . c om*/ * @param fontFile TTF font file. * @throws IOException */ public PDFMaker(File fontFile) throws IOException { this.temp = new ArrayList<PDOutlineItem>(); this.doc = new PDDocument(); this.font = PDType0Font.load(this.doc, fontFile); this.factory = new ValueParserFactory(); this.docOutline = new PDDocumentOutline(); this.doc.getDocumentCatalog().setDocumentOutline(this.docOutline); PDOutlineItem rootOI = new PDOutlineItem(); rootOI.setTitle("All"); this.docOutline.addLast(rootOI); this.hierarchyOI = new ArrayDeque<PDOutlineItem>(); this.hierarchyOI.push(rootOI); this.bookmarkPages = new ArrayList<BookmarkPage>(); }