/** * Checks if this <CODE>PdfObject</CODE> is of the type * <CODE>PdfDictionary</CODE>. * * @return <CODE>true</CODE> or <CODE>false</CODE> */ public boolean isDictionary() { return getType() == DICTIONARY; }
/** * This methods adds new name in the Dests NameTree. It throws an exception, if the name already exists. * * @param key Name of the destination. * @param value An object destination refers to. Must be an array or a dictionary with key /D and array. * See ISO 32000-1 12.3.2.3 for more info. */ public void addNamedDestination(String key, PdfObject value) { checkClosingStatus(); if (value.isArray() && ((PdfArray)value).get(0).isNumber()) LoggerFactory.getLogger(PdfDocument.class).warn(LogMessageConstant.INVALID_DESTINATION_TYPE); catalog.addNamedDestination(key, value); }
/** * Tells you if the node contains a dictionary. * * @return true if the object is a PdfDictionary */ public boolean isDictionary() { return object.isDictionary(); }
protected static void markObjectAsIndirect(PdfObject pdfObject) { if (pdfObject.getIndirectReference() == null) { pdfObject.setState(PdfObject.MUST_BE_INDIRECT); } }
private PdfArray getDestArray(PdfObject obj) { if (obj == null) return null; if (obj.isArray()) return (PdfArray) obj; else if (obj.isDictionary()) { PdfArray arr = ((PdfDictionary) obj).getAsArray(PdfName.D); return arr; } return null; } }
private void flushMustBeIndirectObject(PdfObject obj) { // TODO DEVSIX-744 obj.makeIndirect(getDocument()).flush(); }
PdfObject cmpDirectObj = null; if (outObj != null) outDirectObj = outObj.isIndirectReference() ? ((PdfIndirectReference) outObj).getRefersTo(false) : outObj; if (cmpObj != null) cmpDirectObj = cmpObj.isIndirectReference() ? ((PdfIndirectReference) cmpObj).getRefersTo(false) : cmpObj; compareResult.addError(currentPath, "Found object which was not expected to be found."); return false; } else if (cmpDirectObj.getType() != outDirectObj.getType()) { compareResult.addError(currentPath, MessageFormatUtil.format("Types do not match. Expected: {0}. Found: {1}.", cmpDirectObj.getClass().getSimpleName(), outDirectObj.getClass().getSimpleName())); return false; } else if (cmpObj.isIndirectReference() && !outObj.isIndirectReference()) { compareResult.addError(currentPath, "Expected indirect object."); return false; } else if (!cmpObj.isIndirectReference() && outObj.isIndirectReference()) { compareResult.addError(currentPath, "Expected direct object."); return false; if (currentPath != null && cmpObj.isIndirectReference() && outObj.isIndirectReference()) { if (currentPath.isComparing((PdfIndirectReference) cmpObj, (PdfIndirectReference) outObj)) return true; if (cmpDirectObj.isDictionary() && PdfName.Page.equals(((PdfDictionary) cmpDirectObj).getAsName(PdfName.Type)) && useCachedPagesForComparison) { if (!outDirectObj.isDictionary() || !PdfName.Page.equals(((PdfDictionary) outDirectObj).getAsName(PdfName.Type))) { if (compareResult != null && currentPath != null) compareResult.addError(currentPath, "Expected a page. Found not a page."); return false;
ByteBuffer savedBb = null; if (obj.isIndirectReference()) { reference = (PdfIndirectReference) obj; byte[] cached = serializedCache.get(reference); if (obj.isStream()) { serDic((PdfDictionary) obj, bb, level - 1, serializedCache); bb.append("$B"); bb.append(md5.digest(((PdfStream) obj).getBytes(false))); } else if (obj.isDictionary()) { serDic((PdfDictionary) obj, bb, level - 1, serializedCache); } else if (obj.isArray()) { serArray((PdfArray) obj, bb, level - 1, serializedCache); } else if (obj.isString()) { bb.append("$S").append(obj.toString()); // TODO specify length for strings, streams, may be names? } else if (obj.isName()) { bb.append("$N").append(obj.toString()); } else {
private void checkSeparationCS(PdfArray separation) { if (separationColorSpaces.containsKey(separation.getAsName(0))) { boolean altCSIsTheSame; boolean tintTransformIsTheSame; PdfArray sameNameSeparation = separationColorSpaces.get(separation.getAsName(0)); PdfObject cs1 = separation.get(2); PdfObject cs2 = sameNameSeparation.get(2); altCSIsTheSame = isAltCSIsTheSame(cs1, cs2); // TODO(DEVSIX-1672) in fact need to check if objects content is equal. ISO 19005-2, 6.2.4.4 "Separation and DeviceN colour spaces": // In evaluating equivalence, the PDF objects shall be compared, rather than the computational // result of the use of those PDF objects. Compression and whether or not an object is direct or indirect shall be ignored. PdfObject f1Obj = separation.get(3); PdfObject f2Obj = sameNameSeparation.get(3); //Can be a stream or dict boolean bothAllowedType = (f1Obj.getType() == f2Obj.getType()) && (f1Obj.isDictionary() || f1Obj.isStream()); //Check if the indirect references are equal tintTransformIsTheSame = bothAllowedType && f1Obj.equals(f2Obj); if (!altCSIsTheSame || !tintTransformIsTheSame) { throw new PdfAConformanceException(PdfAConformanceException.TINT_TRANSFORM_AND_ALTERNATE_SPACE_SHALL_BE_THE_SAME_FOR_THE_ALL_SEPARATION_CS_WITH_THE_SAME_NAME); } } else { separationColorSpaces.put(separation.getAsName(0), separation); } }
if (ap != null && !ap.isFlushed()) { PdfObject normal = ap.get(PdfName.N); if (normal != null && !normal.isFlushed()) { if (normal.isDictionary()) { for (PdfName key : ((PdfDictionary) normal).keySet()) { PdfStream appearance = ((PdfDictionary) normal).getAsStream(key); } else if (normal.isStream()) { PdfDictionary resDict = ((PdfStream) normal).getAsDictionary(PdfName.Resources); if (resDict != null) {
for (int i = 1; i <= document.getNumberOfPages(); i++) { PdfObject resources = document.getPage(i).getPdfObject().getAsDictionary(PdfName.Resources); initialPageResourceClones.put(i, resources == null ? null : resources.clone()); PdfObject normal = appDic.get(PdfName.N); PdfFormXObject xObject = null; if (normal.isStream()) { xObject = new PdfFormXObject((PdfStream) normal); } else if (normal.isDictionary()) { PdfName as = fieldObject.getAsName(PdfName.AS); if (((PdfDictionary) normal).getAsStream(as) != null) {
public static PdfDestination makeDestination(PdfObject pdfObject) { if (pdfObject.getType() == PdfObject.STRING) { return new PdfStringDestination((PdfString) pdfObject); } else if (pdfObject.getType() == PdfObject.NAME) { return new PdfNamedDestination((PdfName) pdfObject); } else if (pdfObject.getType() == PdfObject.ARRAY) { PdfArray destArray = (PdfArray) pdfObject; if (destArray.size() == 0) { throw new IllegalArgumentException(); } else { PdfObject firstObj = destArray.get(0); // In case of explicit destination for remote go-to action this is a page number if (firstObj.isNumber()) { return new PdfExplicitRemoteGoToDestination(destArray); } // In case of explicit destination for not remote go-to action this is a page dictionary if (firstObj.isDictionary() && PdfName.Page.equals(((PdfDictionary) firstObj).getAsName(PdfName.Type))) { return new PdfExplicitDestination(destArray); } // In case of structure destination this is a struct element dictionary or a string ID. Type is not required for structure elements return new PdfStructureDestination(destArray); } } else { throw new UnsupportedOperationException(); } } }
public SerializedObjectContent serializeObject(PdfObject obj) { if (!obj.isStream() && !obj.isDictionary()) { return null; } PdfIndirectReference indRef = obj.getIndirectReference(); assert indRef != null; Map<PdfIndirectReference, byte[]> serializedCache = indRef.getDocument().serializedObjectsCache; byte[] content = serializedCache.get(indRef); if (content == null) { ByteBuffer bb = new ByteBuffer(); int level = 100; try { serObject(obj, bb, level, serializedCache); } catch (SelfReferenceException e) { return null; } content = bb.toByteArray(); } return new SerializedObjectContent(content); }
if (pdfObject.checkState(PdfObject.MUST_BE_INDIRECT) && document != null) { pdfObject.makeIndirect(document); pdfObject = pdfObject.getIndirectReference(); if (pdfObject.checkState(PdfObject.READ_ONLY)) { throw new PdfException(PdfException.CannotWriteObjectAfterItWasReleased); switch (pdfObject.getType()) { case PdfObject.ARRAY: write((PdfArray) pdfObject);
public static void validateNotRemoteDestination(PdfDestination destination) { if (destination instanceof PdfExplicitRemoteGoToDestination) { LoggerFactory.getLogger(PdfAction.class).warn(LogMessageConstant.INVALID_DESTINATION_TYPE); } else if (destination instanceof PdfExplicitDestination) { // No page number can be specified for a destination associated with a not remote go-to action because the // destination page is in a current PDF document. See section 12.3.2.2 of ISO 32000-1. PdfObject firstObj = ((PdfArray)destination.getPdfObject()).get(0); if (firstObj.isNumber()) { LoggerFactory.getLogger(PdfAction.class).warn(LogMessageConstant.INVALID_DESTINATION_TYPE); } } } }
if (n == null || !n.isDictionary()) throw new PdfAConformanceException(PdfAConformanceException.APPEARANCE_DICTIONARY_OF_WIDGET_SUBTYPE_AND_BTN_FIELD_TYPE_SHALL_CONTAIN_ONLY_THE_N_KEY_WITH_DICTIONARY_VALUE); } else { if (n == null || !n.isStream()) throw new PdfAConformanceException(PdfAConformanceException.APPEARANCE_DICTIONARY_SHALL_CONTAIN_ONLY_THE_N_KEY_WITH_STREAM_VALUE);
/** * Mark an object with {@link PdfObject#MUST_BE_FLUSHED}. * * @param pdfObject an object to mark. */ protected void markObjectAsMustBeFlushed(PdfObject pdfObject) { if (pdfObject.getIndirectReference() != null) { pdfObject.getIndirectReference().setState(PdfObject.MUST_BE_FLUSHED); } }
/** * Tells you if the node contains an array. * * @return true if the object is a PdfArray */ public boolean isArray() { return object.isArray(); }
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()); } } } }
PdfIndirectReference indirectReference = obj.getIndirectReference(); PdfObject newObject = obj.newInstance(); if (indirectReference != null) { if (copiedObjectKey == null) { copiedObjectKey = new PdfDocument.IndirectRefDescription(indirectReference); PdfIndirectReference indRef = newObject.makeIndirect(documentTo).getIndirectReference(); if (serializedContent != null) { smartModeSerializer.saveSerializedObject(serializedContent, indRef); newObject.copyContent(obj, documentTo);