Ejemplo n.º 1
0
        public virtual void IndirectsChain4()
        {
            MemoryStream baos     = new MemoryStream();
            PdfWriter    writer   = new PdfWriter(baos);
            PdfDocument  document = new PdfDocument(writer);

            document.AddNewPage();
            PdfDictionary catalog    = document.GetCatalog().GetPdfObject();
            PdfDictionary dictionary = GetTestPdfDictionary();
            PdfObject     @object    = dictionary;

            for (int i = 0; i < 31; i++)
            {
                @object = @object.MakeIndirect(document).GetIndirectReference();
            }
            PdfArray array = new PdfArray();

            array.Add(@object);
            catalog.Put(new PdfName("a"), array);
            @object = ((PdfArray)catalog.Get(new PdfName("a"))).Get(0, true);
            NUnit.Framework.Assert.IsTrue(@object is PdfDictionary);
            NUnit.Framework.Assert.AreEqual(new PdfName("c").ToString(), ((PdfDictionary)@object).Get(new PdfName("b")
                                                                                                      ).ToString());
            document.Close();
        }
 private void MakeIndirectIfNeeded(PdfObject o)
 {
     if (o.CheckState(PdfObject.MUST_BE_INDIRECT))
     {
         o.MakeIndirect(pdfDoc);
     }
 }
Ejemplo n.º 3
0
        protected internal virtual PdfObject CopyObject(PdfObject obj, PdfDocument document, bool allowDuplicating
                                                        )
        {
            if (obj is PdfIndirectReference)
            {
                obj = ((PdfIndirectReference)obj).GetRefersTo();
            }
            if (obj == null)
            {
                obj = PdfNull.PDF_NULL;
            }
            if (CheckTypeOfPdfDictionary(obj, PdfName.Catalog))
            {
                ILogger logger = LoggerFactory.GetLogger(typeof(PdfReader));
                logger.Warn(LogMessageConstant.MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN);
                obj = PdfNull.PDF_NULL;
            }
            PdfIndirectReference indirectReference = obj.GetIndirectReference();
            PdfIndirectReference copiedIndirectReference;
            int copyObjectKey = 0;

            if (!allowDuplicating && indirectReference != null)
            {
                copyObjectKey           = GetCopyObjectKey(obj);
                copiedIndirectReference = copiedObjects.Get(copyObjectKey);
                if (copiedIndirectReference != null)
                {
                    return(copiedIndirectReference.GetRefersTo());
                }
            }
            if (properties.smartMode && !CheckTypeOfPdfDictionary(obj, PdfName.Page))
            {
                PdfObject copiedObject = SmartCopyObject(obj);
                if (copiedObject != null)
                {
                    return(copiedObjects.Get(GetCopyObjectKey(copiedObject)).GetRefersTo());
                }
            }
            PdfObject newObject = obj.NewInstance();

            if (indirectReference != null)
            {
                if (copyObjectKey == 0)
                {
                    copyObjectKey = GetCopyObjectKey(obj);
                }
                PdfIndirectReference reference = newObject.MakeIndirect(document).GetIndirectReference();
                copiedObjects[copyObjectKey] = reference;
            }
            newObject.CopyContent(obj, document);
            return(newObject);
        }
Ejemplo n.º 4
0
        public virtual void IndirectsChain2()
        {
            PdfDocument document = new PdfDocument(new PdfWriter(new MemoryStream()));

            document.AddNewPage();
            PdfDictionary catalog    = document.GetCatalog().GetPdfObject();
            PdfDictionary dictionary = GetTestPdfDictionary();
            PdfObject     @object    = dictionary;

            for (int i = 0; i < 200; i++)
            {
                @object = @object.MakeIndirect(document).GetIndirectReference();
            }
            catalog.Put(new PdfName("a"), @object);
            ((PdfIndirectReference)catalog.Get(new PdfName("a"))).GetRefersTo(true);
            NUnit.Framework.Assert.IsNotNull(((PdfIndirectReference)catalog.Get(new PdfName("a"))).GetRefersTo(true));
            document.Close();
        }
Ejemplo n.º 5
0
 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 is 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());
             }
         }
     }
 }
Ejemplo n.º 6
0
        // For internal usage only
        /// <summary>Write a PdfObject to the outputstream.</summary>
        /// <param name="pdfObject">PdfObject to write</param>
        /// <returns>this PdfOutPutStream</returns>
        public virtual iText.Kernel.Pdf.PdfOutputStream Write(PdfObject pdfObject)
        {
            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.GetObjectType())
            {
            case PdfObject.ARRAY: {
                Write((PdfArray)pdfObject);
                break;
            }

            case PdfObject.DICTIONARY: {
                Write((PdfDictionary)pdfObject);
                break;
            }

            case PdfObject.INDIRECT_REFERENCE: {
                Write((PdfIndirectReference)pdfObject);
                break;
            }

            case PdfObject.NAME: {
                Write((PdfName)pdfObject);
                break;
            }

            case PdfObject.NULL:
            case PdfObject.BOOLEAN: {
                Write((PdfPrimitiveObject)pdfObject);
                break;
            }

            case PdfObject.LITERAL: {
                Write((PdfLiteral)pdfObject);
                break;
            }

            case PdfObject.STRING: {
                Write((PdfString)pdfObject);
                break;
            }

            case PdfObject.NUMBER: {
                Write((PdfNumber)pdfObject);
                break;
            }

            case PdfObject.STREAM: {
                Write((PdfStream)pdfObject);
                break;
            }
            }
            return(this);
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        protected internal virtual PdfObject CopyObject(PdfObject obj, PdfDocument document, bool allowDuplicating
                                                        )
        {
            if (obj is PdfIndirectReference)
            {
                obj = ((PdfIndirectReference)obj).GetRefersTo();
            }
            if (obj == null)
            {
                obj = PdfNull.PDF_NULL;
            }
            if (CheckTypeOfPdfDictionary(obj, PdfName.Catalog))
            {
                ILogger logger = LoggerFactory.GetLogger(typeof(PdfReader));
                logger.Warn(LogMessageConstant.MAKE_COPY_OF_CATALOG_DICTIONARY_IS_FORBIDDEN);
                obj = PdfNull.PDF_NULL;
            }
            PdfIndirectReference indirectReference = obj.GetIndirectReference();
            int  copyObjectKey      = 0;
            bool tryToFindDuplicate = !allowDuplicating && indirectReference != null;

            if (tryToFindDuplicate)
            {
                copyObjectKey = CalculateIndRefKey(indirectReference);
                PdfIndirectReference copiedIndirectReference = copiedObjects.Get(copyObjectKey);
                if (copiedIndirectReference != null)
                {
                    return(copiedIndirectReference.GetRefersTo());
                }
            }
            if (obj.IsDictionary())
            {
                PdfName subtype = ((PdfDictionary)obj).GetAsName(PdfName.Subtype);
                if (subtype != null && subtype.Equals(PdfName.Widget))
                {
                    tryToFindDuplicate = false;
                }
            }
            if (properties.smartMode && tryToFindDuplicate && !CheckTypeOfPdfDictionary(obj, PdfName.Page))
            {
                PdfIndirectReference copiedObjectRef = TryToFindPreviouslyCopiedEqualObject(obj);
                if (copiedObjectRef != null)
                {
                    PdfIndirectReference copiedIndirectReference = copiedObjects.Get(CalculateIndRefKey(copiedObjectRef));
                    copiedObjects[copyObjectKey] = copiedIndirectReference;
                    return(copiedIndirectReference.GetRefersTo());
                }
            }
            PdfObject newObject = obj.NewInstance();

            if (indirectReference != null)
            {
                if (copyObjectKey == 0)
                {
                    copyObjectKey = CalculateIndRefKey(indirectReference);
                }
                PdfIndirectReference indRef = newObject.MakeIndirect(document).GetIndirectReference();
                copiedObjects[copyObjectKey] = indRef;
            }
            newObject.CopyContent(obj, document);
            return(newObject);
        }