public List<HashMap<String, Object>> getOutlineUntillPageWithOffset(int endPage, int offset) throws TaskException { if (startPage < 0 || startPage > endPage) { throw new TaskException( "Unable to process document bookmarks: start page is negative or higher then end page."); } if (bookmarks.isEmpty()) { return Collections.emptyList(); } List<HashMap<String, Object>> books = getDeepCopyBookmarks(bookmarks); if (endPage < totalNumberOfPages) { SimpleBookmark.eliminatePages(books, new int[] { endPage + 1, totalNumberOfPages }); } if (startPage > 1) { SimpleBookmark.eliminatePages(books, new int[] { 1, startPage - 1 }); SimpleBookmark.shiftPageNumbers(books, -(startPage - 1), null); } if (offset != 0) { SimpleBookmark.shiftPageNumbers(books, offset, null); } return books; }
void doExecuteMergeAll(boolean hasBookmarks, int pages) throws TaskException, IOException { when(context.getTask(parameters)).thenReturn((Task) victimTask); victim.execute(parameters); PdfReader reader = null; try { reader = getReaderFromResultFile(); assertCreator(reader); assertEquals(PdfVersion.VERSION_1_6.getVersionAsCharacter(), reader.getPdfVersion()); assertEquals(pages, reader.getNumberOfPages()); if (hasBookmarks) { assertNotNull(SimpleBookmark.getBookmark(reader)); } else { assertNull(SimpleBookmark.getBookmark(reader)); } } finally { ITextUtils.nullSafeClosePdfReader(reader); } }
public static CropJob createCropJob(ClusterJob curClusterJob) throws IOException { File source = curClusterJob.getSource(); if (source != null && source.exists()) { PdfReader reader = new PdfReader(source.getAbsolutePath()); CropJob result = new CropJob(source, reader.getNumberOfPages(), reader.getInfo(), SimpleBookmark.getBookmark(reader)); reader.close(); result.setClusterCollection(curClusterJob.getClusterCollection()); return result; } return null; }
public static CropJob createCropJob(File source) throws IOException { CropJob result = null; if (source != null && source.exists()) { PdfReader reader = new PdfReader(source.getAbsolutePath()); result = new CropJob(source, reader.getNumberOfPages(), reader.getInfo(), SimpleBookmark.getBookmark(reader)); reader.close(); return result; } return result; }
public PdfMetaInformation(final File source) throws IOException { PdfReader reader = new PdfReader(source.getAbsolutePath()); this.sourcePageCount = reader.getNumberOfPages(); this.sourceMetaInfo = reader.getInfo(); this.sourceBookmarks = SimpleBookmark.getBookmark(reader); reader.close(); }
private List<HashMap<String, Object>> getBookmarksOrEmpty(PdfReader reader) { List<HashMap<String, Object>> documentBookmarks = SimpleBookmark.getBookmark(reader); if (documentBookmarks != null) { return Collections.unmodifiableList(documentBookmarks); } return Collections.emptyList(); }
public List<HashMap<String, Object>> getOutlineWithOffset(int offset) { List<HashMap<String, Object>> books = getDeepCopyBookmarks(bookmarks); if (offset != 0) { SimpleBookmark.shiftPageNumbers(books, offset, null); } return books; }
public ITextOutlineLevelsHandler(PdfReader reader, String matchingTitleRegEx) { reader.consolidateNamedDestinations(); this.bookmarks = SimpleBookmark.getBookmark(reader); if (isNotBlank(matchingTitleRegEx)) { titleMatchingPattern = Pattern.compile(matchingTitleRegEx); } }
private static void cropMultipliedFile(File source, CropJob cropJob) throws FileNotFoundException, DocumentException, IOException { PdfReader reader = new PdfReader(source.getAbsolutePath()); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(cropJob.getDestinationFile())); stamper.setMoreInfo(cropJob.getSourceMetaInfo()); PdfDictionary pageDict; int newPageNumber = 1; for (int origPageNumber = 1; origPageNumber <= cropJob.getSourcePageCount(); origPageNumber++) { SingleCluster cluster = cropJob.getClusterCollection().getSingleCluster(origPageNumber); // if no crop was selected do nothing if (cluster.getRatiosList().size() == 0) { newPageNumber++; continue; } for (Float[] ratios : cluster.getRatiosList()) { pageDict = reader.getPageN(newPageNumber); List<Rectangle> boxes = new ArrayList<Rectangle>(); boxes.add(reader.getBoxSize(newPageNumber, "media")); boxes.add(reader.getBoxSize(newPageNumber, "crop")); int rotation = reader.getPageRotation(newPageNumber); Rectangle scaledBox = calculateScaledRectangle(boxes, ratios, rotation); PdfArray scaleBoxArray = new PdfArray(); scaleBoxArray.add(new PdfNumber(scaledBox.getLeft())); scaleBoxArray.add(new PdfNumber(scaledBox.getBottom())); scaleBoxArray.add(new PdfNumber(scaledBox.getRight())); scaleBoxArray.add(new PdfNumber(scaledBox.getTop())); pageDict.put(PdfName.CROPBOX, scaleBoxArray); pageDict.put(PdfName.MEDIABOX, scaleBoxArray); // increment the pagenumber newPageNumber++; } int[] range = new int[2]; range[0] = newPageNumber - 1; range[1] = cropJob.getSourcePageCount() + (newPageNumber - origPageNumber); SimpleBookmark.shiftPageNumbers(cropJob.getSourceBookmarks(), cluster.getRatiosList().size() - 1, range); } stamper.setOutlines(cropJob.getSourceBookmarks()); stamper.close(); reader.close(); }
private static void cropMultipliedFile(final CropDefinition cropDefinition, final File multipliedDocument, final PdfMetaInformation pdfMetaInformation) throws DocumentException, IOException { PdfReader reader = new PdfReader(multipliedDocument.getAbsolutePath()); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(cropDefinition.getDestinationFile())); stamper.setMoreInfo(pdfMetaInformation.getSourceMetaInfo()); PdfDictionary pageDict; int newPageNumber = 1; for (int sourcePageNumber = 1; sourcePageNumber <= pdfMetaInformation.getSourcePageCount(); sourcePageNumber++) { List<Float[]> rectangleList = cropDefinition.getRectanglesForPage(sourcePageNumber); // if no crop was selected do nothing if (rectangleList.isEmpty()) { newPageNumber++; continue; } for (Float[] ratios : rectangleList) { pageDict = reader.getPageN(newPageNumber); List<Rectangle> boxes = new ArrayList<Rectangle>(); boxes.add(reader.getBoxSize(newPageNumber, "media")); boxes.add(reader.getBoxSize(newPageNumber, "crop")); int rotation = reader.getPageRotation(newPageNumber); Rectangle scaledBox = RectangleHandler.calculateScaledRectangle(boxes, ratios, rotation); PdfArray scaleBoxArray = createScaledBoxArray(scaledBox); pageDict.put(PdfName.CROPBOX, scaleBoxArray); pageDict.put(PdfName.MEDIABOX, scaleBoxArray); // increment the pagenumber newPageNumber++; } int[] range = new int[2]; range[0] = newPageNumber - 1; range[1] = pdfMetaInformation.getSourcePageCount() + (newPageNumber - sourcePageNumber); SimpleBookmark.shiftPageNumbers(pdfMetaInformation.getSourceBookmarks(), rectangleList.size() - 1, range); } stamper.setOutlines(pdfMetaInformation.getSourceBookmarks()); stamper.close(); reader.close(); }
@Test public void testRetainAll() throws TaskException { OutlineMerger victim = new OutlineMerger(OutlinePolicy.RETAIN); victim.updateOutline(reader, input, 0); assertEquals(SimpleBookmark.getBookmark(reader).size(), victim.getOutline().size()); }
public void mergePdfs() throws DocumentException, IOException { String title = book.getPdfTitle() + ".pdf"; File saveFile = new File(saveFolder, title); int count = 1; while (saveFile.exists()) { title = book.getPdfTitle() + "_" + count++ + ".pdf"; saveFile = new File(saveFolder, title); } book.setInfo("saveFile", saveFile.toString()); Document document = new Document(); PdfCopy destPdf = new PdfCopy(document, new FileOutputStream(saveFile)); document.open(); PdfReader reader; int page_offset = 0; int n; ArrayList<HashMap<String, Object>> bookmarks = new ArrayList<HashMap<String, Object>>(); List<HashMap<String, Object>> tmp; count = 1; System.out.println("Start mergin\u2026"); for (File srcPdf : src) { if (Thread.interrupted()) { return; } System.out.print(":: " + count++ + "/" + src.size()); reader = new PdfReader(srcPdf.toString()); tmp = SimpleBookmark.getBookmark(reader); if (tmp != null) { SimpleBookmark.shiftPageNumbers(tmp, page_offset, null); bookmarks.addAll(tmp); } n = reader.getNumberOfPages(); page_offset += n; for (int page = 0; page < n;) { destPdf.addPage(destPdf.getImportedPage(reader, ++page)); } destPdf.freeReader(reader); reader.close(); System.out.println(" succeed."); } if (!bookmarks.isEmpty()) { destPdf.setOutlines(bookmarks); } if (book.getInfo("author") != null) document.addAuthor(book.getInfo("author")); if (book.getInfo("title") != null) document.addTitle(book.getInfo("title")); if (book.getInfo("subtitle") != null) document.addSubject(book.getInfo("subtitle")); document.close(); System.out.println("Merge complete. Saved to " + saveFile); }