Exemple #1
0
        public virtual void SmartModeObjectSelfReferencingTest()
        {
            PdfDocument   document = new PdfDocument(new PdfWriter(new MemoryStream()));
            PdfDictionary dict1    = new PdfDictionary();

            dict1.MakeIndirect(document);
            PdfDictionary dict2 = new PdfDictionary();

            dict2.MakeIndirect(document);
            PdfArray array = new PdfArray();

            array.MakeIndirect(document);
            array.Add(new PdfString(new byte[10000]));
            array.Add(new PdfDictionary(dict2));
            dict1.Put(new PdfName("FirstDict"), array.GetIndirectReference());
            dict2.Put(new PdfName("SecondDict"), dict1.GetIndirectReference());
            SmartModePdfObjectsSerializer serializer       = new SmartModePdfObjectsSerializer();
            SerializedObjectContent       serializedObject = serializer.SerializeObject(dict1);
            //It is essential to serialize object with huge amount of memory
            StringBuilder stringBytes = new StringBuilder().Append("$D$N/FirstDict$A$S");
            String        end         = "$D$\\D$\\A$\\D";

            for (int i = 0; i < 10000; i++)
            {
                stringBytes.Append("\x0");
            }
            stringBytes.Append(end);
            SerializedObjectContent expected = new SerializedObjectContent(stringBytes.ToString().GetBytes(System.Text.Encoding
                                                                                                           .UTF8));

            NUnit.Framework.Assert.AreEqual(expected, serializedObject);
        }
Exemple #2
0
        protected internal virtual PdfObject CopyObject(PdfObject obj, PdfDocument documentTo, bool allowDuplicating
                                                        )
        {
            if (obj is PdfIndirectReference)
            {
                obj = ((PdfIndirectReference)obj).GetRefersTo();
            }
            if (obj == null)
            {
                obj = PdfNull.PDF_NULL;
            }
            if (CheckTypeOfPdfDictionary(obj, PdfName.Catalog))
            {
                ILog logger = LogManager.GetLogger(typeof(PdfReader));
                logger.Warn(iText.IO.LogMessageConstant.MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN);
                obj = PdfNull.PDF_NULL;
            }
            PdfIndirectReference indirectReference = obj.GetIndirectReference();

            PdfDocument.IndirectRefDescription copiedObjectKey = null;
            bool tryToFindDuplicate = !allowDuplicating && indirectReference != null;

            if (tryToFindDuplicate)
            {
                copiedObjectKey = new PdfDocument.IndirectRefDescription(indirectReference);
                PdfIndirectReference copiedIndirectReference = copiedObjects.Get(copiedObjectKey);
                if (copiedIndirectReference != null)
                {
                    return(copiedIndirectReference.GetRefersTo());
                }
            }
            SerializedObjectContent serializedContent = null;

            if (properties.smartMode && tryToFindDuplicate && !CheckTypeOfPdfDictionary(obj, PdfName.Page))
            {
                serializedContent = smartModeSerializer.SerializeObject(obj);
                PdfIndirectReference objectRef = smartModeSerializer.GetSavedSerializedObject(serializedContent);
                if (objectRef != null)
                {
                    copiedObjects.Put(copiedObjectKey, objectRef);
                    return(objectRef.refersTo);
                }
            }
            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);
                }
                copiedObjects.Put(copiedObjectKey, indRef);
            }
            newObject.CopyContent(obj, documentTo);
            return(newObject);
        }