List of usage examples for com.itextpdf.text.pdf PdfReader consolidateNamedDestinations
boolean consolidateNamedDestinations
To view the source code for com.itextpdf.text.pdf PdfReader consolidateNamedDestinations.
Click Source Link
From source file:be.roots.taconic.pricingguide.util.iTextUtil.java
License:Open Source License
public static byte[] merge(byte[]... pdfAsBytes) throws DocumentException, IOException { try (final ByteArrayOutputStream copyBaos = new ByteArrayOutputStream()) { final Document doc = new Document(); final PdfCopy copy = new PdfSmartCopy(doc, copyBaos); doc.open();//from w ww.j a va2 s. com int numberOfPages = 0; final java.util.List<HashMap<String, Object>> bookmarks = new ArrayList<>(); PdfReader pdf = null; for (byte[] pdfAsByte : pdfAsBytes) { if (pdfAsByte != null && pdfAsByte.length > 0) { pdf = new PdfReader(pdfAsByte); pdf.consolidateNamedDestinations(); final List<HashMap<String, Object>> pdfBookmarks = SimpleBookmark.getBookmark(pdf); if (!CollectionUtils.isEmpty(pdfBookmarks)) { SimpleBookmark.shiftPageNumbers(pdfBookmarks, numberOfPages, null); bookmarks.addAll(pdfBookmarks); } for (int i = 1; i <= pdf.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(pdf, i)); } numberOfPages += pdf.getNumberOfPages(); } } if (pdf != null) { SimpleNamedDestination.getNamedDestination(pdf, false); } if (!CollectionUtils.isEmpty(bookmarks)) { copy.setOutlines(bookmarks); } copy.close(); return copyBaos.toByteArray(); } }
From source file:com.photon.phresco.framework.docs.impl.DocumentUtil.java
License:Apache License
/** * @param titleSection/*w w w. j a va2 s . c o m*/ * @param pdfCopy * @throws PhrescoException */ public static void addPages(InputStream titleSection, PdfCopy pdfCopy) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug("Entering Method DocumentUtil.addPages(InputStream titleSection, PdfCopy pdfCopy)"); } try { PdfReader reader = new PdfReader(titleSection); reader.consolidateNamedDestinations(); int pages = reader.getNumberOfPages(); for (int i = 1; i <= pages; i++) { PdfImportedPage importedPage = pdfCopy.getImportedPage(reader, i); pdfCopy.addPage(importedPage); } pdfCopy.freeReader(reader); } catch (BadPdfFormatException e) { e.printStackTrace(); throw new PhrescoException(e); } catch (IOException e) { throw new PhrescoException(e); } }
From source file:com.photon.phresco.framework.docs.impl.DocumentUtil.java
License:Apache License
/** * @param titleSection/*w w w . j a v a2 s . c o m*/ * @param writer * @param docu * @throws PhrescoException */ public static void addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu) throws PhrescoException { if (isDebugEnabled) { S_LOGGER.debug( "Entering Method DocumentUtil.addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu)"); } try { PdfReader reader = new PdfReader(titleSection); reader.consolidateNamedDestinations(); PdfContentByte cb = writer.getDirectContent(); int pages = reader.getNumberOfPages(); for (int i = 1; i <= pages; i++) { PdfImportedPage importedPage = writer.getImportedPage(reader, i); cb.addTemplate(importedPage, 0, 0); docu.newPage(); } } catch (IOException e) { e.printStackTrace(); throw new PhrescoException(e); } }
From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java
License:Apache License
/** * @param titleSection//from w ww. j a v a2s .c om * @param pdfCopy * @throws IOException * @throws BadPdfFormatException */ public static void addPages(InputStream titleSection, PdfCopy pdfCopy) throws IOException, BadPdfFormatException { if (isDebugEnabled) { S_LOGGER.debug("Entering Method DocumentUtil.addPages(InputStream titleSection, PdfCopy pdfCopy)"); } PdfReader reader = new PdfReader(titleSection); reader.consolidateNamedDestinations(); int pages = reader.getNumberOfPages(); for (int i = 1; i <= pages; i++) { PdfImportedPage importedPage = pdfCopy.getImportedPage(reader, i); pdfCopy.addPage(importedPage); } pdfCopy.freeReader(reader); }
From source file:com.photon.phresco.service.docs.impl.DocumentUtil.java
License:Apache License
/** * @param titleSection/*from ww w .j a va 2s.co m*/ * @param writer * @param docu * @throws IOException * @throws DocumentException */ public static void addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu) throws IOException, DocumentException { if (isDebugEnabled) { S_LOGGER.debug( "Entering Method DocumentUtil.addPages(InputStream titleSection, PdfWriter writer, com.itextpdf.text.Document docu)"); } PdfReader reader = new PdfReader(titleSection); reader.consolidateNamedDestinations(); PdfContentByte cb = writer.getDirectContent(); int pages = reader.getNumberOfPages(); for (int i = 1; i <= pages; i++) { PdfImportedPage importedPage = writer.getImportedPage(reader, i); cb.addTemplate(importedPage, 0, 0); docu.newPage(); } }
From source file:com.preselect.pdfservice.tasks.PdfConversionTask.java
License:Open Source License
public static void splitIText(OutlineItems outline, String inputFile, String outputPath) throws IOException, BadPdfFormatException, DocumentException { List<OutlineItem> outlineItems = filter(having(on(OutlineItem.class).getLevel(), equalTo(1)), outline.getItems());//from w w w .ja v a2 s.c o m PdfReader reader = new PdfReader(inputFile); reader.consolidateNamedDestinations(); int numberOfPages = reader.getNumberOfPages(); // Check if there are outline items if (outlineItems.size() <= 0) { throw new IllegalArgumentException("No outline items found!"); } // Find start and end of each chapter for (int i = 0; i < outlineItems.size(); i++) { OutlineItem outlineItem = outlineItems.get(i); // fix for first chapter start at zero int chapterStart = outlineItem.getPage() < 1 ? 1 : outlineItem.getPage(); int chapterEnd; // if last chapter is not reached yet ... if (outlineItems.size() > i + 1) { // ... get first page of next chapter and ... chapterEnd = outlineItems.get(i + 1).getPage(); // ... reduce it for one page, if it's not a one page chapter if (chapterEnd != chapterStart) { chapterEnd--; } } else { // Otherwise set the end of last chapter to the end of the document chapterEnd = numberOfPages; } File file = new File(outputPath); file.mkdirs(); String path = outputPath + File.separator + outlineItem.getId() + ".pdf"; copyDocument(reader, chapterStart, chapterEnd, path, outline); Logger.getLogger(Task.class.getName()).log(Level.INFO, "Chapter: {0} - Start:{1} <-> End: {2}", new Object[] { outlineItem.getTitle(), chapterStart, chapterEnd }); } reader.close(); }
From source file:org.ednovo.gooru.domain.service.resource.ResourceServiceImpl.java
License:Open Source License
private static List<String> splitToChaptersAndSaveFiles(String newLocalUrl) { try {//from www.j a v a 2 s.c o m HashMap<Integer, String> chapters = new HashMap<Integer, String>(); ArrayList<Integer> pages = new ArrayList<Integer>(); /** Call the split method with filename and page size as params **/ PdfReader reader = new PdfReader(newLocalUrl); reader.consolidateNamedDestinations(); List<HashMap<String, Object>> list = SimpleBookmark.getBookmark(reader); for (HashMap<String, Object> test : list) { String page = test.get(PAGE).toString(); Integer num = Integer.parseInt(page.substring(0, page.indexOf(' '))); chapters.put(num, (String) test.get(_TITLE)); pages.add(num); } int index = 1; List<String> chaptersUrls = new ArrayList<String>(); for (Integer i : pages) { String chapterUrl = null; if (pages.size() != index) { chapterUrl = splitAndSaveChapter(newLocalUrl, i, pages.get(index), chapters.get(i)); } else { chapterUrl = splitAndSaveChapter(newLocalUrl, i, reader.getNumberOfPages(), chapters.get(i)); } index++; if (chapterUrl != null) { chaptersUrls.add(chapterUrl); } } return chaptersUrls; } catch (Exception ex) { return new ArrayList<String>(); } }
From source file:org.sejda.impl.itext5.component.DefaultPdfSourceOpener.java
License:Open Source License
private PdfReader doOpen(RandomAccessSource ras, byte[] pwd) throws TaskIOException, IOException { PdfReader reader; try {//from w w w . ja v a 2 s . co m reader = new PdfReader(new RandomAccessFileOrArray(ras), pwd); } catch (BadPasswordException bpe) { throw new TaskWrongPasswordException("Unable to open the document due to a wrong password.", bpe); } reader.removeUnusedObjects(); reader.consolidateNamedDestinations(); return reader; }
From source file:org.sejda.impl.itext5.component.ITextOutlineHandler.java
License:Open Source License
public ITextOutlineHandler(PdfReader reader, String matchingTitleRegEx) { reader.consolidateNamedDestinations(); this.bookmarks = SimpleBookmark.getBookmark(reader); if (isNotBlank(matchingTitleRegEx)) { titleMatchingPattern = Pattern.compile(matchingTitleRegEx); }//w w w . j av a 2 s . c om }