コード例 #1
0
ファイル: PdfObject.cs プロジェクト: ReaZhuang/itext7-dotnet
 /// <summary>
 /// Processes two cases of object copying:
 /// <list type="number">
 /// <item><description>copying to the other document
 /// </description></item>
 /// <item><description>cloning inside of the current document
 /// </description></item>
 /// </list>
 /// </summary>
 /// <remarks>
 /// Processes two cases of object copying:
 /// <list type="number">
 /// <item><description>copying to the other document
 /// </description></item>
 /// <item><description>cloning inside of the current document
 /// </description></item>
 /// </list>
 /// <para />
 /// This two cases are distinguished by the state of
 /// <c>document</c>
 /// parameter:
 /// the second case is processed if
 /// <c>document</c>
 /// is
 /// <see langword="null"/>.
 /// </remarks>
 /// <param name="documentTo">if not null: document to copy object to; otherwise indicates that object is to be cloned.
 ///     </param>
 /// <param name="allowDuplicating">
 /// indicates if to allow copy objects which already have been copied.
 /// If object is associated with any indirect reference and allowDuplicating is false then already existing reference will be returned instead of copying object.
 /// If allowDuplicating is true then object will be copied and new indirect reference will be assigned.
 /// </param>
 /// <returns>copied object.</returns>
 internal virtual PdfObject ProcessCopying(PdfDocument documentTo, bool allowDuplicating)
 {
     if (documentTo != null)
     {
         //copyTo case
         PdfWriter writer = documentTo.GetWriter();
         if (writer == null)
         {
             throw new PdfException(PdfException.CannotCopyToDocumentOpenedInReadingMode);
         }
         return(writer.CopyObject(this, documentTo, allowDuplicating));
     }
     else
     {
         //clone case
         PdfObject obj = this;
         if (obj.IsIndirectReference())
         {
             PdfObject refTo = ((PdfIndirectReference)obj).GetRefersTo();
             obj = refTo != null ? refTo : obj;
         }
         if (obj.IsIndirect() && !allowDuplicating)
         {
             return(obj);
         }
         return(obj.Clone());
     }
 }
コード例 #2
0
        /// <exception cref="System.Exception"/>
        private void TestFile(String filename, int page, String objectid)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + filename));

            try {
                PdfResources  resources = pdfDocument.GetPage(page).GetResources();
                PdfDictionary xobjets   = resources.GetResource(PdfName.XObject);
                PdfObject     obj       = xobjets.Get(new PdfName(objectid));
                if (obj == null)
                {
                    throw new ArgumentNullException("Reference " + objectid + " not found - Available keys are " + xobjets.KeySet
                                                        ());
                }
                PdfImageXObject img = new PdfImageXObject((PdfStream)(obj.IsIndirectReference() ? ((PdfIndirectReference)obj
                                                                                                   ).GetRefersTo() : obj));
                byte[] result = img.GetImageBytes(true);
                NUnit.Framework.Assert.IsNotNull(result);
                int zeroCount = 0;
                foreach (byte b in result)
                {
                    if (b == 0)
                    {
                        zeroCount++;
                    }
                }
                NUnit.Framework.Assert.IsTrue(zeroCount > 0);
            }
            finally {
                pdfDocument.Close();
            }
        }
コード例 #3
0
ファイル: PdfObject.cs プロジェクト: ReaZhuang/itext7-dotnet
        internal static bool EqualContent(PdfObject obj1, PdfObject obj2)
        {
            PdfObject direct1 = obj1 != null && obj1.IsIndirectReference() ? ((PdfIndirectReference)obj1).GetRefersTo(
                true) : obj1;
            PdfObject direct2 = obj2 != null && obj2.IsIndirectReference() ? ((PdfIndirectReference)obj2).GetRefersTo(
                true) : obj2;

            return(direct1 != null && direct1.Equals(direct2));
        }
コード例 #4
0
ファイル: PdfWriter.cs プロジェクト: KenneyChen/itext7-dotnet
            protected internal virtual int GetCopyObjectKey(PdfObject obj)
            {
                PdfIndirectReference reference;

                if (obj.IsIndirectReference())
                {
                    reference = (PdfIndirectReference)obj;
                }
                else
                {
                    reference = obj.GetIndirectReference();
                }
                int result = reference.GetHashCode();

                result = 31 * result + reference.GetDocument().GetHashCode();
                return(result);
            }
コード例 #5
0
        public virtual ICollection <KeyValuePair <PdfName, PdfObject> > DirectEntrySet()
        {
            IDictionary <PdfName, PdfObject> directMap = new SortedDictionary <PdfName, PdfObject>();

            foreach (KeyValuePair <PdfName, PdfObject> entry in map)
            {
                PdfObject value = entry.Value;
                if (value.IsIndirectReference())
                {
                    directMap[entry.Key] = ((PdfIndirectReference)value).GetRefersTo();
                }
                else
                {
                    directMap[entry.Key] = value;
                }
            }
            return(directMap);
        }
コード例 #6
0
        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);
            }
        }
コード例 #7
0
        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());
            }
        }
コード例 #8
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);
                }
            }
コード例 #9
0
ファイル: PdfWriter.cs プロジェクト: KenneyChen/itext7-dotnet
            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);
                }
            }