Beispiel #1
0
 private static void GetUsedNonFlushedOCGsFromXObject(PdfDictionary toXObject, PdfDictionary fromXObject, ICollection
     <PdfIndirectReference> fromUsedOcgs, PdfDictionary toOcProperties) {
     if (toXObject != null && !toXObject.IsFlushed()) {
         if (toXObject.IsStream() && !toXObject.IsFlushed()) {
             PdfStream toStream = (PdfStream)toXObject;
             PdfStream fromStream = (PdfStream)fromXObject;
             iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toStream.GetAsDictionary(PdfName.OC), 
                 fromStream.GetAsDictionary(PdfName.OC), fromUsedOcgs, toOcProperties);
             iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromResources(toStream.GetAsDictionary(PdfName.Resources
                 ), fromStream.GetAsDictionary(PdfName.Resources), fromUsedOcgs, toOcProperties);
         }
         else {
             foreach (PdfName name in toXObject.KeySet()) {
                 PdfObject toCurrObj = toXObject.Get(name);
                 PdfObject fromCurrObj = fromXObject.Get(name);
                 if (toCurrObj.IsStream() && !toCurrObj.IsFlushed()) {
                     PdfStream toStream = (PdfStream)toCurrObj;
                     PdfStream fromStream = (PdfStream)fromCurrObj;
                     iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toStream, fromStream, fromUsedOcgs, 
                         toOcProperties);
                 }
             }
         }
     }
 }
Beispiel #2
0
        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)
                    {
                        PdfArray arr = GetNameArray(items.Get(key));
                        if (arr != null)
                        {
                            items[key] = arr;
                        }
                        else
                        {
                            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 = GetNameArray(destinations.Get(key));
                        if (array == null)
                        {
                            continue;
                        }
                        items[key.GetValue()] = array;
                    }
                }
            }
            return(items);
        }
Beispiel #3
0
 private void SerDic(PdfDictionary dic, int level, ByteBufferOutputStream bb)
 {
     bb.Append("$D");
     if (level <= 0)
     {
         return;
     }
     PdfName[] keys = new PdfName[dic.KeySet().Count];
     keys = dic.KeySet().ToArray(keys);
     iText.IO.Util.JavaUtil.Sort(keys);
     foreach (Object key in keys)
     {
         if (key.Equals(PdfName.P) && (dic.Get((PdfName)key).IsIndirectReference() || dic.Get((PdfName)key).IsDictionary
                                           ()) || key.Equals(PdfName.Parent))
         {
             // ignore recursive call
             continue;
         }
         SerObject((PdfObject)key, level, bb);
         SerObject(dic.Get((PdfName)key, false), level, bb);
     }
 }
        public virtual void RemovePageWithFormFieldsTest()
        {
            String      filename = sourceFolder + "docWithFields.pdf";
            PdfDocument pdfDoc   = new PdfDocument(new PdfReader(filename));

            pdfDoc.RemovePage(1);
            PdfArray fields = pdfDoc.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.AcroForm).GetAsArray(PdfName.
                                                                                                              Fields);
            PdfDictionary field = (PdfDictionary)fields.Get(0);
            PdfDictionary kid   = (PdfDictionary)field.GetAsArray(PdfName.Kids).Get(0);

            NUnit.Framework.Assert.AreEqual(6, kid.KeySet().Count);
            NUnit.Framework.Assert.AreEqual(3, fields.Size());
        }
 private void FlushDictRecursively(PdfDictionary dict, PageFlushingHelper.DeepFlushingContext context)
 {
     foreach (PdfName key in dict.KeySet())
     {
         PageFlushingHelper.DeepFlushingContext innerContext = null;
         if (context != null)
         {
             if (context.IsKeyInBlackList(key))
             {
                 continue;
             }
             innerContext = context.GetInnerContextFor(key);
         }
         PdfObject value = dict.Get(key, false);
         FlushObjectRecursively(value, innerContext);
     }
 }
 private void SerDic(PdfDictionary dic, ByteBuffer bb, int level, IDictionary <PdfIndirectReference, byte[]>
                     serializedCache)
 {
     bb.Append("$D");
     if (level <= 0)
     {
         return;
     }
     foreach (PdfName key in dic.KeySet())
     {
         if (IsKeyRefersBack(dic, key))
         {
             continue;
         }
         SerObject(key, bb, level, serializedCache);
         SerObject(dic.Get(key, false), bb, level, serializedCache);
     }
     bb.Append("$\\D");
 }
Beispiel #7
0
 private static void GetUsedNonFlushedOCGsFromResources(PdfDictionary toResources, PdfDictionary fromResources
     , ICollection<PdfIndirectReference> fromUsedOcgs, PdfDictionary toOcProperties) {
     if (toResources != null && !toResources.IsFlushed()) {
         // Copy OCGs from properties
         PdfDictionary toProperties = toResources.GetAsDictionary(PdfName.Properties);
         PdfDictionary fromProperties = fromResources.GetAsDictionary(PdfName.Properties);
         if (toProperties != null && !toProperties.IsFlushed()) {
             foreach (PdfName name in toProperties.KeySet()) {
                 PdfObject toCurrObj = toProperties.Get(name);
                 PdfObject fromCurrObj = fromProperties.Get(name);
                 iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromOcDict(toCurrObj, fromCurrObj, fromUsedOcgs, 
                     toOcProperties);
             }
         }
         // Copy OCGs from xObject
         PdfDictionary toXObject = toResources.GetAsDictionary(PdfName.XObject);
         PdfDictionary fromXObject = fromResources.GetAsDictionary(PdfName.XObject);
         iText.Kernel.Pdf.OcgPropertiesCopier.GetUsedNonFlushedOCGsFromXObject(toXObject, fromXObject, fromUsedOcgs
             , toOcProperties);
     }
 }
Beispiel #8
0
 private void Write(PdfDictionary pdfDictionary)
 {
     WriteBytes(openDict);
     foreach (PdfName key in pdfDictionary.KeySet())
     {
         bool isAlreadyWriteSpace = false;
         Write(key);
         PdfObject value = pdfDictionary.Get(key, false);
         if (value == null)
         {
             ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.PdfOutputStream));
             logger.Warn(MessageFormatUtil.Format(iText.IO.LogMessageConstant.INVALID_KEY_VALUE_KEY_0_HAS_NULL_VALUE, key
                                                  ));
             value = PdfNull.PDF_NULL;
         }
         if ((value.GetObjectType() == PdfObject.NUMBER || value.GetObjectType() == PdfObject.LITERAL || value.GetObjectType
                  () == PdfObject.BOOLEAN || value.GetObjectType() == PdfObject.NULL || value.GetObjectType() == PdfObject
              .INDIRECT_REFERENCE || value.CheckState(PdfObject.MUST_BE_INDIRECT)))
         {
             isAlreadyWriteSpace = true;
             WriteSpace();
         }
         PdfIndirectReference indirectReference;
         if ((indirectReference = value.GetIndirectReference()) != null)
         {
             if (!isAlreadyWriteSpace)
             {
                 WriteSpace();
             }
             Write(indirectReference);
         }
         else
         {
             Write(value);
         }
     }
     WriteBytes(closeDict);
 }
Beispiel #9
0
        /// <exception cref="iText.Kernel.XMP.XMPException"/>
        internal static void AppendDocumentInfoToMetadata(PdfDocumentInfo info, XMPMeta xmpMeta)
        {
            PdfDictionary docInfo = info.GetPdfObject();

            if (docInfo != null)
            {
                PdfName   key;
                PdfObject obj;
                String    value;
                foreach (PdfName pdfName in docInfo.KeySet())
                {
                    key = pdfName;
                    obj = docInfo.Get(key);
                    if (obj == null)
                    {
                        continue;
                    }
                    if (obj.IsString())
                    {
                        value = ((PdfString)obj).ToUnicodeString();
                    }
                    else
                    {
                        if (obj.IsName())
                        {
                            value = ((PdfName)obj).GetValue();
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (PdfName.Title.Equals(key))
                    {
                        xmpMeta.SetLocalizedText(XMPConst.NS_DC, PdfConst.Title, XMPConst.X_DEFAULT, XMPConst.X_DEFAULT, value);
                    }
                    else
                    {
                        if (PdfName.Author.Equals(key))
                        {
                            foreach (String v in iText.IO.Util.StringUtil.Split(value, ",|;"))
                            {
                                if (v.Trim().Length > 0)
                                {
                                    AppendArrayItemIfDoesNotExist(xmpMeta, XMPConst.NS_DC, PdfConst.Creator, v.Trim(), PropertyOptions.ARRAY_ORDERED
                                                                  );
                                }
                            }
                        }
                        else
                        {
                            if (PdfName.Subject.Equals(key))
                            {
                                xmpMeta.SetLocalizedText(XMPConst.NS_DC, PdfConst.Description, XMPConst.X_DEFAULT, XMPConst.X_DEFAULT, value
                                                         );
                            }
                            else
                            {
                                if (PdfName.Keywords.Equals(key))
                                {
                                    foreach (String v in iText.IO.Util.StringUtil.Split(value, ",|;"))
                                    {
                                        if (v.Trim().Length > 0)
                                        {
                                            AppendArrayItemIfDoesNotExist(xmpMeta, XMPConst.NS_DC, PdfConst.Subject, v.Trim(), PropertyOptions.ARRAY);
                                        }
                                    }
                                    xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Keywords, value);
                                }
                                else
                                {
                                    if (PdfName.Creator.Equals(key))
                                    {
                                        xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.CreatorTool, value);
                                    }
                                    else
                                    {
                                        if (PdfName.Producer.Equals(key))
                                        {
                                            xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Producer, value);
                                        }
                                        else
                                        {
                                            if (PdfName.CreationDate.Equals(key))
                                            {
                                                xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.CreateDate, PdfDate.GetW3CDate(value));
                                            }
                                            else
                                            {
                                                if (PdfName.ModDate.Equals(key))
                                                {
                                                    xmpMeta.SetProperty(XMPConst.NS_XMP, PdfConst.ModifyDate, PdfDate.GetW3CDate(value));
                                                }
                                                else
                                                {
                                                    if (PdfName.Trapped.Equals(key))
                                                    {
                                                        xmpMeta.SetProperty(XMPConst.NS_PDF, PdfConst.Trapped, value);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <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();
            }
        }
        /// <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);
        }
        public virtual void TestPdfNamesFetching()
        {
            byte[][] namesBytes = new byte[][] { new byte[] { 35, 67, 51, 35, 57, 67, 98, 101, 114, 115, 99, 104, 114,
                                                              105, 102, 116, 95, 49 }, new byte[] { 35, 67, 51, 35, 57, 67, 98, 101, 114, 115, 99, 104, 114, 105, 102
                                                                                                    , 116, 95, 50 }, new byte[] { 65, 114, 116, 105, 99, 108, 101 }, new byte[] { 66, 105, 108, 100, 117,
                                                                                                                                                                                  110, 116, 101, 114, 115, 99, 104, 114, 105, 102, 116 }, new byte[] { 78, 111, 114, 109, 97, 108, 80, 97
                                                                                                                                                                                                                                                       , 114, 97, 103, 114, 97, 112, 104, 83, 116, 121, 108, 101 }, new byte[] { 83, 116, 111, 114, 121 }, new
                                                 byte[] { 84, 79, 67, 45, 49 }, new byte[] { 84, 79, 67, 45, 50, 45, 50 }, new byte[] { 84, 79, 67, 45,
                                                                                                                                        72, 101, 97, 100 }, new byte[] { 84, 97, 98, 101, 108, 108, 101 }, new byte[] { 84, 97, 98, 101, 108,
                                                                                                                                                                                                                        108, 101, 95, 72, 101, 97, 100 }, new byte[] { 84, 97, 98, 101, 108, 108, 101, 95, 102, 101, 116, 116 }
                                                 , new byte[] { 84, 101, 120, 116, 95, 73, 78, 70, 79 }, new byte[] { 84, 101, 120, 116, 95, 73, 110, 102
                                                                                                                      , 111, 95, 72, 101, 97, 100 }, new byte[] { 84, 101, 120, 116, 107, 35, 67, 51, 35, 66, 54, 114, 112,
                                                                                                                                                                  101, 114 }, new byte[] { 84, 101, 120, 116, 107, 35, 67, 51, 35, 66, 54, 114, 112, 101, 114, 45, 69, 114
                                                                                                                                                                                           , 115, 116, 122, 101, 105, 108, 101 }, new byte[] { 84, 101, 120, 116, 107, 35, 67, 51, 35, 66, 54, 114
                                                                                                                                                                                                                                               , 112, 101, 114, 95, 66, 97, 99, 107 }, new byte[] { 95, 78, 111, 95, 112, 97, 114, 97, 103, 114, 97,
                                                                                                                                                                                                                                                                                                    112, 104, 95, 115, 116, 121, 108, 101, 95 } };
            // /#C3#9Cberschrift_1
            // /#C3#9Cberschrift_2
            // /Article
            // /Bildunterschrift
            // /NormalParagraphStyle
            // /Story
            // /TOC-1
            // /TOC-2-2
            // /TOC-Head
            // /Tabelle
            // /Tabelle_Head
            // /Tabelle_fett
            // /Text_INFO
            // /Text_Info_Head
            // /Textk#C3#B6rper
            // /Textk#C3#B6rper-Erstzeile
            // /Textk#C3#B6rper_Back
            // /_No_paragraph_style_
            bool[] haveValue = new bool[] { true, true, false, true, true, true, false, false, false, false, false, false
                                            , false, false, false, false, false, false };
            IList <PdfName> names = new List <PdfName>();

            for (int i = 0; i < namesBytes.Length; i++)
            {
                byte[]  b = namesBytes[i];
                PdfName n = new PdfName(b);
                names.Add(n);
                if (haveValue[i])
                {
                    n.GenerateValue();
                }
            }
            PdfDictionary dict = new PdfDictionary();

            foreach (PdfName name in names)
            {
                dict.Put(name, new PdfName("dummy"));
            }
            PdfName expectedToContain = new PdfName("Article");
            bool    found             = false;

            foreach (PdfName pdfName in dict.KeySet())
            {
                found = pdfName.Equals(expectedToContain);
                if (found)
                {
                    break;
                }
            }
            NUnit.Framework.Assert.IsTrue(found);
            NUnit.Framework.Assert.IsTrue(dict.ContainsKey(expectedToContain));
        }