Beispiel #1
0
 private void Write(PdfIndirectReference indirectReference)
 {
     if (document != null && !indirectReference.GetDocument().Equals(document))
     {
         throw new PdfException(PdfException.PdfIndirectObjectBelongsToOtherPdfDocument);
     }
     if (indirectReference.IsFree())
     {
         ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.PdfOutputStream));
         logger.Error(iText.IO.LogMessageConstant.FLUSHED_OBJECT_CONTAINS_FREE_REFERENCE);
         Write(PdfNull.PDF_NULL);
     }
     else
     {
         if (indirectReference.GetRefersTo() == null)
         {
             ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.PdfOutputStream));
             logger.Error(iText.IO.LogMessageConstant.FLUSHED_OBJECT_CONTAINS_REFERENCE_WHICH_NOT_REFER_TO_ANY_OBJECT);
             Write(PdfNull.PDF_NULL);
         }
         else
         {
             if (indirectReference.GetGenNumber() == 0)
             {
                 WriteInteger(indirectReference.GetObjNumber()).WriteBytes(endIndirectWithZeroGenNr);
             }
             else
             {
                 WriteInteger(indirectReference.GetObjNumber()).WriteSpace().WriteInteger(indirectReference.GetGenNumber())
                 .WriteBytes(endIndirect);
             }
         }
     }
 }
Beispiel #2
0
        /// <summary>Flushes all modified objects which have not been flushed yet.</summary>
        /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks>
        /// <param name="forbiddenToFlush">
        /// a
        /// <see cref="Java.Util.Set{E}"/>
        /// of
        /// <see cref="PdfIndirectReference">references</see>
        /// that are forbidden to be flushed automatically.
        /// </param>
        protected internal virtual void FlushModifiedWaitingObjects(ICollection <PdfIndirectReference> forbiddenToFlush
                                                                    )
        {
            PdfXrefTable xref = document.GetXref();

            for (int i = 1; i < xref.Size(); i++)
            {
                PdfIndirectReference indirectReference = xref.Get(i);
                if (null != indirectReference && !indirectReference.IsFree() && !forbiddenToFlush.Contains(indirectReference
                                                                                                           ))
                {
                    bool isModified = indirectReference.CheckState(PdfObject.MODIFIED);
                    if (isModified)
                    {
                        PdfObject obj = indirectReference.GetRefersTo(false);
                        if (obj != null)
                        {
                            if (!obj.Equals(objectStream))
                            {
                                obj.Flush();
                            }
                        }
                    }
                }
            }
            if (objectStream != null && objectStream.GetSize() > 0)
            {
                objectStream.Flush();
                objectStream = null;
            }
        }
Beispiel #3
0
 private PdfObject SmartCopyObject(PdfObject obj)
 {
     PdfWriter.ByteStore streamKey;
     if (obj.IsStream())
     {
         streamKey = new PdfWriter.ByteStore((PdfStream)obj, serialized);
         PdfIndirectReference streamRef = streamMap.Get(streamKey);
         if (streamRef != null)
         {
             return(streamRef);
         }
         streamMap[streamKey] = obj.GetIndirectReference();
     }
     else
     {
         if (obj.IsDictionary())
         {
             streamKey = new PdfWriter.ByteStore((PdfDictionary)obj, serialized);
             PdfIndirectReference streamRef = streamMap.Get(streamKey);
             if (streamRef != null)
             {
                 return(streamRef.GetRefersTo());
             }
             streamMap[streamKey] = obj.GetIndirectReference();
         }
     }
     return(null);
 }
Beispiel #4
0
        /// <summary>Flushes all objects which have not been flushed yet.</summary>
        /// <param name="forbiddenToFlush">
        /// a
        /// <see cref="Java.Util.Set{E}"/>
        /// of
        /// <see cref="PdfIndirectReference">references</see>
        /// that are forbidden to be flushed automatically.
        /// </param>
        protected internal virtual void FlushWaitingObjects(ICollection <PdfIndirectReference> forbiddenToFlush)
        {
            PdfXrefTable xref      = document.GetXref();
            bool         needFlush = true;

            while (needFlush)
            {
                needFlush = false;
                for (int i = 1; i < xref.Size(); i++)
                {
                    PdfIndirectReference indirectReference = xref.Get(i);
                    if (indirectReference != null && !indirectReference.IsFree() && indirectReference.CheckState(PdfObject.MUST_BE_FLUSHED
                                                                                                                 ) && !forbiddenToFlush.Contains(indirectReference))
                    {
                        PdfObject obj = indirectReference.GetRefersTo(false);
                        if (obj != null)
                        {
                            obj.Flush();
                            needFlush = true;
                        }
                    }
                }
            }
            if (objectStream != null && objectStream.GetSize() > 0)
            {
                objectStream.Flush();
                objectStream = null;
            }
        }
Beispiel #5
0
        /// <summary>Flushes all modified objects which have not been flushed yet.</summary>
        /// <remarks>Flushes all modified objects which have not been flushed yet. Used in case incremental updates.</remarks>
        protected internal virtual void FlushModifiedWaitingObjects()
        {
            PdfXrefTable xref = document.GetXref();

            for (int i = 1; i < xref.Size(); i++)
            {
                PdfIndirectReference indirectReference = xref.Get(i);
                if (null != indirectReference)
                {
                    PdfObject obj = indirectReference.GetRefersTo(false);
                    if (obj != null && !obj.Equals(objectStream) && obj.IsModified())
                    {
                        obj.Flush();
                    }
                }
            }
            if (objectStream != null && objectStream.GetSize() > 0)
            {
                objectStream.Flush();
                objectStream = null;
            }
        }
Beispiel #6
0
 private void Write(PdfIndirectReference indirectReference)
 {
     if (document != null && !indirectReference.GetDocument().Equals(document))
     {
         throw new PdfException(PdfException.PdfInderectObjectBelongToOtherPdfDocument);
     }
     if (indirectReference.GetRefersTo() == null)
     {
         Write(PdfNull.PDF_NULL);
     }
     else
     {
         if (indirectReference.GetGenNumber() == 0)
         {
             WriteInteger(indirectReference.GetObjNumber()).WriteBytes(endIndirectWithZeroGenNr);
         }
         else
         {
             WriteInteger(indirectReference.GetObjNumber()).WriteSpace().WriteInteger(indirectReference.GetGenNumber())
             .WriteBytes(endIndirect);
         }
     }
 }
        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());
            }
        }
Beispiel #9
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);
        }
Beispiel #10
0
            // 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);
                }
            }
Beispiel #11
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);
        }