/** * 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); }
/** * 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); }
/** * 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); }