/** * Copies a range of pages from current document to {@code toDocument} appending copied pages to the end. * Use this method if you want to copy pages across tagged documents. * This will keep resultant PDF structure consistent. * * @param pagesToCopy list of pages to be copied. * @param toDocument a document to copy pages to. * @return list of copied pages */ public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument) { return copyPagesTo(pagesToCopy, toDocument, null); }
/** * Copies a range of pages from current document to {@code toDocument}. * Use this method if you want to copy pages across tagged documents. * This will keep resultant PDF structure consistent. * * @param pagesToCopy list of pages to be copied. * @param toDocument a document to copy pages to. * @param insertBeforePage a position where to insert copied pages. * @return list of new copied pages */ public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, int insertBeforePage) { return copyPagesTo(pagesToCopy, toDocument, insertBeforePage, null); }
/** * Copies a range of pages from current document to {@code toDocument}. * Use this method if you want to copy pages across tagged documents. * This will keep resultant PDF structure consistent. * * @param pageFrom start of the range of pages to be copied. * @param pageTo end of the range of pages to be copied. * @param toDocument a document to copy pages to. * @param insertBeforePage a position where to insert copied pages. * @return list of copied pages */ public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, int insertBeforePage) { return copyPagesTo(pageFrom, pageTo, toDocument, insertBeforePage, null); }
/** * Copies a range of pages from current document to {@code toDocument} appending copied pages to the end. This range * is inclusive, both {@code page} and {@code pageTo} are included in list of copied pages. * Use this method if you want to copy pages across tagged documents. * This will keep resultant PDF structure consistent. * * @param pageFrom 1-based start of the range of pages to be copied. * @param pageTo 1-based end (inclusive) of the range of pages to be copied. This page is included in list of copied pages. * @param toDocument a document to copy pages to. * @return list of new copied pages */ public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument) { return copyPagesTo(pageFrom, pageTo, toDocument, null); }
/** * Copies a range of pages from current document to {@code toDocument}. This range is inclusive, both {@code page} * and {@code pageTo} are included in list of copied pages. * Use this method if you want to copy pages across tagged documents. * This will keep resultant PDF structure consistent. * * @param pageFrom 1-based start of the range of pages to be copied. * @param pageTo 1-based end (inclusive) of the range of pages to be copied. This page is included in list of copied pages. * @param toDocument a document to copy pages to. * @param insertBeforePage a position where to insert copied pages. * @param copier a copier which bears a special copy logic. May be null. * It is recommended to use the same instance of {@link IPdfPageExtraCopier} * for the same output document. * @return list of new copied pages */ public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, int insertBeforePage, IPdfPageExtraCopier copier) { List<Integer> pages = new ArrayList<>(); for (int i = pageFrom; i <= pageTo; i++) { pages.add(i); } return copyPagesTo(pages, toDocument, insertBeforePage, copier); }
/** * Copies a range of pages from current document to {@code toDocument} appending copied pages to the end. * Use this method if you want to copy pages across tagged documents. * This will keep resultant PDF structure consistent. * * @param pagesToCopy list of pages to be copied. * @param toDocument a document to copy pages to. * @param copier a copier which bears a special copy logic. May be null. * It is recommended to use the same instance of {@link IPdfPageExtraCopier} * for the same output document. * @return list of copied pages */ public List<PdfPage> copyPagesTo(List<Integer> pagesToCopy, PdfDocument toDocument, IPdfPageExtraCopier copier) { return copyPagesTo(pagesToCopy, toDocument, toDocument.getNumberOfPages() + 1, copier); }
/** * Copies a range of pages from current document to {@code toDocument} appending copied pages to the end. This range * is inclusive, both {@code page} and {@code pageTo} are included in list of copied pages. * Use this method if you want to copy pages across tagged documents. * This will keep resultant PDF structure consistent. * * @param pageFrom 1-based start of the range of pages to be copied. * @param pageTo 1-based end (inclusive) of the range of pages to be copied. This page is included in list of copied pages. * @param toDocument a document to copy pages to. * @param copier a copier which bears a special copy logic. May be null. * It is recommended to use the same instance of {@link IPdfPageExtraCopier} * for the same output document. * @return list of new copied pages. */ public List<PdfPage> copyPagesTo(int pageFrom, int pageTo, PdfDocument toDocument, IPdfPageExtraCopier copier) { return copyPagesTo(pageFrom, pageTo, toDocument, toDocument.getNumberOfPages() + 1, copier); }
/** * Extracts the specified page ranges from a document. * * @param pageRanges the list of page ranges for each of the resultant document. * @return the list of the resultant documents for each of the specified page range. * Be warned that these documents are not closed. */ public List<PdfDocument> extractPageRanges(List<PageRange> pageRanges) { List<PdfDocument> splitDocuments = new ArrayList<>(); for (PageRange currentPageRange : pageRanges) { PdfDocument currentPdfDocument = createPdfDocument(currentPageRange); splitDocuments.add(currentPdfDocument); pdfDocument.copyPagesTo(currentPageRange.getQualifyingPageNums(pdfDocument.getNumberOfPages()), currentPdfDocument); } return splitDocuments; }
/** * Splits the document by page numbers. * * @param pageNumbers the numbers of pages from which another document is to be started. * If the first element is not 1, then 1 is implied (i.e. the first split document will start from page 1 in any case). * @param documentReady the event listener which is called when another document is ready. * You can close this document in this listener, for instance. */ public void splitByPageNumbers(List<Integer> pageNumbers, IDocumentReadyListener documentReady) { int currentPageNumber = 1; for (int ind = 0; ind <= pageNumbers.size(); ind++) { int nextPageNumber = ind == pageNumbers.size() ? pdfDocument.getNumberOfPages() + 1 : (int) pageNumbers.get(ind); if (ind == 0 && nextPageNumber == 1) continue; PageRange currentPageRange = new PageRange().addPageSequence(currentPageNumber, nextPageNumber - 1); PdfDocument currentDocument = createPdfDocument(currentPageRange); pdfDocument.copyPagesTo(currentPageNumber, nextPageNumber - 1, currentDocument); documentReady.documentReady(currentDocument, currentPageRange); currentPageNumber = nextPageNumber; } }
public static void compressPDF(Path originPDF, Path outfilePath) throws IOException { PdfReader pdfReader = new PdfReader(originPDF.toString()); PdfDocument inputPdfDoc = new PdfDocument(pdfReader); File outputPDF = new File(outfilePath.toString()); PdfDocument outPdfDoc = new PdfDocument(new PdfWriter(outputPDF.getPath() ).setSmartMode(true)); int size = inputPdfDoc.getNumberOfPages(); inputPdfDoc.copyPagesTo(1, size, outPdfDoc); outPdfDoc.close(); inputPdfDoc.close(); }
/** * Splits a document into smaller documents with no more than @pageCount pages each. * * @param pageCount the biggest possible number of pages in a split document. * @param documentReady the event listener which is called when another document is ready. * You can close this document in this listener, for instance. */ public void splitByPageCount(int pageCount, IDocumentReadyListener documentReady) { for (int startPage = 1; startPage <= pdfDocument.getNumberOfPages(); startPage += pageCount) { int endPage = Math.min(startPage + pageCount - 1, pdfDocument.getNumberOfPages()); PageRange currentPageRange = new PageRange().addPageSequence(startPage, endPage); PdfDocument currentDocument = createPdfDocument(currentPageRange); pdfDocument.copyPagesTo(startPage, endPage, currentDocument); documentReady.documentReady(currentDocument, currentPageRange); } }
/** * This method merges pages from the source document to the current one. * <br><br> * If <i>closeSourceDocuments</i> flag is set to <i>true</i> (see {@link #setCloseSourceDocuments(boolean)}), * passed {@code PdfDocument} will be closed after pages are merged. * @param from - document, from which pages will be copied. * @param pages - List of numbers of pages which will be copied. * @return this {@code PdfMerger} instance. */ public PdfMerger merge(PdfDocument from, List<Integer> pages) { if (mergeTags && from.isTagged()) { pdfDocument.setTagged(); } if (mergeOutlines && from.hasOutlines()) { pdfDocument.initializeOutlines(); } from.copyPagesTo(pages, pdfDocument); if (closeSrcDocuments) { from.close(); } return this; }
return null; pdfDocument.copyPagesTo(startPage, endPage, toDocument);