/// <summary>Used in the smart mode.</summary> /// <remarks> /// Used in the smart mode. /// It serializes given object content and tries to find previously copied object with the same content. /// If already copied object is not found, it saves current object serialized content into the map. /// </remarks> /// <param name="object">an object to check if some other object with the same content was already copied.</param> /// <returns>indirect reference of the object with the same content, which already has a copy in the new document. /// </returns> private PdfIndirectReference TryToFindPreviouslyCopiedEqualObject(PdfObject @object) { PdfWriter.SerializedPdfObject objectKey; if (@object.IsStream() || @object.IsDictionary()) { objectKey = new PdfWriter.SerializedPdfObject(@object, objectRefToSerializedContent); PdfIndirectReference objectRef = serializedContentToObjectRef.Get(objectKey); if (objectRef != null) { return(objectRef); } serializedContentToObjectRef[objectKey] = @object.GetIndirectReference(); } return(null); }
internal SerializedPdfObject(PdfObject obj, Dictionary <int, byte[]> objToSerializedContent) { System.Diagnostics.Debug.Assert(obj.IsDictionary() || obj.IsStream()); this.objToSerializedContent = objToSerializedContent; try { md5 = Org.BouncyCastle.Security.DigestUtilities.GetDigest("MD5"); } catch (Exception e) { throw new PdfException(e); } ByteBufferOutputStream bb = new ByteBufferOutputStream(); int level = 100; SerObject(obj, level, bb); this.serializedContent = bb.ToByteArray(); hash = CalculateHash(this.serializedContent); md5 = null; }
public virtual SerializedObjectContent SerializeObject(PdfObject obj) { if (!obj.IsStream() && !obj.IsDictionary()) { return(null); } PdfIndirectReference indRef = obj.GetIndirectReference(); System.Diagnostics.Debug.Assert(indRef != null); IDictionary <PdfIndirectReference, byte[]> serializedCache = indRef.GetDocument().serializedObjectsCache; byte[] content = serializedCache.Get(indRef); if (content == null) { ByteBuffer bb = new ByteBuffer(); int level = 100; SerObject(obj, bb, level, serializedCache); content = bb.ToByteArray(); } return(new SerializedObjectContent(content)); }
private void FlushObjectRecursively(PdfObject obj, PageFlushingHelper.DeepFlushingContext context) { if (obj == null) { return; } bool avoidReleaseForIndirectObjInstance = false; if (obj.IsIndirectReference()) { PdfIndirectReference indRef = (PdfIndirectReference)obj; if (indRef.refersTo == null || indRef.CheckState(PdfObject.FLUSHED)) { return; } obj = indRef.GetRefersTo(); } else { if (obj.IsFlushed()) { return; } else { if (release && obj.IsIndirect()) { // We should avoid the case when object is going to be released but is stored in containing object // not as indirect reference. This can happen when containing object is somehow modified. // Generally containing objects should not contain released read-only object instance. System.Diagnostics.Debug.Assert(obj.IsReleaseForbidden() || obj.GetIndirectReference() == null); avoidReleaseForIndirectObjInstance = true; } } } if (pdfDoc.IsDocumentFont(obj.GetIndirectReference()) || layersRefs.Contains(obj.GetIndirectReference())) { return; } if (obj.IsDictionary() || obj.IsStream()) { if (!currNestedObjParents.Add(obj)) { return; } FlushDictRecursively((PdfDictionary)obj, context); currNestedObjParents.Remove(obj); } else { if (obj.IsArray()) { if (!currNestedObjParents.Add(obj)) { return; } PdfArray array = (PdfArray)obj; for (int i = 0; i < array.Size(); ++i) { FlushObjectRecursively(array.Get(i, false), context); } currNestedObjParents.Remove(obj); } } if (!avoidReleaseForIndirectObjInstance) { FlushOrRelease(obj); } }
private void SerObject(PdfObject obj, ByteBuffer bb, int level, IDictionary <PdfIndirectReference, byte[]> serializedCache) { if (level <= 0) { return; } if (obj == null) { bb.Append("$Lnull"); return; } PdfIndirectReference reference = null; ByteBuffer savedBb = null; if (obj.IsIndirectReference()) { reference = (PdfIndirectReference)obj; byte[] cached = serializedCache.Get(reference); if (cached != null) { bb.Append(cached); return; } else { savedBb = bb; bb = new ByteBuffer(); obj = reference.GetRefersTo(); } } if (obj.IsStream()) { SerDic((PdfDictionary)obj, bb, level - 1, serializedCache); bb.Append("$B"); if (level > 0) { 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()); } else { // TODO specify length for strings, streams, may be names? if (obj.IsName()) { bb.Append("$N").Append(obj.ToString()); } else { bb.Append("$L").Append(obj.ToString()); } } } } } // PdfNull case is also here if (savedBb != null) { serializedCache.Put(reference, bb.ToByteArray()); savedBb.Append(bb.GetInternalBuffer()); } }
// TODO 2: object is not checked if it was already serialized on start, double work could be done // TODO 3: indirect objects often stored multiple times as parts of the other objects private void SerObject(PdfObject obj, int level, ByteBufferOutputStream bb) { if (level <= 0) { return; } if (obj == null) { bb.Append("$Lnull"); return; } PdfIndirectReference reference = null; ByteBufferOutputStream savedBb = null; int indRefKey = -1; if (obj.IsIndirectReference()) { reference = (PdfIndirectReference)obj; indRefKey = CalculateIndRefKey(reference); byte[] cached = objToSerializedContent.Get(indRefKey); if (cached != null) { bb.Append(cached); return; } else { savedBb = bb; bb = new ByteBufferOutputStream(); obj = reference.GetRefersTo(); } } if (obj.IsStream()) { bb.Append("$B"); SerDic((PdfDictionary)obj, level - 1, bb); if (level > 0) { md5.Reset(); bb.Append(md5.Digest(((PdfStream)obj).GetBytes(false))); } } 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()); } } } } } // PdfNull case is also here if (savedBb != null) { objToSerializedContent[indRefKey] = bb.GetBuffer(); savedBb.Append(bb); } }
private void SerObject(PdfObject obj, int level, ByteBufferOutputStream bb, IntHashtable serialized) { if (level <= 0) { return; } if (obj == null) { bb.Append("$Lnull"); return; } PdfIndirectReference reference = null; ByteBufferOutputStream savedBb = null; if (obj.IsIndirectReference()) { reference = (PdfIndirectReference)obj; int key = GetCopyObjectKey(obj); if (serialized.ContainsKey(key)) { bb.Append((int)serialized.Get(key)); return; } else { savedBb = bb; bb = new ByteBufferOutputStream(); } } if (obj.IsStream()) { bb.Append("$B"); SerDic((PdfDictionary)obj, level - 1, bb, serialized); if (level > 0) { md5.Reset(); bb.Append(md5.Digest(((PdfStream)obj).GetBytes(false))); } } else { if (obj.IsDictionary()) { SerDic((PdfDictionary)obj, level - 1, bb, serialized); } else { if (obj.IsArray()) { SerArray((PdfArray)obj, level - 1, bb, serialized); } 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()); } } } } } if (savedBb != null) { int key = GetCopyObjectKey(reference); if (!serialized.ContainsKey(key)) { serialized.Put(key, CalculateHash(bb.GetBuffer())); } savedBb.Append(bb); } }