/** * Returns the index of the first occurrence of the page in this tree * specified by it's PdfDictionary, or 0 if this tree does not contain the page. */ public int getPageNumber(PdfDictionary pageDictionary) { int pageNum = pageRefs.indexOf(pageDictionary); if (pageNum >= 0) { return pageNum + 1; } for (int i = 0; i < pageRefs.size(); i++) { if (pageRefs.get(i) == null) { loadPage(i); } if (pageRefs.get(i).equals(pageDictionary)) { return i + 1; } } return 0; }
/** * 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; }
/** * 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); }
/** * 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); }
loadPage(pageNum); } else { int from = parent.getFrom();