/** * Insert {@see PdfPage} into specific one-based position. * * @param index one-base index of the page * @param pdfPage {@link PdfPage} to insert. */ public void addPage(int index, PdfPage pdfPage) { --index; if (index > pageRefs.size()) throw new IndexOutOfBoundsException("index"); if (index == pageRefs.size()) { addPage(pdfPage); return; } loadPage(index); pdfPage.makeIndirect(document); int parentIndex = findPageParent(index); PdfPages parentPages = parents.get(parentIndex); parentPages.addPage(index, pdfPage); pdfPage.parentPages = parentPages; correctPdfPagesFromProperty(parentIndex + 1, +1); pageRefs.add(index, pdfPage.getPdfObject()); pages.add(index, pdfPage); }
/** * Returns the {@see PdfPage} at the specified position in this list. * * @param pageNum one-based index of the element to return * @return the {@see PdfPage} at the specified position in this list */ public PdfPage getPage(int pageNum) { if (pageNum < 1 || pageNum > getNumberOfPages()) { throw new IndexOutOfBoundsException(MessageFormatUtil.format(PdfException.RequestedPageNumberIsOutOfBounds, pageNum)); } --pageNum; PdfPage pdfPage = pages.get(pageNum); if (pdfPage == null) { loadPage(pageNum); if (pageRefs.get(pageNum) != null) { int parentIndex = findPageParent(pageNum); pdfPage = new PdfPage(pageRefs.get(pageNum)); pdfPage.parentPages = parents.get(parentIndex); } else { LoggerFactory.getLogger(getClass()).error(MessageFormatUtil.format(LogMessageConstant.PAGE_TREE_IS_BROKEN_FAILED_TO_RETRIEVE_PAGE, pageNum + 1)); } pages.set(pageNum, pdfPage); } return pdfPage; }
/** * Returns the {@see PdfPage} by page's PdfDictionary. * * @param pageDictionary page's PdfDictionary * @return the {@code PdfPage} object, that wraps {@code pageDictionary}. */ public PdfPage getPage(PdfDictionary pageDictionary) { int pageNum = getPageNumber(pageDictionary); if (pageNum > 0) { return getPage(pageNum); } return null; }
protected PdfPages findPageParent(PdfPage pdfPage) { int pageNum = getPageNumber(pdfPage) - 1; int parentIndex = findPageParent(pageNum); return parents.get(parentIndex); }
/** * Removes the page at the specified position in this tree. * Shifts any subsequent elements to the left (subtracts one from their * indices). * * @param pageNum the one-based index of the PdfPage to be removed * @return the page that was removed from the list */ public PdfPage removePage(int pageNum) { PdfPage pdfPage = getPage(pageNum); if (pdfPage.isFlushed()) { Logger logger = LoggerFactory.getLogger(PdfPage.class); logger.warn(LogMessageConstant.REMOVING_PAGE_HAS_ALREADY_BEEN_FLUSHED); } if (internalRemovePage(--pageNum)) { return pdfPage; } else { return null; } }
private boolean internalRemovePage(int pageNum) { int parentIndex = findPageParent(pageNum); PdfPages pdfPages = parents.get(parentIndex); if (pdfPages.removePage(pageNum)) { if (pdfPages.getCount() == 0) { parents.remove(parentIndex); pdfPages.removeFromParent(); --parentIndex; } if (parents.size() == 0) { root = null; parents.add(new PdfPages(0, document)); } else { correctPdfPagesFromProperty(parentIndex + 1, -1); } pageRefs.remove(pageNum); pages.remove(pageNum); return true; } else { return false; } }
/** * 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); }
return; int parentIndex = findPageParent(pageNum); PdfPages parent = parents.get(parentIndex); PdfArray kids = parent.getKids(); loadPage(pageNum); } else { int from = parent.getFrom();
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) {
/** * 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); }
/** * 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); }
/** * 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); }
/** * Gets number of pages of the document. * * @return number of pages. */ public int getNumberOfPages() { checkClosingStatus(); return catalog.getPageTree().getNumberOfPages(); }
protected PdfCatalog(PdfDictionary pdfObject) { super(pdfObject); if (pdfObject == null) { throw new PdfException(PdfException.DocumentHasNoPdfCatalogObject); } ensureObjectIsAddedToDocument(pdfObject); getPdfObject().put(PdfName.Type, PdfName.Catalog); setForbidRelease(); pageTree = new PdfPagesTree(this); }
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; }
/** * Gets page number by page. * * @param page the page. * @return page number. */ public int getPageNumber(PdfPage page) { checkClosingStatus(); return catalog.getPageTree().getPageNumber(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); }
/** * 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); }