Exemplo n.º 1
0
        public virtual void SetLinkDestinationToPageAppendMode()
        {
            String      input  = sourceFolder + "100pages.pdf";
            String      output = destinationFolder + "setLinkDestinationToPageAppendMode.pdf";
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(output), new StampingProperties()
                                                 .UseAppendMode());
            PdfPage page1 = pdfDoc.GetPage(1);
            PdfPage page2 = pdfDoc.GetPage(2);
            PdfIndirectReference page1IndRef = page1.GetPdfObject().GetIndirectReference();
            PdfIndirectReference page2IndRef = page2.GetPdfObject().GetIndirectReference();
            PdfDictionary        aDict       = ((PdfLinkAnnotation)page1.GetAnnotations()[0]).GetAction();

            new PdfAction(aDict).Put(PdfName.D, PdfExplicitDestination.CreateXYZ(page2, 300, 400, 1).GetPdfObject());
            PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc);

            flushingHelper.AppendModeFlush(2);
            flushingHelper.UnsafeFlushDeep(1);
            // annotation is flushed
            NUnit.Framework.Assert.IsTrue(aDict.IsFlushed());
            // page is not flushed
            NUnit.Framework.Assert.IsFalse(page1IndRef.CheckState(PdfObject.FLUSHED));
            // page is released
            NUnit.Framework.Assert.IsNull(page1IndRef.refersTo);
            // page is not flushed
            NUnit.Framework.Assert.IsFalse(page2IndRef.CheckState(PdfObject.FLUSHED));
            // page is released
            NUnit.Framework.Assert.IsNull(page2IndRef.refersTo);
            // exception is not thrown
            pdfDoc.Close();
        }
Exemplo n.º 2
0
        public virtual void ModifyAnnotationOnlyAppendMode()
        {
            String      input  = sourceFolder + "100pages.pdf";
            String      output = destinationFolder + "modifyAnnotOnly.pdf";
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(output), new StampingProperties()
                                                 .UseAppendMode());
            PdfPage page = pdfDoc.GetPage(1);
            PdfIndirectReference pageIndRef = page.GetPdfObject().GetIndirectReference();
            PdfDictionary        annotObj   = page.GetAnnotations()[0].SetRectangle(new PdfArray(new Rectangle(0, 0, 300, 300))
                                                                                    ).SetPage(page).GetPdfObject();
            PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc);

            flushingHelper.AppendModeFlush(1);
            // annotation is flushed
            NUnit.Framework.Assert.IsTrue(annotObj.IsFlushed());
            // page is not flushed
            NUnit.Framework.Assert.IsFalse(pageIndRef.CheckState(PdfObject.FLUSHED));
            // page is released
            NUnit.Framework.Assert.IsNull(pageIndRef.refersTo);
            // exception is not thrown
            pdfDoc.Close();
        }
Exemplo n.º 3
0
        public virtual void FlushSelfContainingObjectsWritingMode()
        {
            PdfDocument   pdfDoc   = new PdfDocument(new PdfWriter(new MemoryStream()));
            PdfDictionary pageDict = pdfDoc.AddNewPage().GetPdfObject();
            PdfDictionary dict1    = new PdfDictionary();

            pageDict.Put(new PdfName("dict1"), dict1);
            PdfArray arr1 = new PdfArray();

            pageDict.Put(new PdfName("arr1"), arr1);
            dict1.Put(new PdfName("dict1"), dict1);
            dict1.Put(new PdfName("arr1"), arr1);
            arr1.Add(arr1);
            arr1.Add(dict1);
            arr1.MakeIndirect(pdfDoc);
            dict1.MakeIndirect(pdfDoc);
            PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc);

            flushingHelper.UnsafeFlushDeep(1);
            NUnit.Framework.Assert.IsTrue(dict1.IsFlushed());
            NUnit.Framework.Assert.IsTrue(arr1.IsFlushed());
            pdfDoc.Close();
        }
Exemplo n.º 4
0
        private static void Test(String filename, PageFlushingTest.DocMode docMode, PageFlushingTest.FlushMode flushMode
                                 , PageFlushingTest.PagesOp pagesOp, int total, int flushedExpected, int notReadExpected)
        {
            String      input  = sourceFolder + "100pages.pdf";
            String      output = destinationFolder + filename;
            PdfDocument pdfDoc;

            switch (docMode)
            {
            case PageFlushingTest.DocMode.WRITING: {
                pdfDoc = new PdfDocument(new PdfWriter(output));
                break;
            }

            case PageFlushingTest.DocMode.READING: {
                pdfDoc = new PdfDocument(new PdfReader(input));
                break;
            }

            case PageFlushingTest.DocMode.STAMPING: {
                pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(output));
                break;
            }

            case PageFlushingTest.DocMode.APPEND: {
                pdfDoc = new PdfDocument(new PdfReader(input), new PdfWriter(output), new StampingProperties().UseAppendMode
                                             ());
                break;
            }

            default: {
                throw new InvalidOperationException();
            }
            }
            PageFlushingHelper flushingHelper = new PageFlushingHelper(pdfDoc);
            PdfFont            font           = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            PdfImageXObject    xObject        = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "itext.png"));

            if (docMode != PageFlushingTest.DocMode.WRITING)
            {
                for (int i = 0; i < 100; ++i)
                {
                    PdfPage page = pdfDoc.GetPage(i + 1);
                    switch (pagesOp)
                    {
                    case PageFlushingTest.PagesOp.READ: {
                        PdfTextExtractor.GetTextFromPage(page);
                        break;
                    }

                    case PageFlushingTest.PagesOp.MODIFY: {
                        AddContentToPage(page, font, xObject);
                        break;
                    }

                    case PageFlushingTest.PagesOp.MODIFY_LIGHTLY: {
                        AddBasicContent(page, font);
                        break;
                    }
                    }
                    switch (flushMode)
                    {
                    case PageFlushingTest.FlushMode.UNSAFE_DEEP: {
                        flushingHelper.UnsafeFlushDeep(i + 1);
                        break;
                    }

                    case PageFlushingTest.FlushMode.RELEASE_DEEP: {
                        flushingHelper.ReleaseDeep(i + 1);
                        break;
                    }

                    case PageFlushingTest.FlushMode.APPEND_MODE: {
                        flushingHelper.AppendModeFlush(i + 1);
                        break;
                    }

                    case PageFlushingTest.FlushMode.PAGE_FLUSH: {
                        page.Flush();
                        break;
                    }
                    }
                }
            }
            if (docMode != PageFlushingTest.DocMode.READING && pagesOp == PageFlushingTest.PagesOp.MODIFY)
            {
                for (int i = 0; i < 100; ++i)
                {
                    PdfPage page = pdfDoc.AddNewPage();
                    AddContentToPage(page, font, xObject);
                    switch (flushMode)
                    {
                    case PageFlushingTest.FlushMode.UNSAFE_DEEP: {
                        flushingHelper.UnsafeFlushDeep(pdfDoc.GetNumberOfPages());
                        break;
                    }

                    case PageFlushingTest.FlushMode.RELEASE_DEEP: {
                        flushingHelper.ReleaseDeep(pdfDoc.GetNumberOfPages());
                        break;
                    }

                    case PageFlushingTest.FlushMode.APPEND_MODE: {
                        flushingHelper.AppendModeFlush(pdfDoc.GetNumberOfPages());
                        break;
                    }

                    case PageFlushingTest.FlushMode.PAGE_FLUSH: {
                        page.Flush();
                        break;
                    }
                    }
                }
            }
            VerifyFlushedObjectsNum(pdfDoc, total, flushedExpected, notReadExpected);
            pdfDoc.Close();
        }