Beispiel #1
0
 private static ICollection<PdfIndirectReference> GetAllUsedNonFlushedOCGs(IDictionary<PdfPage, PdfPage> page2page
     , PdfDictionary toOcProperties) {
     // NOTE: the PDF is considered to be valid and therefore the presence of OСG in OCProperties.OCGs is not checked
     ICollection<PdfIndirectReference> fromUsedOcgs = new LinkedHashSet<PdfIndirectReference>();
     // Visit the pages in parallel to find non-flush OSGs
     PdfPage[] fromPages = page2page.Keys.ToArray(new PdfPage[0]);
     PdfPage[] toPages = page2page.Values.ToArray(new PdfPage[0]);
     for (int i = 0; i < toPages.Length; i++) {
         PdfPage fromPage = fromPages[i];
         PdfPage toPage = toPages[i];
         // Copy OCGs from annotations
         IList<PdfAnnotation> toAnnotations = toPage.GetAnnotations();
         IList<PdfAnnotation> fromAnnotations = fromPage.GetAnnotations();
         for (int j = 0; j < toAnnotations.Count; j++) {
             if (!toAnnotations[j].IsFlushed()) {
                 PdfDictionary toAnnotDict = toAnnotations[j].GetPdfObject();
                 PdfDictionary fromAnnotDict = fromAnnotations[j].GetPdfObject();
                 PdfAnnotation toAnnot = toAnnotations[j];
                 PdfAnnotation fromAnnot = fromAnnotations[j];
                 if (!toAnnotDict.IsFlushed()) {
                     iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toAnnotDict.GetAsDictionary(PdfName.OC
                         ), fromAnnotDict.GetAsDictionary(PdfName.OC), fromUsedOcgs, toOcProperties);
                     iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetNormalAppearanceObject(), 
                         fromAnnot.GetNormalAppearanceObject(), fromUsedOcgs, toOcProperties);
                     iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetRolloverAppearanceObject(
                         ), fromAnnot.GetRolloverAppearanceObject(), fromUsedOcgs, toOcProperties);
                     iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toAnnot.GetDownAppearanceObject(), fromAnnot
                         .GetDownAppearanceObject(), fromUsedOcgs, toOcProperties);
                 }
             }
         }
         PdfDictionary toResources = toPage.GetPdfObject().GetAsDictionary(PdfName.Resources);
         PdfDictionary fromResources = fromPage.GetPdfObject().GetAsDictionary(PdfName.Resources);
         iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromResources(toResources, fromResources, fromUsedOcgs
             , toOcProperties);
     }
     return fromUsedOcgs;
 }
        /// <summary>Retrieves the names stored in the name tree</summary>
        /// <returns>Map containing the PdfObjects stored in the tree</returns>
        public virtual IDictionary <String, PdfObject> GetNames()
        {
            if (items.Count > 0)
            {
                return(items);
            }
            PdfDictionary dictionary = catalog.GetPdfObject().GetAsDictionary(PdfName.Names);

            if (dictionary != null)
            {
                dictionary = dictionary.GetAsDictionary(treeType);
                if (dictionary != null)
                {
                    items = ReadTree(dictionary);
                    // A separate collection for keys is used for auto porting to C#, because in C#
                    // it is impossible to change the collection which you iterate in for loop
                    ICollection <String> keys = new HashSet <String>();
                    keys.AddAll(items.Keys);
                    foreach (String key in keys)
                    {
                        if (treeType.Equals(PdfName.Dests))
                        {
                            PdfArray arr = GetDestArray(items.Get(key));
                            if (arr != null)
                            {
                                items.Put(key, arr);
                            }
                            else
                            {
                                items.JRemove(key);
                            }
                        }
                        else
                        {
                            if (items.Get(key) == null)
                            {
                                items.JRemove(key);
                            }
                        }
                    }
                }
            }
            if (treeType.Equals(PdfName.Dests))
            {
                PdfDictionary destinations = catalog.GetPdfObject().GetAsDictionary(PdfName.Dests);
                if (destinations != null)
                {
                    ICollection <PdfName> keys = destinations.KeySet();
                    foreach (PdfName key in keys)
                    {
                        PdfArray array = GetDestArray(destinations.Get(key));
                        if (array == null)
                        {
                            continue;
                        }
                        items.Put(key.GetValue(), array);
                    }
                }
            }
            return(items);
        }
Beispiel #3
0
        /// <summary>Retrieves the names stored in the name tree</summary>
        /// <returns>Map containing the PdfObjects stored in the tree</returns>
        public virtual IDictionary <String, PdfObject> GetNames()
        {
            if (items.Count > 0)
            {
                return(items);
            }
            PdfDictionary dictionary = catalog.GetPdfObject().GetAsDictionary(PdfName.Names);

            if (dictionary != null)
            {
                dictionary = dictionary.GetAsDictionary(treeType);
                if (dictionary != null)
                {
                    items = ReadTree(dictionary);
                    //@TODO It's done for auto porting to itextsharp, cuz u cannot change collection which you iterate
                    // in for loop (even if you change only value of a Map entry) in .NET. Java doesn't have such a problem.
                    // We should find a better solution in the future.
                    ICollection <String> keys = new HashSet <String>();
                    keys.AddAll(items.Keys);
                    foreach (String key in keys)
                    {
                        if (treeType.Equals(PdfName.Dests))
                        {
                            PdfArray arr = GetDestArray(items.Get(key));
                            if (arr != null)
                            {
                                items.Put(key, arr);
                            }
                            else
                            {
                                items.JRemove(key);
                            }
                        }
                        else
                        {
                            if (items.Get(key) == null)
                            {
                                items.JRemove(key);
                            }
                        }
                    }
                }
            }
            if (treeType.Equals(PdfName.Dests))
            {
                PdfDictionary destinations = catalog.GetPdfObject().GetAsDictionary(PdfName.Dests);
                if (destinations != null)
                {
                    ICollection <PdfName> keys = destinations.KeySet();
                    foreach (PdfName key in keys)
                    {
                        PdfArray array = GetDestArray(destinations.Get(key));
                        if (array == null)
                        {
                            continue;
                        }
                        items.Put(key.GetValue(), array);
                    }
                }
            }
            return(items);
        }