Exemple #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);
        }
Exemple #2
0
 protected internal virtual bool CheckEncryption(PdfStream pdfStream)
 {
     if (crypto == null || crypto.IsEmbeddedFilesOnly())
     {
         return(false);
     }
     else
     {
         PdfObject filter = pdfStream.Get(PdfName.Filter, true);
         if (filter != null)
         {
             if (PdfName.Crypt.Equals(filter))
             {
                 return(false);
             }
             else
             {
                 if (filter.GetObjectType() == PdfObject.ARRAY)
                 {
                     PdfArray filters = (PdfArray)filter;
                     if (!filters.IsEmpty() && PdfName.Crypt.Equals(filters.Get(0, true)))
                     {
                         return(false);
                     }
                 }
             }
         }
         return(true);
     }
 }
Exemple #3
0
 private static bool OrderBranchContainsSetElements(PdfObject arrayObj, PdfArray array, int currentIndex, ICollection
     <PdfIndirectReference> ocgs, PdfArray toOcgs, PdfDocument toDocument) {
     if (arrayObj.IsDictionary()) {
         if (ocgs.Contains(arrayObj.GetIndirectReference())) {
             return true;
         }
         else {
             if (currentIndex < (array.Size() - 1) && array.Get(currentIndex + 1).IsArray()) {
                 PdfArray nextArray = array.GetAsArray(currentIndex + 1);
                 if (!nextArray.Get(0).IsString()) {
                     bool result = iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(nextArray, array, currentIndex
                          + 1, ocgs, toOcgs, toDocument);
                     if (result && toOcgs != null && !ocgs.Contains(arrayObj.GetIndirectReference())) {
                         // Add the OCG to the OCGs array to register the OCG in document, since it is not used
                         // directly in the document, but is used as a parent for the order group. If it is not added
                         // to the OCGs array, then the OCG will be deleted at the 3rd stage of the /Order entry coping.
                         toOcgs.Add(arrayObj.CopyTo(toDocument, false));
                     }
                     return result;
                 }
             }
         }
     }
     else {
         if (arrayObj.IsArray()) {
             PdfArray arrayItem = (PdfArray)arrayObj;
             for (int i = 0; i < arrayItem.Size(); i++) {
                 PdfObject obj = arrayItem.Get(i);
                 if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(obj, arrayItem, i, ocgs, toOcgs, toDocument
                     )) {
                     return true;
                 }
             }
             if (!arrayItem.IsEmpty() && !arrayItem.Get(0).IsString()) {
                 if (currentIndex > 0 && array.Get(currentIndex - 1).IsDictionary()) {
                     PdfDictionary previousDict = (PdfDictionary)array.Get(currentIndex - 1);
                     return ocgs.Contains(previousDict.GetIndirectReference());
                 }
             }
         }
     }
     return false;
 }
Exemple #4
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);
        }
Exemple #5
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);
     }
 }