/** * Gets the last page of the document. * * @return last page. */ public PdfPage getLastPage() { return getPage(getNumberOfPages()); }
/** * Convenience method to write a text aligned about the specified point * * @param p paragraph of text to be placed to the page. By default it has no leading and is written in single line. * Set width to write multiline text. * @param x the point about which the text will be aligned and rotated * @param y the point about which the text will be aligned and rotated * @param textAlign horizontal alignment about the specified point * @return this object */ public T showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign) { return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, VerticalAlignment.BOTTOM, 0); }
/** * Convenience method to write a text aligned about the specified point * * @param p paragraph of text to be placed to the page. By default it has no leading and is written in single line. * Set width to write multiline text. * @param x the point about which the text will be aligned and rotated * @param y the point about which the text will be aligned and rotated * @param textAlign horizontal alignment about the specified point * @return this object */ public T showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign) { return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, VerticalAlignment.BOTTOM, 0); }
/** * Convenience method to write a text aligned about the specified point * * @param p paragraph of text to be placed to the page. By default it has no leading and is written in single line. * Set width to write multiline text. * @param x the point about which the text will be aligned and rotated * @param y the point about which the text will be aligned and rotated * @param textAlign horizontal alignment about the specified point * @param vertAlign vertical alignment about the specified point * @return this object */ public T showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign) { return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, vertAlign, 0); }
/** * Convenience method to write a text aligned about the specified point * * @param p paragraph of text to be placed to the page. By default it has no leading and is written in single line. * Set width to write multiline text. * @param x the point about which the text will be aligned and rotated * @param y the point about which the text will be aligned and rotated * @param textAlign horizontal alignment about the specified point * @param vertAlign vertical alignment about the specified point * @return this object */ public T showTextAligned(Paragraph p, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign) { return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, vertAlign, 0); }
/** * 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); }
private void loadPagesFromReader(PdfDocument doc, List<PdfDictionary> pages, List<PdfIndirectReference> pagesRef) { int numOfPages = doc.getNumberOfPages(); for (int i = 0; i < numOfPages; ++i) { pages.add(doc.getPage(i + 1).getPdfObject()); pagesRef.add(pages.get(i).getIndirectReference()); } }
private PdfPage getPageByOutline(int fromPage, PdfOutline outline) { int size = pdfDocument.getNumberOfPages(); for (int i = fromPage; i <= size; i++) { PdfPage pdfPage = pdfDocument.getPage(i); List<PdfOutline> outlineList = pdfPage.getOutlines(false); if (outlineList != null) { for (PdfOutline pdfOutline : outlineList) { if (pdfOutline.equals(outline)) { return pdfPage; } } } } return null; }
/** * Adds some pages so that the overall number is at least n. * Returns the page size of the n'th page. */ private PageSize ensureDocumentHasNPages(int n, PageSize customPageSize) { PageSize lastPageSize = null; while (document.getPdfDocument().getNumberOfPages() < n) { lastPageSize = addNewPage(customPageSize); } return lastPageSize; }
private void checkPages(PdfDocument document) { for (int i = 1; i <= document.getNumberOfPages(); i++) { checkPage(document.getPage(i)); } }
private void checkPages(PdfDocument document) { for (int i = 1; i <= document.getNumberOfPages(); i++) { checkPage(document.getPage(i)); } }
/** * Adds some pages so that the overall number is at least n. * Returns the page size of the n'th page. */ private PageSize ensureDocumentHasNPages(int n, PageSize customPageSize) { PageSize lastPageSize = null; while (document.getPdfDocument().getNumberOfPages() < n) { lastPageSize = addNewPage(customPageSize); } return lastPageSize; }
/** * 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 adds the field to the last page in the document. * If there's no pages, creates a new one. * * @param field the {@link PdfFormField} to be added to the form */ public void addField(PdfFormField field) { PdfPage page; if (document.getNumberOfPages() == 0) { document.addNewPage(); } page = document.getLastPage(); addField(field, page); }
/** * This method adds the field to the last page in the document. * If there's no pages, creates a new one. * * @param field the {@link PdfFormField} to be added to the form */ public void addField(PdfFormField field) { PdfPage page; if (document.getNumberOfPages() == 0) { document.addNewPage(); } page = document.getLastPage(); addField(field, page); }
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(); }
private int getExplicitDestinationPageNum(PdfArray explicitDest) { PdfIndirectReference pageReference = (PdfIndirectReference) explicitDest.get(0, false); PdfDocument doc = pageReference.getDocument(); for (int i = 1; i <= doc.getNumberOfPages(); ++i) { if (doc.getPage(i).getPdfObject().getIndirectReference().equals(pageReference)) return i; } throw new IllegalArgumentException("PdfLinkAnnotation comparison: Page not found."); }
/** * Convenience method to write a text aligned about the specified point * * @param text text to be placed to the page * @param x the point about which the text will be aligned and rotated * @param y the point about which the text will be aligned and rotated * @param textAlign horizontal alignment about the specified point * @param vertAlign vertical alignment about the specified point * @param angle the angle of rotation applied to the text, in radians * @return this object */ public T showTextAligned(String text, float x, float y, TextAlignment textAlign, VerticalAlignment vertAlign, float angle) { Paragraph p = new Paragraph(text).setMultipliedLeading(1).setMargin(0); return showTextAligned(p, x, y, pdfDocument.getNumberOfPages(), textAlign, vertAlign, angle); }
private PdfPage getFieldPage(PdfDictionary annotDic) { PdfDictionary pageDic = annotDic.getAsDictionary(PdfName.P); if (pageDic != null) { return document.getPage(pageDic); } for (int i = 1; i <= document.getNumberOfPages(); i++) { PdfPage page = document.getPage(i); if (!page.isFlushed()) { PdfAnnotation annotation = PdfAnnotation.makeAnnotation(annotDic); if (annotation != null && page.containsAnnotation(annotation)) { return page; } } } return null; }
@Override public void flush() { for (int i = 0; i < getDocument().getNumberOfPages(); ++i) { createParentTreeEntryForPage(getDocument().getPage(i + 1)); } getPdfObject().put(PdfName.ParentTree, getParentTreeHandler().buildParentTree()); getPdfObject().put(PdfName.ParentTreeNextKey, new PdfNumber((int) getDocument().getNextStructParentIndex())); if (!getDocument().isAppendMode()) { flushAllKids(this); } super.flush(); }