/** * Gets page number by {@link PdfDictionary}. * * @param pageDictionary {@link PdfDictionary} that present page. * @return page number by {@link PdfDictionary}. */ public int getPageNumber(PdfDictionary pageDictionary) { return catalog.getPageTree().getPageNumber(pageDictionary); }
/** * Gets number of pages of the document. * * @return number of pages. */ public int getNumberOfPages() { checkClosingStatus(); return catalog.getPageTree().getNumberOfPages(); }
/** * Gets page number by page. * * @param page the page. * @return page number. */ public int getPageNumber(PdfPage page) { checkClosingStatus(); return catalog.getPageTree().getPageNumber(page); }
/** * Gets the {@link PdfPage} instance by {@link PdfDictionary}. * * @param pageDictionary {@link PdfDictionary} that present page. * @return page by {@link PdfDictionary}. */ public PdfPage getPage(PdfDictionary pageDictionary) { checkClosingStatus(); return catalog.getPageTree().getPage(pageDictionary); }
/** * Gets the page by page number. * * @param pageNum page number. * @return page by page number. may return {@code null} in case the page tree is broken */ public PdfPage getPage(int pageNum) { checkClosingStatus(); return catalog.getPageTree().getPage(pageNum); }
private PdfObject getInheritedValue(PdfName pdfName, int type) { if (this.parentPages == null) { this.parentPages = getDocument().getCatalog().getPageTree().findPageParent(this); } PdfObject val = getInheritedValue(this.parentPages, pdfName); return val != null && val.getType() == type ? val : null; }
/** * Moves page to new place in same document with all it tag structure * * @param pageNumber number of Page that will be moved * @param insertBefore indicates before which page new one will be inserted to */ public void movePage(int pageNumber, int insertBefore) { checkClosingStatus(); if (insertBefore < 1 || insertBefore > getNumberOfPages() + 1) { throw new IndexOutOfBoundsException(MessageFormatUtil.format(PdfException.RequestedPageNumberIsOutOfBounds, insertBefore)); } PdfPage page = getPage(pageNumber); if (isTagged()) { getStructTreeRoot().move(page, insertBefore); getTagStructureContext().normalizeDocumentRootTag(); } PdfPage removedPage = catalog.getPageTree().removePage(pageNumber); if (insertBefore > pageNumber) { --insertBefore; } catalog.getPageTree().addPage(insertBefore, removedPage); }
/** * Checks page before adding. * * @param page {@link PdfPage} to add. */ protected void checkAndAddPage(PdfPage page) { if (page.isFlushed()) throw new PdfException(PdfException.FlushedPageCannotBeAddedOrInserted, page); if (page.getDocument() != null && this != page.getDocument()) throw new PdfException(PdfException.Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3).setMessageParams(page, this, page.getDocument()); catalog.getPageTree().addPage(page); }
/** * Checks page before adding and add. * * @param index one-base index of the page. * @param page {@link PdfPage} to add. */ protected void checkAndAddPage(int index, PdfPage page) { if (page.isFlushed()) { throw new PdfException(PdfException.FlushedPageCannotBeAddedOrInserted, page); } if (page.getDocument() != null && this != page.getDocument()) { throw new PdfException(PdfException.Page1CannotBeAddedToDocument2BecauseItBelongsToDocument3).setMessageParams(page, this, page.getDocument()); } catalog.getPageTree().addPage(index, page); }
/** * Removes page from the document by page number. * * @param pageNum the one-based index of the PdfPage to be removed */ public void removePage(int pageNum) { checkClosingStatus(); PdfPage removedPage = catalog.getPageTree().removePage(pageNum); if (removedPage != null) { catalog.removeOutlines(removedPage); removeUnusedWidgetsFromFields(removedPage); if (isTagged()) { getTagStructureContext().removePageTags(removedPage); } // TODO should we remove everything (outlines, tags) if page won't be removed in the end, because it's already flushed? wouldn't tags be also flushed? if (!removedPage.getPdfObject().isFlushed()) { removedPage.getPdfObject().remove(PdfName.Parent); removedPage.getPdfObject().getIndirectReference().setFree(); } dispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.REMOVE_PAGE, removedPage)); } }
PdfObject pageRoot = catalog.getPageTree().generateTree(); if (catalog.getPdfObject().isModified() || pageRoot.isModified()) { catalog.put(PdfName.Pages, pageRoot); catalog.getPdfObject().put(PdfName.Pages, catalog.getPageTree().generateTree()); catalog.getPageTree().clearPageRefs(); removeAllHandlers(); } catch (IOException e) {