コード例 #1
0
ファイル: PdfNumTree.cs プロジェクト: leofidus/itext7-dotnet
 public virtual void AddEntry(int?key, PdfObject value)
 {
     items[key] = value;
 }
コード例 #2
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());
            }
        }
コード例 #3
0
 protected internal override void CopyContent(PdfObject from, PdfDocument document)
 {
     base.CopyContent(from, document);
     iText.Kernel.Pdf.PdfBoolean @bool = (iText.Kernel.Pdf.PdfBoolean)from;
     value = @bool.value;
 }
コード例 #4
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);
        }
コード例 #5
0
 private static bool CheckTypeOfPdfDictionary(PdfObject dictionary, PdfName expectedType)
 {
     return(dictionary.IsDictionary() && expectedType.Equals(((PdfDictionary)dictionary).GetAsName(PdfName.Type
                                                                                                   )));
 }
コード例 #6
0
 /// <summary>This methods adds a new name to the specified NameTree.</summary>
 /// <remarks>This methods adds a new name to the specified NameTree. It throws an exception, if the name already exists.
 ///     </remarks>
 /// <param name="key">key in the name tree</param>
 /// <param name="value">value in the name tree</param>
 /// <param name="treeType">type of the tree (Dests, AP, EmbeddedFiles etc).</param>
 internal virtual void AddNameToNameTree(String key, PdfObject value, PdfName treeType)
 {
     GetNameTree(treeType).AddEntry(key, value);
 }
コード例 #7
0
 public abstract byte[] Decode(byte[] arg1, PdfName arg2, PdfObject arg3, PdfDictionary arg4);
コード例 #8
0
 public virtual iText.Kernel.Pdf.PdfCatalog Put(PdfName key, PdfObject value)
 {
     GetPdfObject().Put(key, value);
     SetModified();
     return(this);
 }
コード例 #9
0
 /// <summary>This methods adds new name to the Dests NameTree.</summary>
 /// <remarks>This methods adds new name to the Dests NameTree. It throws an exception, if the name already exists.
 ///     </remarks>
 /// <param name="key">Name of the destination.</param>
 /// <param name="value">
 /// An object destination refers to. Must be an array or a dictionary with key /D and array.
 /// See ISO 32000-1 12.3.2.3 for more info.
 /// </param>
 internal virtual void AddNamedDestination(String key, PdfObject value)
 {
     AddNameToNameTree(key, value, PdfName.Dests);
 }
コード例 #10
0
 internal virtual iText.Kernel.Pdf.PdfDocumentInfo Put(PdfName key, PdfObject value)
 {
     GetPdfObject().Put(key, value);
     GetPdfObject().SetModified();
     return(this);
 }
コード例 #11
0
        private void LoadPage(int pageNum)
        {
            PdfIndirectReference targetPage = pageRefs[pageNum];

            if (targetPage != null)
            {
                return;
            }
            //if we go here, we have to split PdfPages that contains pageNum
            int      parentIndex = FindPageParent(pageNum);
            PdfPages parent      = parents[parentIndex];
            PdfArray kids        = parent.GetKids();

            if (kids == null)
            {
                throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1);
            }
            int kidsCount = parent.GetCount();
            // we should handle separated pages, it means every PdfArray kids must contain either PdfPage or PdfPages,
            // mix of PdfPage and PdfPages not allowed.
            bool findPdfPages = false;

            // NOTE optimization? when we already found needed index
            for (int i = 0; i < kids.Size(); i++)
            {
                PdfDictionary page = kids.GetAsDictionary(i);
                // null values not allowed in pages tree.
                if (page == null)
                {
                    throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1);
                }
                PdfObject pageKids = page.Get(PdfName.Kids);
                if (pageKids != null)
                {
                    if (pageKids.IsArray())
                    {
                        findPdfPages = true;
                    }
                    else
                    {
                        // kids must be of type array
                        throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1);
                    }
                }
            }
            if (findPdfPages)
            {
                // handle mix of PdfPage and PdfPages.
                // handle count property!
                IList <PdfPages> newParents   = new List <PdfPages>(kids.Size());
                PdfPages         lastPdfPages = null;
                for (int i = 0; i < kids.Size() && kidsCount > 0; i++)
                {
                    PdfDictionary pdfPagesObject = kids.GetAsDictionary(i);
                    if (pdfPagesObject.GetAsArray(PdfName.Kids) == null)
                    {
                        // pdfPagesObject is PdfPage
                        // possible if only first kid is PdfPage
                        if (lastPdfPages == null)
                        {
                            lastPdfPages = new PdfPages(parent.GetFrom(), document, parent);
                            kids.Set(i, lastPdfPages.GetPdfObject());
                            newParents.Add(lastPdfPages);
                        }
                        else
                        {
                            // Only remove from kids if we did not replace the entry with new PdfPages
                            kids.Remove(i);
                            i--;
                        }
                        // decrement count first so that page is not counted twice when moved to lastPdfPages
                        parent.DecrementCount();
                        lastPdfPages.AddPage(pdfPagesObject);
                        kidsCount--;
                    }
                    else
                    {
                        // pdfPagesObject is PdfPages
                        int from = lastPdfPages == null?parent.GetFrom() : lastPdfPages.GetFrom() + lastPdfPages.GetCount();

                        lastPdfPages = new PdfPages(from, kidsCount, pdfPagesObject, parent);
                        newParents.Add(lastPdfPages);
                        kidsCount -= lastPdfPages.GetCount();
                    }
                }
                parents.JRemoveAt(parentIndex);
                for (int i = newParents.Count - 1; i >= 0; i--)
                {
                    parents.Add(parentIndex, newParents[i]);
                }
                // recursive call, to load needed pageRef.
                // NOTE optimization? add to loadPage startParentIndex.
                LoadPage(pageNum);
            }
            else
            {
                int from = parent.GetFrom();
                // Possible exception in case kids.getSize() < parent.getCount().
                // In any case parent.getCount() has higher priority.
                // NOTE optimization? when we already found needed index
                for (int i = 0; i < parent.GetCount(); i++)
                {
                    PdfDictionary kid = kids.GetAsDictionary(i);
                    // make sure it's a dictionary
                    if (kid != null)
                    {
                        pageRefs[from + i] = kid.GetIndirectReference();
                    }
                }
            }
        }
コード例 #12
0
        /// <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);
        }
コード例 #13
0
        protected internal virtual byte[] DecodeFlateBytes(PdfStream stream, byte[] bytes)
        {
            PdfObject filterObject = stream.Get(PdfName.Filter);

            if (filterObject == null)
            {
                return(bytes);
            }
            // check if flateDecode filter is on top
            PdfName  filterName;
            PdfArray filtersArray = null;

            if (filterObject is PdfName)
            {
                filterName = (PdfName)filterObject;
            }
            else
            {
                if (filterObject is PdfArray)
                {
                    filtersArray = (PdfArray)filterObject;
                    filterName   = filtersArray.GetAsName(0);
                }
                else
                {
                    throw new PdfException(PdfException.FilterIsNotANameOrArray);
                }
            }
            if (!PdfName.FlateDecode.Equals(filterName))
            {
                return(bytes);
            }
            // get decode params if present
            PdfDictionary decodeParams;
            PdfArray      decodeParamsArray  = null;
            PdfObject     decodeParamsObject = stream.Get(PdfName.DecodeParms);

            if (decodeParamsObject == null)
            {
                decodeParams = null;
            }
            else
            {
                if (decodeParamsObject.GetObjectType() == PdfObject.DICTIONARY)
                {
                    decodeParams = (PdfDictionary)decodeParamsObject;
                }
                else
                {
                    if (decodeParamsObject.GetObjectType() == PdfObject.ARRAY)
                    {
                        decodeParamsArray = (PdfArray)decodeParamsObject;
                        decodeParams      = decodeParamsArray.GetAsDictionary(0);
                    }
                    else
                    {
                        throw new PdfException(PdfException.DecodeParameterType1IsNotSupported).SetMessageParams(decodeParamsObject
                                                                                                                 .GetType().ToString());
                    }
                }
            }
            // decode
            byte[] res = FlateDecodeFilter.FlateDecode(bytes, true);
            if (res == null)
            {
                res = FlateDecodeFilter.FlateDecode(bytes, false);
            }
            bytes = FlateDecodeFilter.DecodePredictor(res, decodeParams);
            //remove filter and decode params
            filterObject = null;
            if (filtersArray != null)
            {
                filtersArray.Remove(0);
                if (filtersArray.Size() == 1)
                {
                    filterObject = filtersArray.Get(0);
                }
                else
                {
                    if (!filtersArray.IsEmpty())
                    {
                        filterObject = filtersArray;
                    }
                }
            }
            decodeParamsObject = null;
            if (decodeParamsArray != null)
            {
                decodeParamsArray.Remove(0);
                if (decodeParamsArray.Size() == 1 && decodeParamsArray.Get(0).GetObjectType() != PdfObject.NULL)
                {
                    decodeParamsObject = decodeParamsArray.Get(0);
                }
                else
                {
                    if (!decodeParamsArray.IsEmpty())
                    {
                        decodeParamsObject = decodeParamsArray;
                    }
                }
            }
            if (filterObject == null)
            {
                stream.Remove(PdfName.Filter);
            }
            else
            {
                stream.Put(PdfName.Filter, filterObject);
            }
            if (decodeParamsObject == null)
            {
                stream.Remove(PdfName.DecodeParms);
            }
            else
            {
                stream.Put(PdfName.DecodeParms, decodeParamsObject);
            }
            return(bytes);
        }
コード例 #14
0
 protected internal override void CopyContent(PdfObject from, PdfDocument document)
 {
 }
コード例 #15
0
 protected internal virtual void SetRefersTo(PdfObject refersTo)
 {
     this.refersTo = refersTo;
 }
コード例 #16
0
 protected internal override void CopyContent(PdfObject from, PdfDocument document)
 {
     base.CopyContent(from, document);
     iText.Kernel.Pdf.PdfLiteral literal = (iText.Kernel.Pdf.PdfLiteral)from;
     this.content = literal.GetInternalContent();
 }