virtual public void CacheObject(PdfIndirectReference iref, PdfObject obj) { if (obj.Type == 0) { cachedObjects[new RefKey(iref)] = obj; } else if (obj is PdfDictionary) { cachedObjects[new RefKey(iref)] = CleverPdfDictionaryClone((PdfDictionary) obj); } else if (obj.IsArray()) { cachedObjects[new RefKey(iref)] = CleverPdfArrayClone((PdfArray) obj); } }
public virtual PdfArray GetKidsObject() { PdfArray k = null; PdfObject kObj = GetPdfObject().Get(PdfName.K); if (kObj != null && kObj.IsArray()) { k = (PdfArray)kObj; } if (k == null) { k = new PdfArray(); GetPdfObject().Put(PdfName.K, k); SetModified(); if (kObj != null) { k.Add(kObj); } } return(k); }
/// <summary>Highlights the options.</summary> /// <remarks> /// Highlights the options. Is this method is used for Combo box, the first value in input array /// will be the field value /// </remarks> /// <param name="optionNumbers">The option numbers</param> /// <returns> /// The edited /// <see cref="PdfChoiceFormField"/> /// </returns> public virtual iText.Forms.Fields.PdfChoiceFormField SetListSelected(int[] optionNumbers) { PdfArray indices = new PdfArray(); PdfArray values = new PdfArray(); PdfArray options = GetOptions(); foreach (int number in optionNumbers) { if (number >= 0 && number < options.Size()) { indices.Add(new PdfNumber(number)); PdfObject option = options.Get(number); if (option.IsString()) { values.Add(option); } else { if (option.IsArray()) { values.Add(((PdfArray)option).Get(0)); } } } } if (indices.Size() > 0) { SetIndices(indices); if (values.Size() == 1) { Put(PdfName.V, values.Get(0)); } else { Put(PdfName.V, values); } } RegenerateField(); return(this); }
/// <summary>Gets list of the direct kids of StructTreeRoot.</summary> /// <remarks> /// Gets list of the direct kids of StructTreeRoot. /// If certain kid is flushed, there will be a /// <see langword="null"/> /// in the list on it's place. /// </remarks> /// <returns>list of the direct kids of StructTreeRoot.</returns> public virtual IList <IStructureNode> GetKids() { PdfObject k = GetPdfObject().Get(PdfName.K); IList <IStructureNode> kids = new List <IStructureNode>(); if (k != null) { if (k.IsArray()) { PdfArray a = (PdfArray)k; for (int i = 0; i < a.Size(); i++) { IfKidIsStructElementAddToList(a.Get(i), kids); } } else { IfKidIsStructElementAddToList(k, kids); } } return(kids); }
private void CheckOpenAction(PdfObject openAction) { if (openAction == null) { return; } if (openAction.IsDictionary()) { CheckAction((PdfDictionary)openAction); } else { if (openAction.IsArray()) { PdfArray actions = (PdfArray)openAction; foreach (PdfObject action in actions) { CheckAction((PdfDictionary)action); } } } }
/// <summary>Gets list of the direct kids of structure element.</summary> /// <remarks> /// Gets list of the direct kids of structure element. /// If certain kid is flushed, there will be a /// <see langword="null"/> /// in the list on it's place. /// </remarks> /// <returns>list of the direct kids of structure element.</returns> public virtual IList <IStructureNode> GetKids() { PdfObject k = GetK(); IList <IStructureNode> kids = new List <IStructureNode>(); if (k != null) { if (k.IsArray()) { PdfArray a = (PdfArray)k; for (int i = 0; i < a.Size(); i++) { AddKidObjectToStructElemList(a.Get(i), kids); } } else { AddKidObjectToStructElemList(k, kids); } } return(kids); }
private IList <String> OptionsToUnicodeNames() { PdfArray options = GetOptions(); IList <String> optionsToUnicodeNames = new List <String>(options.Size()); for (int index = 0; index < options.Size(); index++) { PdfObject option = options.Get(index); PdfString value = null; if (option.IsString()) { value = (PdfString)option; } else { if (option.IsArray()) { value = (PdfString)((PdfArray)option).Get(1); } } optionsToUnicodeNames.Add(value != null ? value.ToUnicodeString() : null); } return(optionsToUnicodeNames); }
private static PdfDictionary CopyObject(PdfDictionary source, ICollection <PdfObject> objectsToCopy, PdfDocument toDocument, IDictionary <PdfDictionary, PdfDictionary> page2page, bool copyFromDestDocument) { PdfDictionary copied; if (copyFromDestDocument) { copied = source.Clone(ignoreKeysForCopy); if (source.IsIndirect()) { copied.MakeIndirect(toDocument); } } else { copied = source.CopyTo(toDocument, ignoreKeysForCopy, true); } if (source.ContainsKey(PdfName.Obj)) { PdfDictionary obj = source.GetAsDictionary(PdfName.Obj); if (!copyFromDestDocument && obj != null) { // Link annotations could be not added to the toDocument, so we need to identify this case. // When obj.copyTo is called, and annotation was already copied, we would get this already created copy. // If it was already copied and added, /P key would be set. Otherwise /P won't be set. obj = obj.CopyTo(toDocument, iText.IO.Util.JavaUtil.ArraysAsList(PdfName.P), false); copied.Put(PdfName.Obj, obj); } } PdfDictionary pg = source.GetAsDictionary(PdfName.Pg); if (pg != null) { //TODO It is possible, that pg will not be present in the page2page map. Consider the situation, // that we want to copy structElem because it has marked content dictionary reference, which belongs to the page from page2page, // but the structElem itself has /Pg which value could be arbitrary page. copied.Put(PdfName.Pg, page2page.Get(pg)); } PdfObject k = source.Get(PdfName.K); if (k != null) { if (k.IsArray()) { PdfArray kArr = (PdfArray)k; PdfArray newArr = new PdfArray(); for (int i = 0; i < kArr.Size(); i++) { PdfObject copiedKid = CopyObjectKid(kArr.Get(i), copied, objectsToCopy, toDocument, page2page, copyFromDestDocument ); if (copiedKid != null) { newArr.Add(copiedKid); } } // TODO new array may be empty or with single element copied.Put(PdfName.K, newArr); } else { PdfObject copiedKid = CopyObjectKid(k, copied, objectsToCopy, toDocument, page2page, copyFromDestDocument); if (copiedKid != null) { copied.Put(PdfName.K, copiedKid); } } } return(copied); }
private bool ObjectsIsEquals(PdfObject outObj, PdfObject cmpObj) { PdfObject outDirectObj = PdfReader.GetPdfObject(outObj); PdfObject cmpDirectObj = PdfReader.GetPdfObject(cmpObj); if (outDirectObj == null || cmpDirectObj.Type != outDirectObj.Type) { return(false); } if (cmpDirectObj.IsDictionary()) { PdfDictionary cmpDict = (PdfDictionary)cmpDirectObj; PdfDictionary outDict = (PdfDictionary)outDirectObj; if (cmpDict.IsPage()) { if (!outDict.IsPage()) { return(false); } RefKey cmpRefKey = new RefKey((PRIndirectReference)cmpObj); RefKey outRefKey = new RefKey((PRIndirectReference)outObj); if (cmpPagesRef.Contains(cmpRefKey) && cmpPagesRef.IndexOf(cmpRefKey) == outPagesRef.IndexOf(outRefKey)) { return(true); } return(false); } if (!ObjectsIsEquals(outDict, cmpDict)) { return(false); } } else if (cmpDirectObj.IsStream()) { if (!ObjectsIsEquals((PRStream)outDirectObj, (PRStream)cmpDirectObj)) { return(false); } } else if (cmpDirectObj.IsArray()) { if (!ObjectsIsEquals((PdfArray)outDirectObj, (PdfArray)cmpDirectObj)) { return(false); } } else if (cmpDirectObj.IsName()) { if (!ObjectsIsEquals((PdfName)outDirectObj, (PdfName)cmpDirectObj)) { return(false); } } else if (cmpDirectObj.IsNumber()) { if (!ObjectsIsEquals((PdfNumber)outDirectObj, (PdfNumber)cmpDirectObj)) { return(false); } } else if (cmpDirectObj.IsString()) { if (!ObjectsIsEquals((PdfString)outDirectObj, (PdfString)cmpDirectObj)) { return(false); } } else if (cmpDirectObj.IsBoolean()) { if (!ObjectsIsEquals((PdfBoolean)outDirectObj, (PdfBoolean)cmpDirectObj)) { return(false); } } else { throw new InvalidOperationException(); } return(true); }
virtual protected PdfArray GetDirectArray(PdfObject obj) { obj = GetDirectObject(obj); if (obj != null && obj.IsArray()) return (PdfArray) obj; return null; }
/** * Business logic that checks if a certain object is in conformance with PDF/X. * @param writer the writer that is supposed to write the PDF/X file * @param key the type of PDF/X conformance that has to be checked * @param obj1 the object that is checked for conformance */ public static void CheckPDFXConformance(PdfWriter writer, int key, Object obj1) { if (writer == null || !writer.IsPdfX()) { return; } int conf = writer.PDFXConformance; switch (key) { case PDFXKEY_COLOR: switch (conf) { case PdfWriter.PDFX1A2001: if (obj1 is ExtendedColor) { ExtendedColor ec = (ExtendedColor)obj1; switch (ec.Type) { case ExtendedColor.TYPE_CMYK: case ExtendedColor.TYPE_GRAY: return; case ExtendedColor.TYPE_RGB: throw new PdfXConformanceException("Colorspace RGB is not allowed."); case ExtendedColor.TYPE_SEPARATION: SpotColor sc = (SpotColor)ec; CheckPDFXConformance(writer, PDFXKEY_COLOR, sc.PdfSpotColor.AlternativeCS); break; case ExtendedColor.TYPE_SHADING: ShadingColor xc = (ShadingColor)ec; CheckPDFXConformance(writer, PDFXKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace); break; case ExtendedColor.TYPE_PATTERN: PatternColor pc = (PatternColor)ec; CheckPDFXConformance(writer, PDFXKEY_COLOR, pc.Painter.DefaultColor); break; } } else if (obj1 is Color) { throw new PdfXConformanceException("Colorspace RGB is not allowed."); } break; } break; case PDFXKEY_CMYK: break; case PDFXKEY_RGB: if (conf == PdfWriter.PDFX1A2001) { throw new PdfXConformanceException("Colorspace RGB is not allowed."); } break; case PDFXKEY_FONT: if (!((BaseFont)obj1).IsEmbedded()) { throw new PdfXConformanceException("All the fonts must be embedded. This one isn't: " + ((BaseFont)obj1).PostscriptFontName); } break; case PDFXKEY_IMAGE: PdfImage image = (PdfImage)obj1; if (image.Get(PdfName.SMASK) != null) { throw new PdfXConformanceException("The /SMask key is not allowed in images."); } switch (conf) { case PdfWriter.PDFX1A2001: PdfObject cs = image.Get(PdfName.COLORSPACE); if (cs == null) { return; } if (cs.IsName()) { if (PdfName.DEVICERGB.Equals(cs)) { throw new PdfXConformanceException("Colorspace RGB is not allowed."); } } else if (cs.IsArray()) { if (PdfName.CALRGB.Equals(((PdfArray)cs)[0])) { throw new PdfXConformanceException("Colorspace CalRGB is not allowed."); } } break; } break; case PDFXKEY_GSTATE: PdfDictionary gs = (PdfDictionary)obj1; PdfObject obj = gs.Get(PdfName.BM); if (obj != null && !PdfGState.BM_NORMAL.Equals(obj) && !PdfGState.BM_COMPATIBLE.Equals(obj)) { throw new PdfXConformanceException("Blend mode " + obj.ToString() + " not allowed."); } obj = gs.Get(PdfName.CA); double v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException("Transparency is not allowed: /CA = " + v); } obj = gs.Get(PdfName.ca_); v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException("Transparency is not allowed: /ca = " + v); } break; case PDFXKEY_LAYER: throw new PdfXConformanceException("Layers are not allowed."); } }
private static PdfDictionary CopyObject(PdfDictionary source, PdfDictionary destPage, bool parentChangePg, StructureTreeCopier.StructElemCopyingParams copyingParams) { PdfDictionary copied; if (copyingParams.IsCopyFromDestDocument()) { //TODO: detect wether object is needed to be cloned at all copied = source.Clone(ignoreKeysForClone); if (source.IsIndirect()) { copied.MakeIndirect(copyingParams.GetToDocument()); } PdfDictionary pg = source.GetAsDictionary(PdfName.Pg); if (pg != null) { if (copyingParams.IsCopyFromDestDocument()) { if (pg != destPage) { copied.Put(PdfName.Pg, destPage); parentChangePg = true; } else { parentChangePg = false; } } } } else { copied = source.CopyTo(copyingParams.GetToDocument(), ignoreKeysForCopy, true); PdfDictionary obj = source.GetAsDictionary(PdfName.Obj); if (obj != null) { // Link annotations could be not added to the toDocument, so we need to identify this case. // When obj.copyTo is called, and annotation was already copied, we would get this already created copy. // If it was already copied and added, /P key would be set. Otherwise /P won't be set. obj = obj.CopyTo(copyingParams.GetToDocument(), JavaUtil.ArraysAsList(PdfName.P), false); copied.Put(PdfName.Obj, obj); } PdfDictionary nsDict = source.GetAsDictionary(PdfName.NS); if (nsDict != null) { PdfDictionary copiedNsDict = CopyNamespaceDict(nsDict, copyingParams); copied.Put(PdfName.NS, copiedNsDict); } PdfDictionary pg = source.GetAsDictionary(PdfName.Pg); if (pg != null) { PdfDictionary pageAnalog = copyingParams.GetPage2page().Get(pg); if (pageAnalog == null) { pageAnalog = destPage; parentChangePg = true; } else { parentChangePg = false; } copied.Put(PdfName.Pg, pageAnalog); } } PdfObject k = source.Get(PdfName.K); if (k != null) { if (k.IsArray()) { PdfArray kArr = (PdfArray)k; PdfArray newArr = new PdfArray(); for (int i = 0; i < kArr.Size(); i++) { PdfObject copiedKid = CopyObjectKid(kArr.Get(i), copied, destPage, parentChangePg, copyingParams); if (copiedKid != null) { newArr.Add(copiedKid); } } if (!newArr.IsEmpty()) { if (newArr.Size() == 1) { copied.Put(PdfName.K, newArr.Get(0)); } else { copied.Put(PdfName.K, newArr); } } } else { PdfObject copiedKid = CopyObjectKid(k, copied, destPage, parentChangePg, copyingParams); if (copiedKid != null) { copied.Put(PdfName.K, copiedKid); } } } return(copied); }
private void SerObject(PdfObject obj, int level, ByteBuffer bb) { if (level <= 0) return; if (obj == null) { bb.Append("$Lnull"); return; } obj = PdfReader.GetPdfObject(obj); if (obj.IsStream()) { bb.Append("$B"); SerDic((PdfDictionary)obj, level - 1, bb); if (level > 0) { md5.Initialize(); bb.Append(md5.ComputeHash(PdfReader.GetStreamBytesRaw((PRStream)obj))); } } else if (obj.IsDictionary()) { SerDic((PdfDictionary)obj, level - 1, bb); } else if (obj.IsArray()) { SerArray((PdfArray)obj, level - 1, bb); } else if (obj.IsString()) { bb.Append("$S").Append(obj.ToString()); } else if (obj.IsName()) { bb.Append("$N").Append(obj.ToString()); } else bb.Append("$L").Append(obj.ToString()); }
public static string GetObjectInfo(PdfObject pdfObject) { StringBuilder sb = new StringBuilder(); if (pdfObject != null) { if (pdfObject.IsBoolean()) { sb.Append(", bool"); } if (pdfObject.IsNumber()) { sb.Append(", number"); } if (pdfObject.IsString()) { sb.Append(", string"); } if (pdfObject.IsLiteral()) { sb.Append(", literal"); } if (pdfObject.IsArray()) { sb.Append(", array"); } if (pdfObject.IsDictionary()) { sb.Append(", dictionary"); } if (pdfObject.IsName()) { sb.Append(", name"); } if (pdfObject.IsStream()) { sb.Append(", stream"); } if (pdfObject.IsIndirect()) { sb.Append(", indirect"); } if (pdfObject.IsIndirectReference()) { sb.Append(", indirect reference"); } if (pdfObject.IsModified()) { sb.Append(", modified"); } if (pdfObject.IsNull()) { sb.Append(", null"); } if (sb.Length > 0) { sb.Remove(0, 2); } } else { sb.Append("null"); } return(sb.ToString()); }
private static void SeparateKids(PdfDictionary structElem, ICollection <PdfObject> firstPartElems, StructureTreeCopier.LastClonedAncestor lastCloned) { PdfObject k = structElem.Get(PdfName.K); // If /K entry is not a PdfArray - it would be a kid which we won't clone at the moment, because it won't contain // kids from both parts at the same time. It would either be cloned as an ancestor later, or not cloned at all. // If it's kid is struct elem - it would definitely be structElem from the first part, so we simply call separateKids for it. if (!k.IsArray()) { if (k.IsDictionary() && PdfStructElem.IsStructElem((PdfDictionary)k)) { SeparateKids((PdfDictionary)k, firstPartElems, lastCloned); } } else { PdfDocument document = structElem.GetIndirectReference().GetDocument(); PdfArray kids = (PdfArray)k; for (int i = 0; i < kids.Size(); ++i) { PdfObject kid = kids.Get(i); PdfDictionary dictKid = null; if (kid.IsDictionary()) { dictKid = (PdfDictionary)kid; } if (dictKid != null && PdfStructElem.IsStructElem(dictKid)) { if (firstPartElems.Contains(kid)) { SeparateKids((PdfDictionary)kid, firstPartElems, lastCloned); } else { if (dictKid.IsFlushed()) { throw new PdfException(PdfException.TagFromTheExistingTagStructureIsFlushedCannotAddCopiedPageTags); } // elems with no kids will not be marked as from the first part, // but nonetheless we don't want to move all of them to the second part; we just leave them as is if (dictKid.ContainsKey(PdfName.K)) { CloneParents(structElem, lastCloned, document); kids.Remove(i--); PdfStructElem.AddKidObject(lastCloned.clone, -1, kid); } } } else { if (!firstPartElems.Contains(kid)) { CloneParents(structElem, lastCloned, document); PdfMcr mcr; if (dictKid != null) { if (dictKid.Get(PdfName.Type).Equals(PdfName.MCR)) { mcr = new PdfMcrDictionary(dictKid, new PdfStructElem(lastCloned.clone)); } else { mcr = new PdfObjRef(dictKid, new PdfStructElem(lastCloned.clone)); } } else { mcr = new PdfMcrNumber((PdfNumber)kid, new PdfStructElem(lastCloned.clone)); } kids.Remove(i--); PdfStructElem.AddKidObject(lastCloned.clone, -1, kid); document.GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr); } } } } // re-register mcr if (lastCloned.ancestor == structElem) { lastCloned.ancestor = lastCloned.ancestor.GetAsDictionary(PdfName.P); lastCloned.clone = lastCloned.clone.GetAsDictionary(PdfName.P); } }
/** * Business logic that checks if a certain object is in conformance with PDF/X. * @param writer the writer that is supposed to write the PDF/X file * @param key the type of PDF ISO conformance that has to be checked * @param obj1 the object that is checked for conformance */ public static void CheckPDFXConformance(PdfWriter writer, int key, Object obj1) { if (writer == null || !writer.IsPdfX()) { return; } int conf = writer.PDFXConformance; switch (key) { case PdfIsoKeys.PDFISOKEY_COLOR: switch (conf) { case PdfWriter.PDFX1A2001: if (obj1 is ExtendedColor) { ExtendedColor ec = (ExtendedColor)obj1; switch (ec.Type) { case ExtendedColor.TYPE_CMYK: case ExtendedColor.TYPE_GRAY: return; case ExtendedColor.TYPE_RGB: throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); case ExtendedColor.TYPE_SEPARATION: SpotColor sc = (SpotColor)ec; CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, sc.PdfSpotColor.AlternativeCS); break; case ExtendedColor.TYPE_SHADING: ShadingColor xc = (ShadingColor)ec; CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, xc.PdfShadingPattern.Shading.ColorSpace); break; case ExtendedColor.TYPE_PATTERN: PatternColor pc = (PatternColor)ec; CheckPDFXConformance(writer, PdfIsoKeys.PDFISOKEY_COLOR, pc.Painter.DefaultColor); break; } } else if (obj1 is BaseColor) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); } break; } break; case PdfIsoKeys.PDFISOKEY_CMYK: break; case PdfIsoKeys.PDFISOKEY_RGB: if (conf == PdfWriter.PDFX1A2001) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); } break; case PdfIsoKeys.PDFISOKEY_FONT: if (!((BaseFont)obj1).IsEmbedded()) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("all.the.fonts.must.be.embedded.this.one.isn.t.1", ((BaseFont)obj1).PostscriptFontName)); } break; case PdfIsoKeys.PDFISOKEY_IMAGE: PdfImage image = (PdfImage)obj1; if (image.Get(PdfName.SMASK) != null) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("the.smask.key.is.not.allowed.in.images")); } switch (conf) { case PdfWriter.PDFX1A2001: PdfObject cs = image.Get(PdfName.COLORSPACE); if (cs == null) { return; } if (cs.IsName()) { if (PdfName.DEVICERGB.Equals(cs)) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.rgb.is.not.allowed")); } } else if (cs.IsArray()) { if (PdfName.CALRGB.Equals(((PdfArray)cs)[0])) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("colorspace.calrgb.is.not.allowed")); } } break; } break; case PdfIsoKeys.PDFISOKEY_GSTATE: PdfDictionary gs = (PdfDictionary)obj1; PdfObject obj = gs.Get(PdfName.BM); if (obj != null && !PdfGState.BM_NORMAL.Equals(obj) && !PdfGState.BM_COMPATIBLE.Equals(obj)) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("blend.mode.1.not.allowed", obj.ToString())); } obj = gs.Get(PdfName.CA); double v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v)); } obj = gs.Get(PdfName.ca_); v = 0.0; if (obj != null && (v = ((PdfNumber)obj).DoubleValue) != 1.0) { throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("transparency.is.not.allowed.ca.eq.1", v)); } break; case PdfIsoKeys.PDFISOKEY_LAYER: throw new PdfXConformanceException(MessageLocalization.GetComposedMessage("layers.are.not.allowed")); } }
private static void MapGotoBookmark(Hashtable map, PdfObject dest, IntHashtable pages) { if (dest.IsString()) map["Named"] = dest.ToString(); else if (dest.IsName()) map["Named"] = PdfName.DecodeName(dest.ToString()); else if (dest.IsArray()) map["Page"] = MakeBookmarkParam((PdfArray)dest, pages); //changed by ujihara 2004-06-13 map["Action"] = "GoTo"; }