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; } }
/** * Create PdfPages tree. * * @param pdfCatalog {@see PdfCatalog} */ public PdfPagesTree(PdfCatalog pdfCatalog) { this.document = pdfCatalog.getDocument(); this.pageRefs = new ArrayList<>(); this.parents = new ArrayList<>(); this.pages = new ArrayList<>(); if (pdfCatalog.getPdfObject().containsKey(PdfName.Pages)) { PdfDictionary pages = pdfCatalog.getPdfObject().getAsDictionary(PdfName.Pages); if (pages == null) throw new PdfException(PdfException.InvalidPageStructurePagesPagesMustBePdfDictionary); this.root = new PdfPages(0, Integer.MAX_VALUE, pages, null); parents.add(this.root); for (int i = 0; i < this.root.getCount(); i++) { this.pageRefs.add(null); this.pages.add(null); } } else { this.root = null; this.parents.add(new PdfPages(0, this.document)); } //in read mode we will create PdfPages from 0 to Count // and reserve null indexes for pageRefs and pages. }
/** * Appends the specified {@see PdfPage} to the end of this tree. * * @param pdfPage {@see PdfPage} */ public void addPage(PdfPage pdfPage) { PdfPages pdfPages; if (root != null) { // in this case we save tree structure if (pageRefs.size() == 0) { pdfPages = root; } else { loadPage(pageRefs.size() - 1); pdfPages = parents.get(parents.size() - 1); } } else { pdfPages = parents.get(parents.size() - 1); if (pdfPages.getCount() % leafSize == 0 && pageRefs.size() > 0) { pdfPages = new PdfPages(pdfPages.getFrom() + pdfPages.getCount(), document); parents.add(pdfPages); } } pdfPage.makeIndirect(document); pdfPages.addPage(pdfPage.getPdfObject()); pdfPage.parentPages = pdfPages; pageRefs.add(pdfPage.getPdfObject()); pages.add(pdfPage); }
dynamicLeafSize++; } else { current = new PdfPages(-1, document); nextParents.add(current); dynamicLeafSize = leafSize;
if (pdfPagesObject.getAsArray(PdfName.Kids) == null) { // pdfPagesObject is PdfPage if (lastPdfPages == null) { // possible if only first kid is PdfPage lastPdfPages = new PdfPages(parent.getFrom(), document, parent); kids.set(i, lastPdfPages.getPdfObject()); newParents.add(lastPdfPages); ? parent.getFrom() : lastPdfPages.getFrom() + lastPdfPages.getCount(); lastPdfPages = new PdfPages(from, kidsCount, pdfPagesObject, parent); newParents.add(lastPdfPages); kidsCount -= lastPdfPages.getCount();