private void SerArray(PdfArray array, int level, ByteBufferOutputStream bb) { bb.Append("$A"); if (level <= 0) { return; } for (int k = 0; k < array.Size(); ++k) { SerObject(array.Get(k, false), level, bb); } }
private void SerDic(PdfDictionary dic, int level, ByteBufferOutputStream bb) { bb.Append("$D"); if (level <= 0) { return; } PdfName[] keys = new PdfName[dic.KeySet().Count]; keys = dic.KeySet().ToArray(keys); iText.IO.Util.JavaUtil.Sort(keys); foreach (Object key in keys) { if (key.Equals(PdfName.P) && (dic.Get((PdfName)key).IsIndirectReference() || dic.Get((PdfName)key).IsDictionary ()) || key.Equals(PdfName.Parent)) { // ignore recursive call continue; } SerObject((PdfObject)key, level, bb); SerObject(dic.Get((PdfName)key, false), level, bb); } }
// 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); } }