Пример #1
0
        public virtual void CopyPagesWithOCGDifferentNames()
        {
            IList <IList <String> > ocgNames  = new List <IList <String> >();
            IList <String>          ocgNames1 = new List <String>();

            ocgNames1.Add("Name1");
            IList <String> ocgNames2 = new List <String>();

            ocgNames2.Add("Name2");
            ocgNames.Add(ocgNames1);
            ocgNames.Add(ocgNames2);
            IList <byte[]> sourceDocuments = PdfDocumentUnitTest.InitSourceDocuments(ocgNames);

            using (PdfDocument outDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))) {
                foreach (byte[] docBytes in sourceDocuments)
                {
                    using (PdfDocument fromDocument = new PdfDocument(new PdfReader(new MemoryStream(docBytes)))) {
                        for (int i = 1; i <= fromDocument.GetNumberOfPages(); i++)
                        {
                            fromDocument.CopyPagesTo(i, i, outDocument);
                        }
                    }
                }
                IList <String> layerNames = new List <String>();
                layerNames.Add("Name1");
                layerNames.Add("Name2");
                PdfDocumentUnitTest.AssertLayerNames(outDocument, layerNames);
            }
        }
Пример #2
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);
     }
 }
Пример #3
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());
                }
            }
        }