/** * Add an entry to the name tree * * @param key key of the entry * @param value object to add */ public void addEntry(String key, PdfObject value) { PdfObject existingVal = items.get(key); if (existingVal != null) { if (value.getIndirectReference() != null && value.getIndirectReference().equals(existingVal.getIndirectReference())) { return; } else { Logger logger = LoggerFactory.getLogger(PdfNameTree.class); logger.warn(MessageFormatUtil.format(LogMessageConstant.NAME_ALREADY_EXISTS_IN_THE_NAME_TREE, key)); } } modified = true; items.put(key, value); }
private int getExplicitDestinationPageNum(PdfArray explicitDest) { PdfIndirectReference pageReference = (PdfIndirectReference) explicitDest.get(0, false); PdfDocument doc = pageReference.getDocument(); for (int i = 1; i <= doc.getNumberOfPages(); ++i) { if (doc.getPage(i).getPdfObject().getIndirectReference().equals(pageReference)) return i; } throw new IllegalArgumentException("PdfLinkAnnotation comparison: Page not found."); }
private boolean isEqualSameNameDestExist(Map<PdfPage, PdfPage> page2page, PdfDocument toDocument, String srcDestName, PdfArray srcDestArray, PdfPage oldPage) { PdfArray sameNameDest = (PdfArray) toDocument.getCatalog().getNameTree(PdfName.Dests).getNames().get(srcDestName); boolean equalSameNameDestExists = false; if (sameNameDest != null && sameNameDest.getAsDictionary(0) != null) { PdfIndirectReference existingDestPageRef = sameNameDest.getAsDictionary(0).getIndirectReference(); PdfIndirectReference newDestPageRef = page2page.get(oldPage).getPdfObject().getIndirectReference(); if (equalSameNameDestExists = existingDestPageRef.equals(newDestPageRef) && sameNameDest.size() == srcDestArray.size()) { for (int i = 1; i < sameNameDest.size(); ++i) { equalSameNameDestExists = equalSameNameDestExists && sameNameDest.get(i).equals(srcDestArray.get(i)); } } } return equalSameNameDestExists; }
/** * the next element in the entire hierarchy * * @param outline * */ private PdfOutline getAbsoluteTreeNextOutline(PdfOutline outline) { PdfObject nextPdfObject = outline.getContent().get(PdfName.Next); PdfOutline nextPdfOutline = null; if (outline.getParent() != null && nextPdfObject != null) { for (PdfOutline pdfOutline : outline.getParent().getAllChildren()) { if (pdfOutline.getContent().getIndirectReference().equals(nextPdfObject.getIndirectReference())) { nextPdfOutline = pdfOutline; break; } } } if (nextPdfOutline == null && outline.getParent() != null) { nextPdfOutline = getAbsoluteTreeNextOutline(outline.getParent()); } return nextPdfOutline; }
/** * Gets a {@link PdfPage} on which annotation is placed. * * @return {@link PdfPage} on which annotation is placed or null if annotation is not placed yet. */ public PdfPage getPage() { PdfIndirectReference annotationIndirectReference; if (page == null && (annotationIndirectReference = getPdfObject().getIndirectReference()) != null) { PdfDocument doc = annotationIndirectReference.getDocument(); PdfDictionary pageDictionary = getPageObject(); if (pageDictionary != null) { page = doc.getPage(pageDictionary); } else { for (int i = 1; i <= doc.getNumberOfPages(); i++) { PdfPage docPage = doc.getPage(i); if (!docPage.isFlushed()) { for (PdfAnnotation annot : docPage.getAnnotations()) { if (annotationIndirectReference.equals(annot.getPdfObject().getIndirectReference())) { page = docPage; break; } } } } } } return page; }
private void checkAndResolveCircularReferences(PdfObject pdfObject) { // Consider the situation when an XObject references the resources of the first page. // We add this XObject to the first page, there is no need to resolve any circular references // and then we flush this object and try to add it to the second page. // Now there are circular references and we cannot resolve them because the object is flushed // and we cannot get resources. // On the other hand, this situation may occur any time when object is already flushed and we // try to add it to resources and it seems difficult to overcome this without keeping /Resources key value. if (pdfObject instanceof PdfDictionary && !pdfObject.isFlushed()) { PdfDictionary pdfXObject = (PdfDictionary) pdfObject; PdfObject pdfXObjectResources = pdfXObject.get(PdfName.Resources); if (pdfXObjectResources != null && pdfXObjectResources.getIndirectReference() != null) { if (pdfXObjectResources.getIndirectReference().equals(getPdfObject().getIndirectReference())) { PdfObject cloneResources = getPdfObject().clone(); cloneResources.makeIndirect(getPdfObject().getIndirectReference().getDocument()); pdfXObject.put(PdfName.Resources, cloneResources.getIndirectReference()); } } } }