/** * Convenience method for fast PdfCanvas creation by a certain page. * * @param doc The document * @param pageNum The page number */ public PdfCanvas(PdfDocument doc, int pageNum) { this(doc.getPage(pageNum)); }
/** * Gets the last page of the document. * * @return last page. */ public PdfPage getLastPage() { return getPage(getNumberOfPages()); }
/** * Get the first page of the document. * * @return first page of the document. */ public PdfPage getFirstPage() { checkClosingStatus(); return getPage(1); }
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 void moveToNextPage() { // We don't flush this page immediately, but only flush previous one because of manipulations with areas in case // of keepTogether property. if (immediateFlush && currentPageNumber > 1) { document.getPdfDocument().getPage(currentPageNumber - 1).flush(); } currentPageNumber++; } }
private List<PdfLinkAnnotation> getLinkAnnotations(int pageNum, PdfDocument document) { List<PdfLinkAnnotation> linkAnnotations = new ArrayList<>(); List<PdfAnnotation> annotations = document.getPage(pageNum).getAnnotations(); for (PdfAnnotation annotation : annotations) { if (PdfName.Link.equals(annotation.getSubtype())) { linkAnnotations.add((PdfLinkAnnotation) annotation); } } return linkAnnotations; }
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)); } }
private void moveToNextPage() { // We don't flush this page immediately, but only flush previous one because of manipulations with areas in case // of keepTogether property. if (immediateFlush && currentPageNumber > 1) { document.getPdfDocument().getPage(currentPageNumber - 1).flush(); } currentPageNumber++; } }
/** * Draws a String containing valid SVG to a document, on a given page * number on the provided x and y coordinate. * * @param content the String value containing valid SVG content * @param document the {@link PdfDocument} instance to draw on * @param pageNo the page to draw on * @param x x-coordinate of the location to draw at * @param y y-coordinate of the location to draw at */ public static void drawOnDocument(String content, PdfDocument document, int pageNo, float x, float y) { checkNull(document); drawOnPage(content, document.getPage(pageNo), x, y); }
/** * Draws a String containing valid SVG to a document, on a given page * number on the provided x and y coordinate. * * @param content the Stream object containing valid SVG content * @param document the {@link PdfDocument} instance to draw on * @param pageNo the page to draw on * @param x x-coordinate of the location to draw at * @param y y-coordinate of the location to draw at * @param props a container for extra properties that customize the behavior */ public static void drawOnDocument(String content, PdfDocument document, int pageNo, float x, float y, ISvgConverterProperties props) { checkNull(document); drawOnPage(content, document.getPage(pageNo), x, y, props); }
/** * Draws a Stream containing valid SVG to a document, on a given page * number on the provided x and y coordinate. * * @param stream the Stream object containing valid SVG content * @param document the {@link PdfDocument} instance to draw on * @param pageNo the page to draw on * @param x x-coordinate of the location to draw at * @param y y-coordinate of the location to draw at * @throws IOException when the Stream cannot be read correctly */ public static void drawOnDocument(InputStream stream, PdfDocument document, int pageNo, float x, float y) throws IOException { checkNull(document); drawOnPage(stream, document.getPage(pageNo), x, y); }
private void addOutlineToPage(PdfOutline outline, Map<String, PdfObject> names) { PdfObject pageObj = outline.getDestination().getDestinationPage(names); if (pageObj instanceof PdfNumber) pageObj = getDocument().getPage(((PdfNumber) pageObj).intValue() + 1).getPdfObject(); if (pageObj != null) { List<PdfOutline> outs = pagesWithOutlines.get(pageObj); if (outs == null) { outs = new ArrayList<>(); pagesWithOutlines.put(pageObj, outs); } outs.add(outline); } }
/** * Specifies on which page the form field's widget must be shown. * * @param pageNum the page number * @return the edited field */ public PdfFormField setPage(int pageNum) { if (getWidgets().size() > 0) { PdfAnnotation annot = getWidgets().get(0); if (annot != null) { annot.setPage(getDocument().getPage(pageNum)); } } return this; }
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."); }
private static void addOutline(List<Node> nodes, PdfOutline root, PdfDocument pdfDocument) { for (Node node : nodes) { PdfOutline child = root.addOutline(node.getTitle()); child.addAction(PdfAction.createGoTo( PdfExplicitDestination.createFitH(pdfDocument.getPage(node.getPage()), pdfDocument.getPage(node.getPage()).getPageSize().getTop()))); addOutline(node.getChildren(), child, pdfDocument); } }
private static void addOutline(List<Node> nodes, PdfOutline root, PdfDocument pdfDocument) { for (Node node : nodes) { PdfOutline child = root.addOutline(node.getTitle()); child.addAction(PdfAction.createGoTo( PdfExplicitDestination.createFitH(pdfDocument.getPage(node.getPage()), pdfDocument.getPage(node.getPage()).getPageSize().getTop()))); addOutline(node.getChildren(), child, pdfDocument); } }
private void defineWidgetPageAndAddToIt(PdfPage currentPage, PdfDictionary mergedFieldAndWidget, boolean warnIfPageFlushed) { PdfAnnotation annot = PdfAnnotation.makeAnnotation(mergedFieldAndWidget); PdfDictionary pageDic = annot.getPageObject(); if (pageDic != null) { if (warnIfPageFlushed && pageDic.isFlushed()) { throw new PdfException(PdfException.PageAlreadyFlushedUseAddFieldAppearanceToPageMethodBeforePageFlushing); } PdfDocument doc = pageDic.getIndirectReference().getDocument(); PdfPage widgetPage = doc.getPage(pageDic); addWidgetAnnotationToPage(widgetPage, annot); } else { addWidgetAnnotationToPage(currentPage, annot); } }
private void defineWidgetPageAndAddToIt(PdfPage currentPage, PdfDictionary mergedFieldAndWidget, boolean warnIfPageFlushed) { PdfAnnotation annot = PdfAnnotation.makeAnnotation(mergedFieldAndWidget); PdfDictionary pageDic = annot.getPageObject(); if (pageDic != null) { if (warnIfPageFlushed && pageDic.isFlushed()) { throw new PdfException(PdfException.PageAlreadyFlushedUseAddFieldAppearanceToPageMethodBeforePageFlushing); } PdfDocument doc = pageDic.getIndirectReference().getDocument(); PdfPage widgetPage = doc.getPage(pageDic); addWidgetAnnotationToPage(widgetPage, annot); } else { addWidgetAnnotationToPage(currentPage, annot); } }
@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(); }