Пример #1
0
        public virtual void AddAssociatedFilesTest02()
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "add_associated_files02.pdf",
                                                                    new WriterProperties().SetPdfVersion(PdfVersion.PDF_2_0)));

            pdfDocument.SetTagged();
            PdfCanvas       pageCanvas   = new PdfCanvas(pdfDocument.AddNewPage());
            PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "berlin2013.jpg"
                                                                                       ));

            imageXObject.AddAssociatedFile(PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "Associated File 1".GetBytes
                                                                                  (), "af_1.txt", PdfName.Data));
            pageCanvas.AddXObject(imageXObject, 40, 400);
            PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(200, 200));
            PdfCanvas      formCanvas  = new PdfCanvas(formXObject, pdfDocument);

            formCanvas.SaveState().Circle(100, 100, 50).SetColor(ColorConstants.BLACK, true).Fill().RestoreState();
            formCanvas.Release();
            formXObject.AddAssociatedFile(PdfFileSpec.CreateEmbeddedFileSpec(pdfDocument, "Associated File 2".GetBytes
                                                                                 (), "af_2.txt", PdfName.Data));
            pageCanvas.AddXObject(formXObject, 40, 100);
            pdfDocument.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + "add_associated_files02.pdf"
                                                                             , sourceFolder + "cmp_add_associated_files02.pdf", "d:/", "diff_"));
        }
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf       = new PdfDocument(new PdfWriter(dest));
            PdfDocument sourcePdf = new PdfDocument(new PdfReader(SRC));
            //Original page
            PdfPage origPage = sourcePdf.GetPage(1);
            //Original page size
            Rectangle      orig     = origPage.GetPageSize();
            PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
            //N-up page
            PageSize  nUpPageSize = PageSize.A4.Rotate();
            PdfPage   page        = pdf.AddNewPage(nUpPageSize);
            PdfCanvas canvas      = new PdfCanvas(page);
            //Scale page
            AffineTransform transformationMatrix = AffineTransform.GetScaleInstance(nUpPageSize.GetWidth() / orig.GetWidth
                                                                                        () / 2f, nUpPageSize.GetHeight() / orig.GetHeight() / 2f);

            canvas.ConcatMatrix(transformationMatrix);
            //Add pages to N-up page
            canvas.AddXObject(pageCopy, 0, orig.GetHeight());
            canvas.AddXObject(pageCopy, orig.GetWidth(), orig.GetHeight());
            canvas.AddXObject(pageCopy, 0, 0);
            canvas.AddXObject(pageCopy, orig.GetWidth(), 0);
            pdf.Close();
            sourcePdf.Close();
        }
Пример #3
0
        public virtual void MacroPDF417Test01()
        {
            String      filename    = "barcode417Macro_01.pdf";
            PdfWriter   writer      = new PdfWriter(destinationFolder + filename);
            PdfDocument pdfDocument = new PdfDocument(writer);
            PdfCanvas   pdfCanvas   = new PdfCanvas(pdfDocument.AddNewPage());

            pdfCanvas.AddXObject(CreateMacroBarcodePart(pdfDocument, "This is PDF417 segment 0", 1, 1, 0), 1, 0, 0, 1,
                                 36, 791);
            pdfCanvas.AddXObject(CreateMacroBarcodePart(pdfDocument, "This is PDF417 segment 1", 1, 1, 1), 1, 0, 0, 1,
                                 36, 676);
            pdfDocument.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder
                                                                             + "cmp_" + filename, destinationFolder, "diff_"));
        }
 public virtual void NestedXObjectWithTransparencyTest()
 {
     NUnit.Framework.Assert.That(() => {
         PdfWriter writer        = new PdfWriter(new MemoryStream());
         PdfDocument pdfDocument = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_3B, null);
         PdfFormXObject form1    = new PdfFormXObject(new Rectangle(0, 0, 50, 50));
         PdfCanvas canvas1       = new PdfCanvas(form1, pdfDocument);
         canvas1.SaveState();
         PdfExtGState state = new PdfExtGState();
         state.SetFillOpacity(0.6f);
         canvas1.SetExtGState(state);
         canvas1.Circle(25, 25, 10);
         canvas1.Fill();
         canvas1.RestoreState();
         canvas1.Release();
         form1.Flush();
         //Create form XObject and flush to document.
         PdfFormXObject form = new PdfFormXObject(new Rectangle(0, 0, 50, 50));
         PdfCanvas canvas    = new PdfCanvas(form, pdfDocument);
         canvas.Rectangle(10, 10, 30, 30);
         canvas.Stroke();
         canvas.AddXObject(form1, 0, 0);
         canvas.Release();
         form.Flush();
         //Create page1 and add forms to the page.
         PdfPage page1 = pdfDocument.AddNewPage();
         canvas        = new PdfCanvas(page1);
         canvas.AddXObject(form, 0, 0);
         canvas.Release();
         pdfDocument.Close();
     }
                                 , NUnit.Framework.Throws.InstanceOf <PdfAConformanceException>().With.Message.EqualTo(MessageFormatUtil.Format(PdfAConformanceException.THE_DOCUMENT_DOES_NOT_CONTAIN_A_PDFA_OUTPUTINTENT_BUT_PAGE_CONTAINS_TRANSPARENCY_AND_DOES_NOT_CONTAIN_BLENDING_COLOR_SPACE)))
     ;
 }
Пример #5
0
        public virtual void CreatePdf(String src, String dest)
        {
            //Initialize PDF document
            PdfDocument pdf     = new PdfDocument(new PdfWriter(dest));
            PdfDocument origPdf = new PdfDocument(new PdfReader(src));
            //Original page size
            PdfPage   origPage = origPdf.GetPage(1);
            Rectangle orig     = origPage.GetPageSizeWithRotation();
            //Add A4 page
            PdfPage page = pdf.AddNewPage(PageSize.A4.Rotate());
            //Shrink original page content using transformation matrix
            PdfCanvas       canvas = new PdfCanvas(page);
            AffineTransform transformationMatrix = AffineTransform.GetScaleInstance(page.GetPageSize().GetWidth() / orig
                                                                                    .GetWidth(), page.GetPageSize().GetHeight() / orig.GetHeight());

            canvas.ConcatMatrix(transformationMatrix);
            PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);

            canvas.AddXObject(pageCopy, 0, 0);
            //Add page with original size
            pdf.AddPage(origPage.CopyTo(pdf));
            //Add A2 page
            page = pdf.AddNewPage(PageSize.A2.Rotate());
            //Scale original page content using transformation matrix
            canvas = new PdfCanvas(page);
            transformationMatrix = AffineTransform.GetScaleInstance(page.GetPageSize().GetWidth() / orig.GetWidth(), page
                                                                    .GetPageSize().GetHeight() / orig.GetHeight());
            canvas.ConcatMatrix(transformationMatrix);
            canvas.AddXObject(pageCopy, 0, 0);
            pdf.Close();
            origPdf.Close();
        }
Пример #6
0
        public virtual void PageCopyAsFormXObjectWithInheritedResourcesTest()
        {
            String        cmpFile  = sourceFolder + "cmp_pageCopyAsFormXObjectWithInheritedResourcesTest.pdf";
            String        srcFile  = sourceFolder + "pageCopyAsFormXObjectWithInheritedResourcesTest.pdf";
            String        destFile = destinationFolder + "pageCopyAsFormXObjectWithInheritedResourcesTest.pdf";
            PdfDocument   origPdf  = new PdfDocument(new PdfReader(srcFile));
            PdfDocument   copyPdfX = new PdfDocument(new PdfWriter(destFile).SetSmartMode(true));
            PdfDictionary pages    = origPdf.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.Pages);

            if (pages != null)
            {
                for (int i = 1; i < origPdf.GetNumberOfPages() + 1; i++)
                {
                    PdfPage        origPage = origPdf.GetPage(i);
                    Rectangle      ps       = origPage.GetPageSize();
                    PdfPage        page     = copyPdfX.AddNewPage(new PageSize(ps));
                    PdfCanvas      canvas   = new PdfCanvas(page);
                    PdfFormXObject pageCopy = origPage.CopyAsFormXObject(copyPdfX);
                    canvas.AddXObject(pageCopy, 0, 0);
                }
            }
            copyPdfX.Close();
            origPdf.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destFile, cmpFile, destinationFolder));
        }
Пример #7
0
        public void SetBarcode(Barcode barcode, int numberPage, float paddingY)
        {
            _xObject = barcode.GetBarcode.CreateFormXObject(null, _pdfDoc);
            PdfCanvas canvas = new PdfCanvas(_pdfDoc.GetPage(numberPage));

            canvas.AddXObject(_xObject, 2, 0, 0, 2, 40, paddingY);
        }
Пример #8
0
        public static void CreatePdf(string src, string dest, int row, int col)
        {
            //Initialize PDF document
            PdfDocument pdf       = new PdfDocument(new PdfWriter(dest));
            PdfDocument sourcePdf = new PdfDocument(new PdfReader(src));
            //Original page
            PdfPage origPage = sourcePdf.GetPage(1);
            //Original page size
            Rectangle      orig     = origPage.GetPageSize();
            PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
            //N-up page
            PageSize  nUpPageSize = PageSize.LETTER.Rotate();
            PdfPage   page        = pdf.AddNewPage(nUpPageSize);
            PdfCanvas canvas      = new PdfCanvas(page);
            //Scale page
            var width                = nUpPageSize.GetWidth();
            var height               = nUpPageSize.GetHeight();
            var origWidth            = orig.GetWidth();
            var origHeight           = orig.GetHeight();
            var transformationMatrix = AffineTransform.GetScaleInstance(width / origWidth / col, height / origHeight / row);

            canvas.ConcatMatrix(transformationMatrix);
            //Add pages to N-up page
            for (var i = 0; i < col; i++)
            {
                for (var j = 0; j < row; j++)
                {
                    canvas.AddXObject(pageCopy, i * origWidth, j * origHeight);
                }
            }

            pdf.Close();
            sourcePdf.Close();
        }
Пример #9
0
        private byte[] CreatePDF(string html)
        {
            byte[] binData;
            using (var workStream = new MemoryStream())
            {
                var         pdfWriter = new PdfWriter(workStream);
                var         pdf       = new PdfDocument(pdfWriter);
                PdfDocument sourcePdf = new PdfDocument(new PdfReader(new MemoryStream(CreateDocument(html))));
                //Original page
                PdfPage        origPage = sourcePdf.GetPage(1);
                PdfFormXObject pageCopy = origPage.CopyAsFormXObject(pdf);
                //Original page size
                Rectangle orig = origPage.GetPageSizeWithRotation();

                //Просто сжимание в A4
                Rectangle tileSize = PageSize.A4.Rotate();

                // Transformation matrix//30 пунктов - это 20px справа и слева, 40 * 0,75
                AffineTransform transformationMatrix = new AffineTransform();
                if (tileSize.GetHeight() / orig.GetHeight() > 0.5)
                {
                    transformationMatrix = AffineTransform.GetScaleInstance((tileSize.GetWidth() - 30) / orig.GetWidth(), 0.5);
                }
                else
                {
                    transformationMatrix = AffineTransform.GetScaleInstance((tileSize.GetWidth() - 30) / orig.GetWidth(), (tileSize.GetHeight() - 30) / orig.GetHeight());
                }
                PdfPage   transformPage   = pdf.AddNewPage(new PageSize(tileSize));
                PdfCanvas transformCanvas = new PdfCanvas(transformPage);
                transformCanvas.ConcatMatrix(transformationMatrix);
                if (tileSize.GetHeight() / orig.GetHeight() > 0.5)
                {
                    //20 пунктов сверху отступ
                    transformCanvas.AddXObject(pageCopy, 30, tileSize.GetHeight() + (tileSize.GetHeight() - orig.GetHeight()) - 20);
                }
                else
                {
                    transformCanvas.AddXObject(pageCopy, 30, 20);
                }
                pdf.Close();
                pdfWriter.Close();
                binData = workStream.ToArray();
            }

            return(binData);
        }
Пример #10
0
        private static void MergePdfFiles(string outputFile, List <string> sourceFiles)
        {
            using (var writer = new PdfWriter(new FileStream(outputFile, FileMode.OpenOrCreate)))
            {
                using (var outputDocument = new PdfDocument(writer))
                {
                    outputDocument.InitializeOutlines();
                    var        rootOutline = outputDocument.GetOutlines(false);
                    PdfOutline rootO       = rootOutline.AddOutline("Root");
                    int        pages       = 1;
                    int        count       = 1;
                    foreach (var inputFile in sourceFiles)
                    {
                        using (var inputDoc = new PdfDocument(new PdfReader((inputFile))))
                        {
                            for (int i = 1; i <= inputDoc.GetNumberOfPages(); i++)
                            {
                                var newp     = outputDocument.AddNewPage();
                                var canvas   = new PdfCanvas(newp);
                                var origPage = inputDoc.GetPage(i);
                                var copy     = origPage.CopyAsFormXObject(outputDocument);
                                canvas.AddXObject(copy, 0, 0);
                                copy.Flush();
                                origPage = null;
                                canvas.Release();
                                newp.Flush();
                                writer.Flush();
                                canvas = null;
                                newp   = null;
                            }

                            var subPages = inputDoc.GetNumberOfPages();
                            //PdfOutline pdfOutine = inputDoc.GetOutlines(false);

                            /*var link1 = rootO.AddOutline(count.ToString());
                             * link1.AddDestination(PdfExplicitDestination.CreateFit(outputDocument.GetPage(pages)));
                             * pages += subPages;*/

                            PdfOutline pdfOutine = inputDoc.GetOutlines(false);

                            //pdfOutine.GetAllChildren().FirstOrDefault().AddOutline;
                            foreach (var aOutline in pdfOutine.GetAllChildren())
                            {
                                var link1 = rootO.AddOutline(aOutline.GetTitle(), aOutline.pos);
                                link1.AddDestination(PdfExplicitDestination.CreateFit(outputDocument.GetPage(pages)));
                            }
                            pages += subPages;


                            count++;
                        }
                    }
                }
            }
        }
Пример #11
0
        private static void AddContentToPage(PdfPage pdfPage, PdfFont font, PdfImageXObject xObject)
        {
            PdfCanvas canvas = AddBasicContent(pdfPage, font);

            canvas.SaveState().Rectangle(250, 500, 100, 100).Fill().RestoreState();
            PdfFont courier = PdfFontFactory.CreateFont(StandardFonts.COURIER);

            courier.MakeIndirect(pdfPage.GetDocument());
            canvas.SaveState().BeginText().MoveText(36, 650).SetFontAndSize(courier, 16).ShowText("Hello Courier!").EndText
                ().RestoreState();
            canvas.SaveState().Circle(100, 400, 25).Fill().RestoreState();
            canvas.SaveState().RoundRectangle(100, 650, 100, 100, 10).Fill().RestoreState();
            canvas.SaveState().SetLineWidth(10).RoundRectangle(250, 650, 100, 100, 10).Stroke().RestoreState();
            canvas.SaveState().SetLineWidth(5).Arc(400, 650, 550, 750, 0, 180).Stroke().RestoreState();
            canvas.SaveState().SetLineWidth(5).MoveTo(400, 550).CurveTo(500, 570, 450, 450, 550, 550).Stroke().RestoreState
                ();
            canvas.AddXObject(xObject, 100, 500, 400);
            PdfImageXObject xObject2 = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "itext.png"));

            xObject2.MakeIndirect(pdfPage.GetDocument());
            canvas.AddXObject(xObject2, 100, 300, 400);
        }
Пример #12
0
        public virtual void CreateDocumentWithForms()
        {
            String      destinationDocument = destinationFolder + "documentWithForms1.pdf";
            FileStream  fos      = new FileStream(destinationDocument, FileMode.Create);
            PdfWriter   writer   = new PdfWriter(fos);
            PdfDocument document = new PdfDocument(writer);
            //Create form XObject and flush to document.
            PdfFormXObject form   = new PdfFormXObject(new Rectangle(0, 0, 50, 50));
            PdfCanvas      canvas = new PdfCanvas(form, document);

            canvas.Rectangle(10, 10, 30, 30);
            canvas.Fill();
            canvas.Release();
            form.Flush();
            //Create page1 and add forms to the page.
            PdfPage page1 = document.AddNewPage();

            canvas = new PdfCanvas(page1);
            canvas.AddXObject(form, 0, 0).AddXObject(form, 50, 0).AddXObject(form, 0, 50).AddXObject(form, 50, 50);
            canvas.Release();
            //Create form from the page1 and flush it.
            form = new PdfFormXObject(page1);
            form.Flush();
            //Now page1 can be flushed. It's not needed anymore.
            page1.Flush();
            //Create page2 and add forms to the page.
            PdfPage page2 = document.AddNewPage();

            canvas = new PdfCanvas(page2);
            canvas.AddXObject(form, 0, 0);
            canvas.AddXObject(form, 0, 200);
            canvas.AddXObject(form, 200, 0);
            canvas.AddXObject(form, 200, 200);
            canvas.Release();
            page2.Flush();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationDocument, sourceFolder + "cmp_documentWithForms1.pdf"
                                                                             , destinationFolder, "diff_"));
        }
Пример #13
0
        public virtual void CreateDocumentFromImages1()
        {
            String      destinationDocument = destinationFolder + "documentFromImages1.pdf";
            FileStream  fos      = new FileStream(destinationDocument, FileMode.Create);
            PdfWriter   writer   = new PdfWriter(fos);
            PdfDocument document = new PdfDocument(writer);

            PdfImageXObject[] images = new PdfImageXObject[4];
            for (int i = 0; i < 4; i++)
            {
                images[i] = new PdfImageXObject(ImageDataFactory.Create(PdfXObjectTest.images[i]));
                images[i].SetLayer(new PdfLayer("layer" + i, document));
                if (i % 2 == 0)
                {
                    images[i].Flush();
                }
            }
            for (int i_1 = 0; i_1 < 4; i_1++)
            {
                PdfPage   page   = document.AddNewPage();
                PdfCanvas canvas = new PdfCanvas(page);
                canvas.AddXObject(images[i_1], PageSize.Default);
                page.Flush();
            }
            PdfPage   page_1   = document.AddNewPage();
            PdfCanvas canvas_1 = new PdfCanvas(page_1);

            canvas_1.AddXObject(images[0], 0, 0, 200);
            canvas_1.AddXObject(images[1], 300, 0, 200);
            canvas_1.AddXObject(images[2], 0, 300, 200);
            canvas_1.AddXObject(images[3], 300, 300, 200);
            canvas_1.Release();
            page_1.Flush();
            document.Close();
            NUnit.Framework.Assert.IsTrue(new FileInfo(destinationDocument).Length < 20 * 1024 * 1024);
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationDocument, sourceFolder + "cmp_documentFromImages1.pdf"
                                                                             , destinationFolder, "diff_"));
        }
        public virtual void ImageBinaryTransparencySameColorTest()
        {
            // See http://stackoverflow.com/questions/39119776/itext-binary-transparency-bug
            String outFile = destinationFolder + "imageBinaryTransparencySameColorTest.pdf";
            String cmpFile = sourceFolder + "cmp_imageBinaryTransparencySameColorTest.pdf";

            ImageData       bkgnd = ImageDataFactory.Create(sourceFolder + "itext.jpg");
            PdfImageXObject image = new PdfImageXObject(bkgnd);

            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFile));

            PdfCanvas canvas    = new PdfCanvas(pdfDocument.AddNewPage());
            PdfPage   firstPage = pdfDocument.GetFirstPage();

            canvas.AddXObject(image, firstPage.GetPageSize());
            canvas
            .BeginText()
            .SetTextMatrix(36, 790)
            .SetFontAndSize(PdfFontFactory.CreateFont(), 12)
            .ShowText("Invisible image (both opaque and non opaque pixels have the same color)")
            .EndText();
            canvas.AddXObject(new PdfImageXObject(
                                  ImageDataFactory.Create(CreateBinaryTransparentAWTImage(Color.Black, false, null), null)), 36, 580);

            PdfDocument cmpDoc = new PdfDocument(new PdfReader(cmpFile));

            // In general case this code will probably will fail, however in this particular case we know the structure of the pdf
            PdfStream outStream = firstPage.GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName("Im1"));
            PdfStream cmpStream = cmpDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName("Im1"));

            NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStream, cmpStream));

            cmpDoc.Close();
            pdfDocument.Close();

            PrintOutputPdfNameAndDir(outFile);
        }
Пример #15
0
        public virtual void ConvertToXObjectStringPdfDocumentConverterProps()
        {
            String                  name     = "eclipse";
            String                  destName = "CTXO_" + name + "_StringDocProps";
            PdfDocument             doc      = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf"));
            PdfPage                 page     = doc.AddNewPage();
            ISvgConverterProperties props    = new SvgConverterProperties();
            PdfXObject              xObj     = SvgConverter.ConvertToXObject(ECLIPSESVGSTRING, doc, props);
            PdfCanvas               canv     = new PdfCanvas(page);

            canv.AddXObject(xObj, 0, 0);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + destName + ".pdf", sourceFolder
                                                                             + "cmp_" + destName + ".pdf", destinationFolder, "diff_"));
        }
Пример #16
0
            /* (non-Javadoc)
             * @see com.itextpdf.kernel.events.IEventHandler#handleEvent(com.itextpdf.kernel.events.Event)
             */
            public void HandleEvent(Event @event)
            {
                PdfDocumentEvent docEvent  = (PdfDocumentEvent)@event;
                PdfDocument      pdf       = docEvent.GetDocument();
                PdfPage          page      = docEvent.GetPage();
                PdfCanvas        pdfCanvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdf);

                pdfCanvas.AddXObject(stationery, 0, 0);
                Rectangle rect   = new Rectangle(36, 32, 36, 64);
                Canvas    canvas = new Canvas(pdfCanvas, rect);

                canvas.Add(new Paragraph((pdf.GetNumberOfPages().ToString())).SetFontSize(48)
                           .SetFontColor(ColorConstants.WHITE));
                canvas.Close();
            }
Пример #17
0
        protected internal override void DoDraw(SvgDrawContext context)
        {
            ResourceResolver resourceResolver = context.GetResourceResolver();

            if (resourceResolver == null || this.attributesAndStyles == null)
            {
                return;
            }
            String     uri     = this.attributesAndStyles.Get(SvgConstants.Attributes.XLINK_HREF);
            PdfXObject xObject = resourceResolver.RetrieveImageExtended(uri);

            if (xObject == null)
            {
                return;
            }
            PdfCanvas currentCanvas = context.GetCurrentCanvas();
            float     x             = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.X))
            {
                x = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.X));
            }
            float y = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.Y))
            {
                y = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.Y));
            }
            float width = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.WIDTH))
            {
                width = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.WIDTH));
            }
            float height = 0;

            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.HEIGHT))
            {
                height = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.HEIGHT));
            }
            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.PRESERVE_ASPECT_RATIO))
            {
            }
            // TODO RND-876
            float v = y + height;

            currentCanvas.AddXObject(xObject, width, 0, 0, -height, x, v);
        }
Пример #18
0
            public virtual void HandleEvent(Event @event)
            {
                PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
                PdfDocument      pdf      = docEvent.GetDocument();
                PdfPage          page     = docEvent.GetPage();
                int       pageNumber      = pdf.GetPageNumber(page);
                Rectangle pageSize        = page.GetPageSize();
                PdfCanvas pdfCanvas       = new PdfCanvas(page.GetLastContentStream(), page.GetResources(), pdf);

                iText.Layout.Canvas canvas = new iText.Layout.Canvas(pdfCanvas, pdf, pageSize);
                Paragraph           p      = new Paragraph().Add("Page ").Add(pageNumber.ToString()).Add(" of");

                canvas.ShowTextAligned(p, this.x, this.y, TextAlignment.RIGHT);
                pdfCanvas.AddXObject(this.placeholder, this.x + this.space, this.y - this.descent);
                canvas.Close();
            }
Пример #19
0
        public virtual void ConvertToXObjectStreamPdfDocumentConverterProps()
        {
            String                  name     = "eclipse";
            String                  destName = "CTXO_" + name + "_StreamDocProps";
            FileStream              fis      = new FileStream(sourceFolder + name + ".svg", FileMode.Open, FileAccess.Read);
            PdfDocument             doc      = new PdfDocument(new PdfWriter(destinationFolder + destName + ".pdf"));
            PdfPage                 page     = doc.AddNewPage();
            ISvgConverterProperties props    = new SvgConverterProperties();
            PdfXObject              xObj     = SvgConverter.ConvertToXObject(fis, doc, props);
            PdfCanvas               canv     = new PdfCanvas(page);

            canv.AddXObject(xObj, 0, 0);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareVisually(destinationFolder + destName + ".pdf", sourceFolder
                                                                            + "cmp_" + destName + ".pdf", destinationFolder, "diff_"));
        }
Пример #20
0
        public virtual void AddFlushedImageXObjectToCanvas()
        {
            String          filename     = destinationFolder + "addFlushedImageXObjectToCanvas.pdf";
            String          cmpfile      = sourceFolder + "cmp_addFlushedImageXObjectToCanvas.pdf";
            String          image        = sourceFolder + "image.png";
            PdfDocument     pdfDoc       = new PdfDocument(new PdfWriter(filename));
            PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(image));

            // flushing pdf object directly
            imageXObject.GetPdfObject().MakeIndirect(pdfDoc).Flush();
            PdfCanvas canvas = new PdfCanvas(pdfDoc.AddNewPage());

            canvas.AddXObject(imageXObject, 50, 500, 200);
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(filename, cmpfile, destinationFolder));
        }
Пример #21
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 AddImageXObjectTest()
        {
            String          fileName     = "addImageXObjectTest.pdf";
            String          destPdf      = DESTINATION_FOLDER + fileName;
            String          cmpPdf       = SOURCE_FOLDER + "cmp_" + fileName;
            FileStream      fos          = new FileStream(destPdf, FileMode.Create);
            PdfWriter       writer       = new PdfWriter(fos);
            PdfDocument     document     = new PdfDocument(writer);
            PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(SOURCE_FOLDER + "box.png"));
            PdfPage         page         = document.AddNewPage();
            PdfCanvas       canvas       = new PdfCanvas(page);

            canvas.AddXObject(imageXObject);
            canvas.Release();
            page.Flush();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
                                                                             ));
        }
Пример #23
0
        public virtual void Barcode417XObjectTest()
        {
            String      filename = "barcode417XObjectTest.pdf";
            PdfWriter   writer   = new PdfWriter(destinationFolder + filename);
            PdfDocument document = new PdfDocument(writer);
            PdfPage     page     = document.AddNewPage();
            PdfCanvas   canvas   = new PdfCanvas(page);
            String      text     = "Call me Ishmael. Some years ago--never mind how long " + "precisely --having little or no money in my purse, and nothing "
                                   + "particular to interest me on shore, I thought I would sail about " + "a little and see the watery part of the world.";
            BarcodePDF417 barcode = new BarcodePDF417();

            barcode.SetCode(text);
            PdfFormXObject xObject = barcode.CreateFormXObject(document);

            canvas.AddXObject(xObject, 10, 650);
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + filename, sourceFolder
                                                                             + "cmp_" + filename, destinationFolder));
        }
Пример #24
0
        private void ConvertAndCompare(String outFilename, String cmpFilename, String imageFilename)
        {
            PdfDocument     pdfDoc       = new PdfDocument(new PdfWriter(outFilename));
            PdfDocument     cmpDoc       = new PdfDocument(new PdfReader(cmpFilename));
            PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(imageFilename));
            PdfCanvas       canvas       = new PdfCanvas(pdfDoc.AddNewPage());

            canvas.AddXObject(imageXObject, 50, 500, 346);
            pdfDoc.Close();
            PdfDocument outDoc    = new PdfDocument(new PdfReader(outFilename));
            PdfStream   outStream = outDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
                                                                                                                      ("Im1"));
            PdfStream cmpStream = cmpDoc.GetFirstPage().GetResources().GetResource(PdfName.XObject).GetAsStream(new PdfName
                                                                                                                    ("Im1"));

            NUnit.Framework.Assert.IsNull(new CompareTool().CompareStreamsStructure(outStream, cmpStream));
            cmpDoc.Close();
            outDoc.Close();
        }
Пример #25
0
        public void HandleEvent(Event e)
        {
            PdfDocumentEvent docEvent = (PdfDocumentEvent)e;
            PdfDocument      pdf      = docEvent.GetDocument();
            PdfPage          page     = docEvent.GetPage();
            int       pageNumber      = pdf.GetPageNumber(page);
            int       pageCount       = pdf.GetNumberOfPages();
            Rectangle pageSize        = page.GetPageSize();
            PdfCanvas pdfCanvas       = new PdfCanvas(
                page.GetLastContentStream(), page.GetResources(), pdf);
            Canvas canvas = new Canvas(pdfCanvas, pdf, pageSize);

            canvas.SetFont(font);
            Paragraph p = new Paragraph()
                          .Add("第").Add(pageNumber.ToString()).Add("页 ");

            canvas.ShowTextAligned(p, x, y, TextAlignment.RIGHT);
            pdfCanvas.AddXObject(placeholder, x, y - descent);
            pdfCanvas.Release();
        }
Пример #26
0
        public virtual void TestExcessiveXrefEntriesForCopyXObject()
        {
            PdfDocument inputPdf  = new PdfDocument(new PdfReader(sourceFolder + "input500.pdf"));
            PdfDocument outputPdf = new PdfDocument(new PdfWriter(destinationFolder + "output500.pdf"));
            float       scaleX    = 595f / 612f;
            float       scaleY    = 842f / 792f;

            for (int i = 1; i <= inputPdf.GetNumberOfPages(); ++i)
            {
                PdfPage        sourcePage   = inputPdf.GetPage(i);
                PdfFormXObject pageCopy     = sourcePage.CopyAsFormXObject(outputPdf);
                PdfPage        page         = outputPdf.AddNewPage(PageSize.A4);
                PdfCanvas      outputCanvas = new PdfCanvas(page);
                outputCanvas.AddXObject(pageCopy, scaleX, 0, 0, scaleY, 0, 0);
                page.Flush();
            }
            outputPdf.Close();
            inputPdf.Close();
            NUnit.Framework.Assert.IsNotNull(outputPdf.GetXref());
            NUnit.Framework.Assert.AreEqual(500, outputPdf.GetXref().Size() - inputPdf.GetXref().Size());
        }
 public virtual void TransparencyCheckTest1()
 {
     NUnit.Framework.Assert.That(() => {
         PdfWriter writer             = new PdfWriter(new MemoryStream());
         Stream @is                   = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
         PdfOutputIntent outputIntent = new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1"
                                                            , @is);
         PdfADocument doc = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, outputIntent);
         doc.AddNewPage();
         PdfCanvas canvas       = new PdfCanvas(doc.GetLastPage());
         PdfFormXObject xObject = new PdfFormXObject(new Rectangle(100, 100));
         PdfCanvas xObjCanvas   = new PdfCanvas(xObject, doc);
         xObjCanvas.Rectangle(30, 30, 10, 10).Fill();
         PdfTransparencyGroup group = new PdfTransparencyGroup();
         xObject.SetGroup(group);
         canvas.AddXObject(xObject, new Rectangle(300, 300));
         doc.Close();
     }
                                 , NUnit.Framework.Throws.InstanceOf <PdfAConformanceException>().With.Message.EqualTo(PdfAConformanceException.A_GROUP_OBJECT_WITH_AN_S_KEY_WITH_A_VALUE_OF_TRANSPARENCY_SHALL_NOT_BE_INCLUDED_IN_A_FORM_XOBJECT))
     ;
 }
Пример #28
0
        public virtual void CompareColorspacesTest()
        {
            String[] imgFiles = new String[] { "adobe.png", "anon.gif", "anon.jpg", "anon.png", "gamma.png", "odd.png"
                                               , "rec709.jpg", "srgb.jpg", "srgb.png" };
            String      @out        = destinationFolder + "compareColorspacesTest.pdf";
            String      cmp         = sourceFolder + "cmp_compareColorspacesTest.pdf";
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(@out));
            PdfCanvas   canvas      = new PdfCanvas(pdfDocument.AddNewPage());

            canvas.BeginText().MoveText(40, 730).SetFontAndSize(PdfFontFactory.CreateFont(), 12).ShowText("The images below are in row and expected to form four continuous lines of constant colors."
                                                                                                          ).EndText();
            for (int i = 0; i < imgFiles.Length; i++)
            {
                String          imgFile      = imgFiles[i];
                PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "compare_colorspaces/"
                                                                                           + imgFile));
                canvas.AddXObject(imageXObject, 50 + i * 40, 550, 40);
            }
            pdfDocument.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(@out, cmp, destinationFolder, "diff_"));
        }
        public virtual void AddFormXObjectWithoutMatrixTest()
        {
            String         fileName    = "addXObjectWithoutMatrixTest.pdf";
            String         destPdf     = DESTINATION_FOLDER + fileName;
            String         cmpPdf      = SOURCE_FOLDER + "cmp_" + fileName;
            FileStream     fos         = new FileStream(destPdf, FileMode.Create);
            PdfWriter      writer      = new PdfWriter(fos);
            PdfDocument    document    = new PdfDocument(writer);
            PdfFormXObject formXObject = new PdfFormXObject(new Rectangle(5, 5, 15, 20));

            new PdfCanvas(formXObject, document).Circle(10, 15, 10).Fill();
            PdfPage   page   = document.AddNewPage();
            PdfCanvas canvas = new PdfCanvas(page);

            canvas.AddXObject(formXObject);
            canvas.Release();
            page.Flush();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
                                                                             ));
        }
Пример #30
0
        public virtual void FlushingTest01()
        {
            String       outPdf = destinationFolder + "pdfA1b_flushingTest01.pdf";
            String       cmpPdf = sourceFolder + "cmp/PdfAFlushingTest/cmp_pdfA1b_flushingTest01.pdf";
            PdfWriter    writer = new PdfWriter(outPdf);
            Stream       @is    = new FileStream(sourceFolder + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read);
            PdfADocument doc    = new PdfADocument(writer, PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent("Custom", ""
                                                                                                              , "http://www.color.org", "sRGB IEC61966-2.1", @is));
            PdfCanvas       canvas       = new PdfCanvas(doc.AddNewPage());
            PdfImageXObject imageXObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg"));

            imageXObject.MakeIndirect(doc);
            canvas.AddXObject(imageXObject, new Rectangle(30, 300, 300, 300));
            imageXObject.Flush();
            if (imageXObject.IsFlushed())
            {
                NUnit.Framework.Assert.Fail("Flushing of unchecked objects shall be forbidden.");
            }
            doc.Close();
            CompareResult(outPdf, cmpPdf);
        }