Пример #1
0
        public virtual void CopyObject7()
        {
            String      exceptionMessage = null;
            PdfDocument pdfDoc1          = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_1.pdf"));
            PdfDocument pdfDoc2          = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_2.pdf"));

            try {
                PdfPage       page1        = pdfDoc1.AddNewPage();
                PdfDictionary directDict   = new PdfDictionary();
                PdfObject     indirectDict = new PdfDictionary().MakeIndirect(pdfDoc1);
                page1.GetPdfObject().Put(new PdfName("HelloWorldDirect"), directDict);
                page1.GetPdfObject().Put(new PdfName("HelloWorldIndirect"), indirectDict);
                PdfPage page2 = pdfDoc2.AddNewPage();
                page2.GetPdfObject().Put(new PdfName("HelloWorldDirect"), directDict.CopyTo(pdfDoc2));
                page2.GetPdfObject().Put(new PdfName("HelloWorldIndirect"), indirectDict.CopyTo(pdfDoc2));
            }
            catch (PdfException ex) {
                exceptionMessage = ex.Message;
            }
            finally {
                pdfDoc1.Close();
                pdfDoc2.Close();
            }
            NUnit.Framework.Assert.AreEqual(exceptionMessage, PdfException.CannotCopyIndirectObjectFromTheDocumentThatIsBeingWritten
                                            );
        }
Пример #2
0
        public virtual void CopyObject6()
        {
            FileStream    fos        = new FileStream(destinationFolder + "copyObject6_1.pdf", FileMode.Create);
            PdfWriter     writer     = new PdfWriter(fos);
            PdfDocument   pdfDoc     = new PdfDocument(writer);
            PdfDictionary helloWorld = ((PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc));

            helloWorld.Put(new PdfName("Hello"), new PdfString("World"));
            PdfPage page = pdfDoc.AddNewPage();

            page.GetPdfObject().Put(new PdfName("HelloWorld"), helloWorld);
            pdfDoc.Close();
            pdfDoc     = new PdfDocument(new PdfReader(destinationFolder + "copyObject6_1.pdf"));
            helloWorld = (PdfDictionary)pdfDoc.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorld"));
            PdfDocument pdfDoc1 = new PdfDocument(new PdfWriter(new FileStream(destinationFolder + "copyObject6_2.pdf"
                                                                               , FileMode.Create)));
            PdfPage page1 = pdfDoc1.AddNewPage();

            page1.GetPdfObject().Put(new PdfName("HelloWorldCopy1"), ((PdfDictionary)helloWorld.CopyTo(pdfDoc1)));
            page1.GetPdfObject().Put(new PdfName("HelloWorldCopy2"), ((PdfDictionary)helloWorld.CopyTo(pdfDoc1, true))
                                     );
            page1.GetPdfObject().Put(new PdfName("HelloWorldCopy3"), ((PdfDictionary)helloWorld.CopyTo(pdfDoc1, false)
                                                                      ));
            page1.Flush();
            pdfDoc.Close();
            pdfDoc1.Close();
            PdfReader   reader      = new PdfReader(destinationFolder + "copyObject6_2.pdf");
            PdfDocument pdfDocument = new PdfDocument(reader);

            NUnit.Framework.Assert.AreEqual(false, reader.HasRebuiltXref(), "Rebuilt");
            PdfObject            obj1 = pdfDocument.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorldCopy1"));
            PdfIndirectReference ref1 = obj1.GetIndirectReference();

            NUnit.Framework.Assert.AreEqual(6, ref1.objNr);
            NUnit.Framework.Assert.AreEqual(0, ref1.genNr);
            PdfObject            obj2 = pdfDocument.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorldCopy2"));
            PdfIndirectReference ref2 = obj2.GetIndirectReference();

            NUnit.Framework.Assert.AreEqual(7, ref2.GetObjNumber());
            NUnit.Framework.Assert.AreEqual(0, ref2.GetGenNumber());
            PdfObject            obj3 = pdfDocument.GetPage(1).GetPdfObject().Get(new PdfName("HelloWorldCopy3"));
            PdfIndirectReference ref3 = obj3.GetIndirectReference();

            NUnit.Framework.Assert.AreEqual(7, ref3.GetObjNumber());
            NUnit.Framework.Assert.AreEqual(0, ref3.GetGenNumber());
            reader.Close();
        }
Пример #3
0
        public virtual void CopyObject8()
        {
            String      exceptionMessage = null;
            PdfDocument pdfDoc           = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_1.pdf"));

            try {
                PdfPage       page1        = pdfDoc.AddNewPage();
                PdfDictionary directDict   = new PdfDictionary();
                PdfObject     indirectDict = new PdfDictionary().MakeIndirect(pdfDoc);
                page1.GetPdfObject().Put(new PdfName("HelloWorldDirect"), directDict);
                page1.GetPdfObject().Put(new PdfName("HelloWorldIndirect"), indirectDict);
                indirectDict.CopyTo(null);
            }
            catch (PdfException ex) {
                exceptionMessage = ex.Message;
            }
            finally {
                pdfDoc.Close();
            }
            NUnit.Framework.Assert.AreEqual(exceptionMessage, PdfException.DocumentForCopyToCannotBeNull);
        }
        public virtual void SmartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh()
        {
            String outFile = destinationFolder + "smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf";
            String cmpFile = sourceFolder + "cmp_smartModeSameResourcesCopyingModifyingAndFlushing_ensureObjectFresh.pdf";

            String[]    srcFiles    = new String[] { sourceFolder + "indirectResourcesStructure.pdf", sourceFolder + "indirectResourcesStructure2.pdf" };
            PdfDocument outputDoc   = new PdfDocument(new PdfWriter(outFile, new WriterProperties().UseSmartMode()));
            int         lastPageNum = 1;
            PdfFont     font        = PdfFontFactory.CreateFont();

            foreach (String srcFile in srcFiles)
            {
                PdfDocument sourceDoc = new PdfDocument(new PdfReader(srcFile));
                for (int i = 1; i <= sourceDoc.GetNumberOfPages(); ++i)
                {
                    PdfDictionary srcRes = sourceDoc.GetPage(i).GetPdfObject().GetAsDictionary(PdfName.Resources);
                    // Ensures that objects copied to the output document are fresh,
                    // i.e. are not reused from already copied objects cache.
                    bool ensureObjectIsFresh = true;
                    // it's crucial to copy first inner objects and then the container object!
                    foreach (PdfObject v in srcRes.Values())
                    {
                        if (v.GetIndirectReference() != null)
                        {
                            // We are not interested in returned copied objects instances, they will be picked up by
                            // general copying mechanism from copied objects cache by default.
                            v.CopyTo(outputDoc, ensureObjectIsFresh);
                        }
                    }
                    if (srcRes.GetIndirectReference() != null)
                    {
                        srcRes.CopyTo(outputDoc, ensureObjectIsFresh);
                    }
                }
                sourceDoc.CopyPagesTo(1, sourceDoc.GetNumberOfPages(), outputDoc);
                sourceDoc.Close();
                int i_1;
                for (i_1 = lastPageNum; i_1 <= outputDoc.GetNumberOfPages(); ++i_1)
                {
                    PdfPage   page   = outputDoc.GetPage(i_1);
                    PdfCanvas canvas = new PdfCanvas(page);
                    canvas.BeginText().MoveText(36, 36).SetFontAndSize(font, 12).ShowText("Page " + i_1).EndText();
                }
                lastPageNum = i_1;
                outputDoc.FlushCopiedObjects(sourceDoc);
            }
            outputDoc.Close();
            PdfDocument          assertDoc       = new PdfDocument(new PdfReader(outFile));
            PdfIndirectReference page1ResFontObj = assertDoc.GetPage(1).GetPdfObject().GetAsDictionary(PdfName.Resources
                                                                                                       ).GetAsDictionary(PdfName.Font).GetIndirectReference();
            PdfIndirectReference page2ResFontObj = assertDoc.GetPage(2).GetPdfObject().GetAsDictionary(PdfName.Resources
                                                                                                       ).GetAsDictionary(PdfName.Font).GetIndirectReference();
            PdfIndirectReference page3ResFontObj = assertDoc.GetPage(3).GetPdfObject().GetAsDictionary(PdfName.Resources
                                                                                                       ).GetAsDictionary(PdfName.Font).GetIndirectReference();

            NUnit.Framework.Assert.IsFalse(page1ResFontObj.Equals(page2ResFontObj));
            NUnit.Framework.Assert.IsFalse(page1ResFontObj.Equals(page3ResFontObj));
            NUnit.Framework.Assert.IsFalse(page2ResFontObj.Equals(page3ResFontObj));
            assertDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFile, cmpFile, destinationFolder));
        }