public virtual void TestEntrySetRemove2()
        {
            PdfDocument   doc  = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfDictionary dict = new PdfDictionary();

            dict.Put(new PdfName("1"), ((PdfNumber) new PdfNumber(1).MakeIndirect(doc)).GetIndirectReference());
            dict.Put(new PdfName("2"), ((PdfNumber) new PdfNumber(2).MakeIndirect(doc)).GetIndirectReference());
            dict.Put(new PdfName("3"), ((PdfNumber) new PdfNumber(3).MakeIndirect(doc)));
            dict.Put(new PdfName("4"), ((PdfNumber) new PdfNumber(4).MakeIndirect(doc)));
            dict.Put(new PdfName("5"), new PdfNumber(5));
            dict.Put(new PdfName("6"), new PdfNumber(6));
            PdfDictionary dict2 = new PdfDictionary();

            dict2.Put(new PdfName("1"), ((PdfNumber) new PdfNumber(1).MakeIndirect(doc)).GetIndirectReference());
            dict2.Put(new PdfName("2"), ((PdfNumber) new PdfNumber(2).MakeIndirect(doc)).GetIndirectReference());
            dict2.Put(new PdfName("3"), ((PdfNumber) new PdfNumber(3).MakeIndirect(doc)));
            dict2.Put(new PdfName("4"), ((PdfNumber) new PdfNumber(4).MakeIndirect(doc)));
            dict2.Put(new PdfName("5"), new PdfNumber(5));
            dict2.Put(new PdfName("6"), new PdfNumber(6));
            foreach (KeyValuePair <PdfName, PdfObject> e in dict2.EntrySet())
            {
                NUnit.Framework.Assert.IsTrue(dict.EntrySet().Remove(e));
            }
            NUnit.Framework.Assert.AreEqual(0, dict.EntrySet().Count);
            NUnit.Framework.Assert.AreEqual(0, dict.Values().Count);
            NUnit.Framework.Assert.AreEqual(0, dict.Size());
        }
Beispiel #2
0
        public virtual void TestEntrySetRetainAll()
        {
            PdfDocument   doc  = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfDictionary dict = new PdfDictionary();

            dict.Put(new PdfName("1"), new PdfNumber(1).MakeIndirect(doc).GetIndirectReference());
            dict.Put(new PdfName("2"), new PdfNumber(2).MakeIndirect(doc).GetIndirectReference());
            dict.Put(new PdfName("3"), new PdfNumber(3).MakeIndirect(doc));
            dict.Put(new PdfName("4"), new PdfNumber(4).MakeIndirect(doc));
            dict.Put(new PdfName("5"), new PdfNumber(5));
            dict.Put(new PdfName("6"), new PdfNumber(6));
            IList <KeyValuePair <PdfName, PdfObject> > toRemove = new List <KeyValuePair <PdfName, PdfObject> >();
            int i = 0;

            foreach (KeyValuePair <PdfName, PdfObject> e in dict.EntrySet())
            {
                toRemove.Add(e);
                if (i++ > 2)
                {
                    break;
                }
            }
            dict.EntrySet().RetainAll(toRemove);
            NUnit.Framework.Assert.AreEqual(4, dict.EntrySet().Count);
            NUnit.Framework.Assert.AreEqual(4, dict.Values().Count);
            NUnit.Framework.Assert.AreEqual(4, dict.Size());
        }
        public virtual void TestEntrySetClear()
        {
            PdfDocument   doc  = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfDictionary dict = new PdfDictionary();

            dict.Put(new PdfName("1"), ((PdfNumber) new PdfNumber(1).MakeIndirect(doc)).GetIndirectReference());
            dict.Put(new PdfName("2"), ((PdfNumber) new PdfNumber(2).MakeIndirect(doc)).GetIndirectReference());
            dict.Put(new PdfName("3"), ((PdfNumber) new PdfNumber(3).MakeIndirect(doc)));
            dict.Put(new PdfName("4"), ((PdfNumber) new PdfNumber(4).MakeIndirect(doc)));
            dict.Put(new PdfName("5"), new PdfNumber(5));
            dict.Put(new PdfName("6"), new PdfNumber(6));
            dict.EntrySet().Clear();
            NUnit.Framework.Assert.AreEqual(0, dict.EntrySet().Count);
            NUnit.Framework.Assert.AreEqual(0, dict.Values().Count);
            NUnit.Framework.Assert.AreEqual(0, dict.Size());
        }
Beispiel #4
0
        public virtual void FlushingPageResourcesMadeIndependent()
        {
            String      inputFile  = sourceFolder + "100pagesSharedResDict.pdf";
            String      outputFile = destinationFolder + "flushingPageResourcesMadeIndependent.pdf";
            PdfDocument pdf        = new PdfDocument(new PdfReader(inputFile), new PdfWriter(outputFile));
            int         numOfAddedXObjectsPerPage = 10;

            for (int i = 1; i <= pdf.GetNumberOfPages(); ++i)
            {
                PdfPage       sourcePage = pdf.GetPage(i);
                PdfDictionary res        = sourcePage.GetPdfObject().GetAsDictionary(PdfName.Resources);
                PdfDictionary resClone   = new PdfDictionary();
                // clone dictionary manually to ensure this object is direct and is flushed together with the page
                foreach (KeyValuePair <PdfName, PdfObject> e in res.EntrySet())
                {
                    resClone.Put(e.Key, e.Value.Clone());
                }
                sourcePage.GetPdfObject().Put(PdfName.Resources, resClone);
                PdfCanvas pdfCanvas = new PdfCanvas(sourcePage);
                pdfCanvas.SaveState();
                for (int j = 0; j < numOfAddedXObjectsPerPage; ++j)
                {
                    PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "simple.jpg"));
                    pdfCanvas.AddXObject(xObject, new Rectangle(36, 720 - j * 150, 20, 20));
                    xObject.MakeIndirect(pdf).Flush();
                }
                pdfCanvas.RestoreState();
                pdfCanvas.Release();
                sourcePage.Flush();
            }
            VerifyFlushedObjectsNum(pdf, 1416, 1400, 0);
            pdf.Close();
            PrintOutputPdfNameAndDir(outputFile);
            PdfDocument result    = new PdfDocument(new PdfReader(outputFile));
            PdfObject   page15Res = result.GetPage(15).GetPdfObject().Get(PdfName.Resources, false);
            PdfObject   page34Res = result.GetPage(34).GetPdfObject().Get(PdfName.Resources, false);

            NUnit.Framework.Assert.IsTrue(page15Res.IsDictionary());
            NUnit.Framework.Assert.AreEqual(numOfAddedXObjectsPerPage, ((PdfDictionary)page15Res).GetAsDictionary(PdfName
                                                                                                                  .XObject).Size());
            NUnit.Framework.Assert.IsTrue(page34Res.IsDictionary());
            NUnit.Framework.Assert.AreNotEqual(page15Res, page34Res);
            result.Close();
        }
        public virtual void TestEntrySet()
        {
            PdfDocument   doc  = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            PdfDictionary dict = new PdfDictionary();

            dict.Put(new PdfName("1"), ((PdfNumber) new PdfNumber(1).MakeIndirect(doc)).GetIndirectReference());
            dict.Put(new PdfName("2"), ((PdfNumber) new PdfNumber(2).MakeIndirect(doc)).GetIndirectReference());
            dict.Put(new PdfName("3"), ((PdfNumber) new PdfNumber(3).MakeIndirect(doc)));
            dict.Put(new PdfName("4"), ((PdfNumber) new PdfNumber(4).MakeIndirect(doc)));
            dict.Put(new PdfName("5"), new PdfNumber(5));
            dict.Put(new PdfName("6"), new PdfNumber(6));
            IList <int> nums = new List <int>(iText.IO.Util.JavaUtil.ArraysAsList(1, 2, 3, 4, 5, 6));

            foreach (KeyValuePair <PdfName, PdfObject> e in dict.EntrySet())
            {
                NUnit.Framework.Assert.AreEqual(e.Key.ToString(), "/" + e.Value);
                if (!nums.Remove(((PdfNumber)e.Value).IntValue()))
                {
                    NUnit.Framework.Assert.Fail("Element not found");
                }
            }
        }
Beispiel #6
0
 private void Write(PdfDictionary pdfDictionary)
 {
     WriteBytes(openDict);
     foreach (KeyValuePair <PdfName, PdfObject> entry in pdfDictionary.EntrySet())
     {
         bool isAlreadyWriteSpace = false;
         Write(entry.Key);
         PdfObject value = entry.Value;
         if (value == null)
         {
             ILogger logger = LoggerFactory.GetLogger(typeof(iText.Kernel.Pdf.PdfOutputStream));
             logger.Warn(String.Format(LogMessageConstant.INVALID_KEY_VALUE_KEY_0_HAS_NULL_VALUE, entry.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);
 }