Exemplo n.º 1
0
        public virtual void ResourcesTest2()
        {
            MemoryStream baos      = new MemoryStream();
            PdfWriter    writer    = new PdfWriter(baos);
            PdfDocument  document  = new PdfDocument(writer);
            PdfPage      page      = document.AddNewPage();
            PdfExtGState egs1      = new PdfExtGState();
            PdfExtGState egs2      = new PdfExtGState();
            PdfResources resources = page.GetResources();

            resources.AddExtGState(egs1);
            resources.AddExtGState(egs2);
            document.Close();
            PdfReader reader = new PdfReader(new MemoryStream(baos.ToArray()));

            document  = new PdfDocument(reader, new PdfWriter(new ByteArrayOutputStream()));
            page      = document.GetPage(1);
            resources = page.GetResources();
            ICollection <PdfName> names = resources.GetResourceNames();

            NUnit.Framework.Assert.AreEqual(2, names.Count);
            String[] expectedNames = new String[] { "Gs1", "Gs2" };
            int      i             = 0;

            foreach (PdfName name in names)
            {
                NUnit.Framework.Assert.AreEqual(expectedNames[i++], name.GetValue());
            }
            PdfExtGState egs3 = new PdfExtGState();
            PdfName      n3   = resources.AddExtGState(egs3);

            NUnit.Framework.Assert.AreEqual("Gs3", n3.GetValue());
            PdfDictionary egsResources = page.GetPdfObject().GetAsDictionary(PdfName.Resources).GetAsDictionary(PdfName
                                                                                                                .ExtGState);
            PdfObject e1 = egsResources.Get(new PdfName("Gs1"), false);
            PdfName   n1 = resources.AddExtGState(e1);

            NUnit.Framework.Assert.AreEqual("Gs1", n1.GetValue());
            PdfObject e2 = egsResources.Get(new PdfName("Gs2"));
            PdfName   n2 = resources.AddExtGState(e2);

            NUnit.Framework.Assert.AreEqual("Gs2", n2.GetValue());
            PdfObject e4 = (PdfObject)e2.Clone();
            PdfName   n4 = resources.AddExtGState(e4);

            NUnit.Framework.Assert.AreEqual("Gs4", n4.GetValue());
            document.Close();
        }
Exemplo n.º 2
0
        public virtual void ReadFormXObjectsWithCircularReferencesInResources()
        {
            // given input file contains circular reference in resources of form xobjects
            // (form xobjects are nested inside each other)
            String                 input        = sourceFolder + "circularReferencesInResources.pdf";
            PdfReader              reader1      = new PdfReader(input);
            PdfDocument            inputPdfDoc1 = new PdfDocument(reader1);
            PdfPage                page         = inputPdfDoc1.GetPage(1);
            PdfResources           resources    = page.GetResources();
            IList <PdfFormXObject> formXObjects = new List <PdfFormXObject>();

            // We just try to work with resources in arbitrary way and make sure that circular reference
            // doesn't block it. However it is expected that PdfResources doesn't try to "look in deep"
            // and recursively resolves resources, so this test should never meet any issues.
            foreach (PdfName xObjName in resources.GetResourceNames(PdfName.XObject))
            {
                PdfFormXObject form = resources.GetForm(xObjName);
                if (form != null)
                {
                    formXObjects.Add(form);
                }
            }
            // ensure resources XObject entry is read correctly
            NUnit.Framework.Assert.AreEqual(2, formXObjects.Count);
        }
Exemplo n.º 3
0
        public virtual void PdfUncoloredPatternColorSize1Test()
        {
            PdfDocument   pdfDocument       = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
            String        contentColorSpace = "/Cs1 cs\n";
            PdfDictionary pageDictionary    = (PdfDictionary) new PdfDictionary().MakeIndirect(pdfDocument);
            PdfStream     contentStream     = new PdfStream(contentColorSpace.GetBytes());

            pageDictionary.Put(PdfName.Contents, contentStream);
            PdfPage page = pdfDocument.AddNewPage();

            page.GetPdfObject().Put(PdfName.Contents, contentStream);
            PdfArray pdfArray = new PdfArray();

            pdfArray.Add(PdfName.Pattern);
            PdfColorSpace space = PdfColorSpace.MakeColorSpace(pdfArray);

            page.GetResources().AddColorSpace(space);
            Rectangle rectangle = new Rectangle(50, 50, 1000, 1000);

            page.SetMediaBox(rectangle);
            PdfCanvasProcessor processor = new PdfCanvasProcessor(new PdfArrayTest.NoOpListener());

            processor.ProcessPageContent(page);
            // Check if we reach the end of the test without failings together with verifying expected color space instance
            NUnit.Framework.Assert.IsTrue(processor.GetGraphicsState().GetFillColor().GetColorSpace() is PdfSpecialCs.Pattern
                                          );
        }
Exemplo n.º 4
0
 public virtual void CopyPagesWithOCGSameObject()
 {
     byte[] docBytes;
     using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
         using (PdfDocument document = new PdfDocument(new PdfWriter(outputStream))) {
             PdfPage       page        = document.AddNewPage();
             PdfResources  pdfResource = page.GetResources();
             PdfDictionary ocg         = new PdfDictionary();
             ocg.Put(PdfName.Type, PdfName.OCG);
             ocg.Put(PdfName.Name, new PdfString("name1"));
             ocg.MakeIndirect(document);
             pdfResource.AddProperties(ocg);
             PdfPage      page2        = document.AddNewPage();
             PdfResources pdfResource2 = page2.GetResources();
             pdfResource2.AddProperties(ocg);
             document.GetCatalog().GetOCProperties(true);
         }
         docBytes = outputStream.ToArray();
     }
     using (PdfDocument outDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) {
         using (PdfDocument fromDocument = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
             fromDocument.CopyPagesTo(1, fromDocument.GetNumberOfPages(), outDocument);
         }
         IList <String> layerNames = new List <String>();
         layerNames.Add("name1");
         PdfDocumentUnitTest.AssertLayerNames(outDocument, layerNames);
     }
 }
Exemplo n.º 5
0
        /// <exception cref="iText.Kernel.XMP.XMPException"/>
        /// <exception cref="System.IO.IOException"/>
        /// <exception cref="System.Exception"/>
        public virtual void EncryptWithPassword(String filename, int encryptionType, int compression)
        {
            String    outFileName = destinationFolder + filename;
            int       permissions = EncryptionConstants.ALLOW_SCREENREADERS;
            PdfWriter writer      = new PdfWriter(new FileStream(outFileName, FileMode.Create), new WriterProperties().SetStandardEncryption
                                                      (USER, OWNER, permissions, encryptionType).AddXmpMetadata());

            writer.SetCompressionLevel(compression);
            PdfDocument document = new PdfDocument(writer);

            document.GetDocumentInfo().SetAuthor(author).SetCreator(creator);
            PdfPage page = document.AddNewPage();

            page.GetFirstContentStream().GetOutputStream().WriteBytes(("q\n" + "BT\n" + "36 706 Td\n" + "0 0 Td\n" + "/F1 24 Tf\n"
                                                                       + "(Hello world!)Tj\n" + "0 0 Td\n" + "ET\n" + "Q ").GetBytes());
            page.GetResources().AddFont(document, PdfFontFactory.CreateFont(FontConstants.HELVETICA));
            page.Flush();
            document.Close();
            CompareTool compareTool   = new CompareTool().EnableEncryptionCompare();
            String      compareResult = compareTool.CompareByContent(outFileName, sourceFolder + "cmp_" + filename, destinationFolder
                                                                     , "diff_", USER, USER);

            if (compareResult != null)
            {
                NUnit.Framework.Assert.Fail(compareResult);
            }
            CheckDecryptedContent(filename, OWNER, "(Hello world!)");
            CheckDecryptedContent(filename, USER, "(Hello world!)");
            CheckDocumentStamping(filename, OWNER);
            CheckDocumentAppending(filename, OWNER);
        }
Exemplo n.º 6
0
        public virtual void TestInheritedResources()
        {
            PdfDocument   pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "simpleInheritedResources.pdf"));
            PdfPage       page        = pdfDocument.GetPage(1);
            PdfDictionary dict        = page.GetResources().GetResource(PdfName.ExtGState);

            NUnit.Framework.Assert.AreEqual(2, dict.Size());
            PdfExtGState gState = new PdfExtGState((PdfDictionary)dict.Get(new PdfName("Gs1")));

            NUnit.Framework.Assert.AreEqual(10, gState.GetLineWidth());
        }
Exemplo n.º 7
0
        /// <exception cref="System.IO.IOException"/>
        private int CountPdfFonts(String filename)
        {
            PdfReader   reader = new PdfReader(filename);
            PdfDocument pdfDoc = new PdfDocument(reader);
            ICollection <PdfIndirectReference> fonts = new HashSet <PdfIndirectReference>();

            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
            {
                PdfPage page = pdfDoc.GetPage(i);
                foreach (PdfObject value in page.GetResources().GetResource(PdfName.Font).Values())
                {
                    fonts.Add(value.GetIndirectReference());
                }
            }
            return(fonts.Count);
        }
Exemplo n.º 8
0
        public virtual void ResourcesTest1()
        {
            PdfDocument  document  = new PdfDocument(new PdfWriter(new MemoryStream()));
            PdfPage      page      = document.AddNewPage();
            PdfExtGState egs1      = new PdfExtGState();
            PdfExtGState egs2      = new PdfExtGState();
            PdfResources resources = page.GetResources();
            PdfName      n1        = resources.AddExtGState(egs1);

            NUnit.Framework.Assert.AreEqual("Gs1", n1.GetValue());
            PdfName n2 = resources.AddExtGState(egs2);

            NUnit.Framework.Assert.AreEqual("Gs2", n2.GetValue());
            n1 = resources.AddExtGState(egs1);
            NUnit.Framework.Assert.AreEqual("Gs1", n1.GetValue());
            document.Close();
        }
Exemplo n.º 9
0
 private static byte[] InitDocument(IList <String> names)
 {
     using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
         using (PdfDocument document = new PdfDocument(new PdfWriter(outputStream))) {
             PdfPage      page        = document.AddNewPage();
             PdfResources pdfResource = page.GetResources();
             foreach (String name in names)
             {
                 PdfDictionary ocg = new PdfDictionary();
                 ocg.Put(PdfName.Type, PdfName.OCG);
                 ocg.Put(PdfName.Name, new PdfString(name));
                 ocg.MakeIndirect(document);
                 pdfResource.AddProperties(ocg);
             }
             document.GetCatalog().GetOCProperties(true);
         }
         return(outputStream.ToArray());
     }
 }
Exemplo n.º 10
0
        public virtual void CopyPagesFlushedResources()
        {
            byte[] docBytes;
            using (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
                using (PdfDocument document = new PdfDocument(new PdfWriter(outputStream))) {
                    PdfPage       page        = document.AddNewPage();
                    PdfResources  pdfResource = page.GetResources();
                    PdfDictionary ocg         = new PdfDictionary();
                    ocg.Put(PdfName.Type, PdfName.OCG);
                    ocg.Put(PdfName.Name, new PdfString("name1"));
                    ocg.MakeIndirect(document);
                    pdfResource.AddProperties(ocg);
                    pdfResource.MakeIndirect(document);
                    PdfPage page2 = document.AddNewPage();
                    page2.SetResources(pdfResource);
                    document.GetCatalog().GetOCProperties(true);
                }
                docBytes = outputStream.ToArray();
            }
            PdfWriter writer = new PdfWriter(new ByteArrayOutputStream());

            using (PdfDocument outDocument = new PdfDocument(writer)) {
                using (PdfDocument fromDocument = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
                    fromDocument.CopyPagesTo(1, 1, outDocument);
                    IList <String> layerNames = new List <String>();
                    layerNames.Add("name1");
                    PdfDocumentUnitTest.AssertLayerNames(outDocument, layerNames);
                    outDocument.FlushCopiedObjects(fromDocument);
                    fromDocument.CopyPagesTo(2, 2, outDocument);
                    NUnit.Framework.Assert.IsNotNull(outDocument.GetCatalog());
                    PdfOCProperties ocProperties = outDocument.GetCatalog().GetOCProperties(false);
                    NUnit.Framework.Assert.IsNotNull(ocProperties);
                    NUnit.Framework.Assert.AreEqual(1, ocProperties.GetLayers().Count);
                    PdfLayer layer = ocProperties.GetLayers()[0];
                    NUnit.Framework.Assert.IsTrue(layer.GetPdfObject().IsFlushed());
                }
            }
        }
        private bool FlushPage(int pageNum)
        {
            PdfPage page = pdfDoc.GetPage(pageNum);

            if (page.IsFlushed())
            {
                return(false);
            }
            bool pageChanged = false;

            if (!release)
            {
                pdfDoc.DispatchEvent(new PdfDocumentEvent(PdfDocumentEvent.END_PAGE, page));
                InitCurrentLayers(pdfDoc);
            }
            PdfDictionary pageDict = page.GetPdfObject();
            // Using PdfPage package internal methods in order to avoid PdfResources initialization: initializing PdfResources
            // limits processing possibilities only to cases in which resources and specific resource type dictionaries are not flushed.
            PdfDictionary resourcesDict = page.InitResources(false);
            // inits /Resources dict entry if not inherited and not created yet
            PdfResources resources = page.GetResources(false);

            if (resources != null && resources.IsModified() && !resources.IsReadOnly())
            {
                resourcesDict = resources.GetPdfObject();
                pageDict.Put(PdfName.Resources, resources.GetPdfObject());
                pageDict.SetModified();
                pageChanged = true;
            }
            if (!resourcesDict.IsFlushed())
            {
                FlushDictRecursively(resourcesDict, null);
                FlushOrRelease(resourcesDict);
            }
            FlushDictRecursively(pageDict, pageContext);
            if (release)
            {
                if (!page.GetPdfObject().IsModified())
                {
                    pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum);
                    page.UnsetForbidRelease();
                    page.GetPdfObject().Release();
                }
            }
            else
            {
                if (pdfDoc.IsTagged() && !pdfDoc.GetStructTreeRoot().IsFlushed())
                {
                    page.TryFlushPageTags();
                }
                if (!pdfDoc.IsAppendMode() || page.GetPdfObject().IsModified())
                {
                    page.ReleaseInstanceFields();
                    page.GetPdfObject().Flush();
                }
                else
                {
                    // it's append mode
                    pdfDoc.GetCatalog().GetPageTree().ReleasePage(pageNum);
                    page.UnsetForbidRelease();
                    page.GetPdfObject().Release();
                }
            }
            layersRefs.Clear();
            return(pageChanged);
        }
Exemplo n.º 12
0
 /// <exception cref="System.IO.IOException"/>
 private void WriteTextBytesOnPageContent(PdfPage page, String text)
 {
     page.GetFirstContentStream().GetOutputStream().WriteBytes(("q\n" + "BT\n" + "36 706 Td\n" + "0 0 Td\n" + "/F1 24 Tf\n"
                                                                + "(" + text + ")Tj\n" + "0 0 Td\n" + "ET\n" + "Q ").GetBytes(iText.IO.Util.EncodingUtil.ISO_8859_1));
     page.GetResources().AddFont(page.GetDocument(), PdfFontFactory.CreateFont(FontConstants.HELVETICA));
 }