Ejemplo n.º 1
0
        /// <summary>Removes an annotation from the page.</summary>
        /// <remarks>
        /// Removes an annotation from the page.
        /// <br /><br />
        /// NOTE: If document is tagged, PdfDocument's PdfTagStructure instance will point at annotation tag parent after method call.
        /// </remarks>
        /// <param name="annotation">an annotation to be removed.</param>
        /// <returns>this PdfPage instance.</returns>
        public virtual iText.Kernel.Pdf.PdfPage RemoveAnnotation(PdfAnnotation annotation)
        {
            PdfArray annots = GetAnnots(false);

            if (annots != null)
            {
                if (annots.Contains(annotation.GetPdfObject()))
                {
                    annots.Remove(annotation.GetPdfObject());
                }
                else
                {
                    annots.Remove(annotation.GetPdfObject().GetIndirectReference());
                }
                if (annots.IsEmpty())
                {
                    GetPdfObject().Remove(PdfName.Annots);
                }
            }
            if (GetDocument().IsTagged())
            {
                TagTreePointer tagPointer = GetDocument().GetTagStructureContext().RemoveAnnotationTag(annotation);
                if (tagPointer != null)
                {
                    bool standardAnnotTagRole = tagPointer.GetRole().Equals(PdfName.Annot) || tagPointer.GetRole().Equals(PdfName
                                                                                                                          .Form);
                    if (tagPointer.GetKidsRoles().Count == 0 && standardAnnotTagRole)
                    {
                        tagPointer.RemoveTag();
                    }
                }
            }
            return(this);
        }
Ejemplo n.º 2
0
        public virtual void TestRemove2()
        {
            PdfDocument doc   = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfArray    array = new PdfArray();

            array.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference());
            array.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference());
            array.Add(new PdfNumber(3).MakeIndirect(doc));
            array.Add(new PdfNumber(4).MakeIndirect(doc));
            array.Add(new PdfNumber(5));
            array.Add(new PdfNumber(6));
            PdfArray array2 = new PdfArray();

            array2.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference());
            array2.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference());
            array2.Add(new PdfNumber(3).MakeIndirect(doc));
            array2.Add(new PdfNumber(4).MakeIndirect(doc));
            array2.Add(new PdfNumber(5));
            array2.Add(new PdfNumber(6));
            for (int i = 0; i < array2.Size(); i++)
            {
                array.Remove(array2.Get(i));
            }
            NUnit.Framework.Assert.AreEqual(0, array.Size());
        }
Ejemplo n.º 3
0
        public virtual void TestRemove()
        {
            PdfDocument doc   = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfArray    array = new PdfArray();

            array.Add(((PdfNumber) new PdfNumber(1).MakeIndirect(doc)).GetIndirectReference());
            array.Add(((PdfNumber) new PdfNumber(2).MakeIndirect(doc)).GetIndirectReference());
            array.Add(((PdfNumber) new PdfNumber(3).MakeIndirect(doc)));
            array.Add(((PdfNumber) new PdfNumber(4).MakeIndirect(doc)));
            array.Add(new PdfNumber(5));
            array.Add(new PdfNumber(6));
            PdfArray array2 = new PdfArray();

            array2.Add(((PdfNumber) new PdfNumber(1).MakeIndirect(doc)).GetIndirectReference());
            array2.Add(((PdfNumber) new PdfNumber(2).MakeIndirect(doc)).GetIndirectReference());
            array2.Add(((PdfNumber) new PdfNumber(3).MakeIndirect(doc)));
            array2.Add(((PdfNumber) new PdfNumber(4).MakeIndirect(doc)));
            array2.Add(new PdfNumber(5));
            array2.Add(new PdfNumber(6));
            foreach (PdfObject obj in array2)
            {
                array.Remove(obj);
            }
            NUnit.Framework.Assert.AreEqual(0, array.Size());
        }
Ejemplo n.º 4
0
        public virtual void NotReuseIndirectRefForObjectStreamTest()
        {
            String      inputFile  = sourceFolder + "notReuseIndirectRefForObjectStream.pdf";
            String      outputFile = destinationFolder + "adjustingsInObjStm.pdf";
            PdfDocument pdfDoc     = new PdfDocument(new PdfReader(inputFile), new PdfWriter(outputFile).SetCompressionLevel
                                                         (CompressionConstants.NO_COMPRESSION));
            PdfArray media = pdfDoc.GetPage(1).GetPdfObject().GetAsArray(PdfName.MediaBox);

            media.Remove(2);
            media.Add(new PdfNumber(500));
            media.SetModified();
            pdfDoc.Close();
            PdfDocument   doc      = new PdfDocument(new PdfReader(sourceFolder + "adjustingsInObjStm.pdf"));
            PdfObject     @object  = doc.GetPdfObject(8);
            PdfDictionary pageDict = (PdfDictionary)@object;
            int           expectNumberOfObjects = pdfDoc.GetNumberOfPdfObjects();

            //output pdf document should be openable
            NUnit.Framework.Assert.AreEqual(10, expectNumberOfObjects);
            NUnit.Framework.Assert.AreEqual(PdfName.ObjStm, pageDict.Get(PdfName.Type));
        }
Ejemplo n.º 5
0
        public virtual void TestValuesIndirectRemove()
        {
            PdfDocument doc   = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfArray    array = new PdfArray();

            array.Add(new PdfNumber(1).MakeIndirect(doc).GetIndirectReference());
            array.Add(new PdfNumber(2).MakeIndirect(doc).GetIndirectReference());
            array.Add(new PdfNumber(3).MakeIndirect(doc));
            array.Add(new PdfNumber(4).MakeIndirect(doc));
            array.Add(new PdfNumber(5));
            array.Add(new PdfNumber(6));
            array.Remove(array.Get(0, false));
            array.Remove(array.Get(0, false));
            array.Remove(array.Get(0).GetIndirectReference());
            array.Remove(array.Get(0).GetIndirectReference());
            array.Remove(array.Get(0));
            array.Remove(array.Get(0));
            NUnit.Framework.Assert.AreEqual(0, array.Size());
        }
Ejemplo n.º 6
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);
        }
Ejemplo n.º 7
0
        private void LoadPage(int pageNum)
        {
            PdfDictionary 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);
                if (page == null)
                {
                    // null values not allowed in pages tree.
                    throw new PdfException(PdfException.InvalidPageStructure1).SetMessageParams(pageNum + 1);
                }
                PdfObject pageKids = page.Get(PdfName.Kids);
                if (pageKids != null)
                {
                    if (pageKids.GetObjectType() == PdfObject.ARRAY)
                    {
                        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_1 = 0; i_1 < kids.Size() && kidsCount > 0; i_1++)
                {
                    PdfDictionary pdfPagesObject = kids.GetAsDictionary(i_1);
                    if (pdfPagesObject.GetAsArray(PdfName.Kids) == null)
                    {
                        // pdfPagesObject is PdfPage
                        if (lastPdfPages == null)
                        {
                            // possible if only first kid is PdfPage
                            lastPdfPages = new PdfPages(parent.GetFrom(), document, parent);
                            kids.Set(i_1, lastPdfPages.GetPdfObject());
                            newParents.Add(lastPdfPages);
                        }
                        lastPdfPages.AddPage(pdfPagesObject);
                        kids.Remove(i_1);
                        i_1--;
                        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_2 = newParents.Count - 1; i_2 >= 0; i_2--)
                {
                    parents.Add(parentIndex, newParents[i_2]);
                }
                // 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_1 = 0; i_1 < parent.GetCount(); i_1++)
                {
                    pageRefs[from + i_1] = kids.GetAsDictionary(i_1);
                }
            }
        }
Ejemplo n.º 8
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 (document.GetReader().IsMemorySavingMode() && !findPdfPages && parent.GetFrom() + i != pageNum)
                {
                    page.Release();
                }
            }
            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++)
                {
                    /*
                     * We don't release pdfPagesObject in the end of each loop because we enter this for-cycle only when parent has PdfPages kids.
                     * If all of the kids are PdfPages, then there's nothing to release, because we don't release PdfPages at this point.
                     * If there are kids that are instances of PdfPage, then there's no sense in releasing them:
                     * in this case ParentTreeStructure is being rebuilt by inserting an intermediate PdfPages between the parent and a PdfPage,
                     * thus modifying the page object by resetting its parent, thus making it impossible to release the object.
                     */
                    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++)
                {
                    PdfObject kid = kids.Get(i, false);
                    if (kid is PdfIndirectReference)
                    {
                        pageRefs[from + i] = (PdfIndirectReference)kid;
                    }
                    else
                    {
                        pageRefs[from + i] = kid.GetIndirectReference();
                    }
                }
            }
        }
Ejemplo n.º 9
0
 private static void CopyDArrayField(PdfName fieldToCopy, ICollection<PdfIndirectReference> fromOcgsToCopy, 
     PdfDictionary fromDict, PdfDictionary toDict, PdfDocument toDocument) {
     if (fromDict.GetAsArray(fieldToCopy) == null) {
         return;
     }
     PdfArray fromArray = fromDict.GetAsArray(fieldToCopy);
     if (toDict.GetAsArray(fieldToCopy) == null) {
         toDict.Put(fieldToCopy, new PdfArray());
     }
     PdfArray toArray = toDict.GetAsArray(fieldToCopy);
     ICollection<PdfIndirectReference> toOcgsToCopy = new HashSet<PdfIndirectReference>();
     foreach (PdfIndirectReference fromRef in fromOcgsToCopy) {
         toOcgsToCopy.Add(fromRef.GetRefersTo().CopyTo(toDocument, false).GetIndirectReference());
     }
     if (PdfName.Order.Equals(fieldToCopy)) {
         // Stage 1: delete all Order the entire branches from the output document in which the copied OCGs were
         IList<int> removeIndex = new List<int>();
         for (int i = 0; i < toArray.Size(); i++) {
             PdfObject toOrderItem = toArray.Get(i);
             if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(toOrderItem, toArray, i, toOcgsToCopy
                 , null, null)) {
                 removeIndex.Add(i);
             }
         }
         for (int i = removeIndex.Count - 1; i > -1; i--) {
             toArray.Remove(removeIndex[i]);
         }
         PdfArray toOcgs = toDocument.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.OCProperties).GetAsArray(
             PdfName.OCGs);
         // Stage 2: copy all the Order the entire branches in which the copied OСGs were
         for (int i = 0; i < fromArray.Size(); i++) {
             PdfObject fromOrderItem = fromArray.Get(i);
             if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(fromOrderItem, fromArray, i, fromOcgsToCopy
                 , toOcgs, toDocument)) {
                 toArray.Add(fromOrderItem.CopyTo(toDocument, false));
             }
         }
     }
     else {
         // Stage 3: remove from Order OCGs not presented in the output document. When forming
         // the Order dictionary in the PdfOcProperties constructor, only those OCGs that are
         // in the OCProperties/OCGs array will be taken into account
         if (PdfName.RBGroups.Equals(fieldToCopy)) {
             // Stage 1: delete all RBGroups from the output document in which the copied OCGs were
             for (int i = toArray.Size() - 1; i > -1; i--) {
                 PdfArray toRbGroup = (PdfArray)toArray.Get(i);
                 foreach (PdfObject toRbGroupItemObj in toRbGroup) {
                     if (toOcgsToCopy.Contains(toRbGroupItemObj.GetIndirectReference())) {
                         toArray.Remove(i);
                         break;
                     }
                 }
             }
             // Stage 2: copy all the RBGroups in which the copied OCGs were
             foreach (PdfObject fromRbGroupObj in fromArray) {
                 PdfArray fromRbGroup = (PdfArray)fromRbGroupObj;
                 foreach (PdfObject fromRbGroupItemObj in fromRbGroup) {
                     if (fromOcgsToCopy.Contains(fromRbGroupItemObj.GetIndirectReference())) {
                         toArray.Add(fromRbGroup.CopyTo(toDocument, false));
                         break;
                     }
                 }
             }
         }
         else {
             // Stage 3: remove from RBGroups OCGs not presented in the output
             // document (is in the PdfOcProperties#fillDictionary method)
             foreach (PdfObject fromObj in fromArray) {
                 iText.Kernel.Pdf.OcgPropertiesCopier.AttemptToAddObjectToArray(fromOcgsToCopy, fromObj, toArray, toDocument
                     );
             }
         }
     }
     if (toArray.IsEmpty()) {
         toDict.Remove(fieldToCopy);
     }
 }