/** * Creates clones of the dictionary in the current document. * It's possible to pass a list of keys to exclude when cloning. * * @param excludeKeys list of objects to exclude when cloning dictionary. * @return cloned dictionary. */ public PdfDictionary clone(List<PdfName> excludeKeys) { Map<PdfName, PdfObject> excluded = new TreeMap<>(); for (PdfName key : excludeKeys) { PdfObject obj = map.get(key); if (obj != null) excluded.put(key, map.remove(key)); } PdfDictionary dictionary = (PdfDictionary) clone(); map.putAll(excluded); return dictionary; }
private static void cloneParents(PdfDictionary structElem, LastClonedAncestor lastCloned, PdfDocument document) { if (lastCloned.ancestor != structElem) { PdfDictionary structElemClone = (PdfDictionary) structElem.clone(ignoreKeysForClone).makeIndirect(document); PdfDictionary currClone = structElemClone; PdfDictionary currElem = structElem; while (currElem.get(PdfName.P) != lastCloned.ancestor) { PdfDictionary parent = currElem.getAsDictionary(PdfName.P); PdfDictionary parentClone = (PdfDictionary) parent.clone(ignoreKeysForClone).makeIndirect(document); currClone.put(PdfName.P, parentClone); parentClone.put(PdfName.K, currClone); currClone = parentClone; currElem = parent; } PdfStructElem.addKidObject(lastCloned.clone, -1, currClone); lastCloned.clone = structElemClone; lastCloned.ancestor = structElem; } }
/** * Creates an in-memory copy of a {@link PdfFormField}. This new field is * not added to the document. * * @param name the name of the {@link PdfFormField form field} to be copied * @return a clone of the original {@link PdfFormField} */ public PdfFormField copyField(String name) { PdfFormField oldField = getField(name); if (oldField != null) { PdfFormField field = new PdfFormField((PdfDictionary) oldField.getPdfObject().clone().makeIndirect(document)); return field; } return null; }
/** * Creates an in-memory copy of a {@link PdfFormField}. This new field is * not added to the document. * * @param name the name of the {@link PdfFormField form field} to be copied * @return a clone of the original {@link PdfFormField} */ public PdfFormField copyField(String name) { PdfFormField oldField = getField(name); if (oldField != null) { PdfFormField field = new PdfFormField((PdfDictionary) oldField.getPdfObject().clone().makeIndirect(document)); return field; } return null; }
protected void applyLinkAnnotation(PdfDocument document) { PdfLinkAnnotation linkAnnotation = this.<PdfLinkAnnotation>getProperty(Property.LINK_ANNOTATION); if (linkAnnotation != null) { int pageNumber = occupiedArea.getPageNumber(); if (pageNumber < 1 || pageNumber > document.getNumberOfPages()) { Logger logger = LoggerFactory.getLogger(AbstractRenderer.class); String logMessageArg = "Property.LINK_ANNOTATION, which specifies a link associated with this element content area, see com.itextpdf.layout.element.Link."; logger.warn(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_APPLY_PAGE_DEPENDENT_PROP_UNKNOWN_PAGE_ON_WHICH_ELEMENT_IS_DRAWN, logMessageArg)); return; } Rectangle pdfBBox = calculateAbsolutePdfBBox(); if (linkAnnotation.getPage() != null) { PdfDictionary oldAnnotation = (PdfDictionary) linkAnnotation.getPdfObject().clone(); linkAnnotation = (PdfLinkAnnotation) PdfAnnotation.makeAnnotation(oldAnnotation); } linkAnnotation.setRectangle(new PdfArray(pdfBBox)); PdfPage page = document.getPage(pageNumber); page.addAnnotation(linkAnnotation); } }
protected void applyLinkAnnotation(PdfDocument document) { PdfLinkAnnotation linkAnnotation = this.<PdfLinkAnnotation>getProperty(Property.LINK_ANNOTATION); if (linkAnnotation != null) { int pageNumber = occupiedArea.getPageNumber(); if (pageNumber < 1 || pageNumber > document.getNumberOfPages()) { Logger logger = LoggerFactory.getLogger(AbstractRenderer.class); String logMessageArg = "Property.LINK_ANNOTATION, which specifies a link associated with this element content area, see com.itextpdf.layout.element.Link."; logger.warn(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_APPLY_PAGE_DEPENDENT_PROP_UNKNOWN_PAGE_ON_WHICH_ELEMENT_IS_DRAWN, logMessageArg)); return; } Rectangle pdfBBox = calculateAbsolutePdfBBox(); if (linkAnnotation.getPage() != null) { PdfDictionary oldAnnotation = (PdfDictionary) linkAnnotation.getPdfObject().clone(); linkAnnotation = (PdfLinkAnnotation) PdfAnnotation.makeAnnotation(oldAnnotation); } linkAnnotation.setRectangle(new PdfArray(pdfBBox)); PdfPage page = document.getPage(pageNumber); page.addAnnotation(linkAnnotation); } }
/** * Creates form XObject from page content. * The page shall be from the document, to which FormXObject will be added. * * @param page an instance of {@link PdfPage} */ public PdfFormXObject(PdfPage page) { this(page.getCropBox()); getPdfObject().getOutputStream().writeBytes(page.getContentBytes()); resources = new PdfResources((PdfDictionary) page.getResources().getPdfObject().clone()); getPdfObject().put(PdfName.Resources, resources.getPdfObject()); }
protected void addResource(PdfObject resource, PdfName resType, PdfName resName) { if (resType.equals(PdfName.XObject)) { checkAndResolveCircularReferences(resource); } if (readOnly) { setPdfObject(getPdfObject().clone(Collections.<PdfName>emptyList())); buildResources(getPdfObject()); isModified = true; readOnly = false; } if (getPdfObject().containsKey(resType) && getPdfObject().getAsDictionary(resType).containsKey(resName)) return; resourceToName.put(resource, resName); PdfDictionary resourceCategory = getPdfObject().getAsDictionary(resType); if (resourceCategory == null) { getPdfObject().put(resType, resourceCategory = new PdfDictionary()); } resourceCategory.put(resName, resource); }
PdfDictionary topClone = top.clone(ignoreKeysForClone); topClone.put(PdfName.P, document.getStructTreeRoot().getPdfObject()); lastCloned.clone = topClone;
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()); } } } }
if (copyingParams.isCopyFromDestDocument()) { copied = source.clone(ignoreKeysForClone); if (source.isIndirect()) { copied.makeIndirect(copyingParams.getToDocument());