Example #1
0
        private void button4_Click(object sender, EventArgs e)
        {
            PdfWriter   writer   = new PdfWriter("C:\\Users/Slize/Desktop/SemestrV/UP/lab/Siatka.pdf");
            PdfDocument pdf      = new PdfDocument(writer);
            Document    document = new Document(pdf);

            System.Drawing.Image kod  = pictureBox2.Image;
            ImageData            data = ImageDataFactory.Create(kod, Color.White);

            iText.Layout.Element.Image komorka = new iText.Layout.Element.Image(data);
            Table table = new Table(2).UseAllAvailableWidth();

            for (int i = 0; i < 12; i++)
            {
                table.AddCell(komorka);
            }
            document.Add(table);
            document.Close();
        }
        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            ImageData       image   = ImageDataFactory.Create(IMG);
            PdfImageXObject xObject = new PdfImageXObject(image);

            List list = new List()
                        .SetListSymbol(new Image(xObject))
                        .Add("Hello World")
                        .Add("This is a list item with a lot of text. It will certainly take more than one line." +
                             " This shows that the list item is indented and that the image is used as bullet.")
                        .Add("This is a test");

            doc.Add(list);

            doc.Close();
        }
Example #3
0
        public virtual void TestOriginalSizeScaleMode()
        {
            String        path          = PdfHelper.GetDefaultImagePath();
            FileInfo      file          = new FileInfo(path);
            OcrPdfCreator ocrPdfCreator = new OcrPdfCreator(new CustomOcrEngine());
            PdfDocument   doc           = ocrPdfCreator.CreatePdf(JavaCollectionsUtil.SingletonList <FileInfo>(file), PdfHelper.GetPdfWriter
                                                                      ());

            NUnit.Framework.Assert.IsNotNull(doc);
            ImageData imageData   = ImageDataFactory.Create(file.FullName);
            float     imageWidth  = GetPoints(imageData.GetWidth());
            float     imageHeight = GetPoints(imageData.GetHeight());
            float     realWidth   = doc.GetFirstPage().GetPageSize().GetWidth();
            float     realHeight  = doc.GetFirstPage().GetPageSize().GetHeight();

            NUnit.Framework.Assert.AreEqual(imageWidth, realWidth, DELTA);
            NUnit.Framework.Assert.AreEqual(imageHeight, realHeight, DELTA);
            doc.Close();
        }
        public void fnc_AddImage(String pImgPath, String pFootText)
        {
            Paragraph p      = new Paragraph();
            float     fSpace = 10f;

            p.SetBorderBottom(new iText.Layout.Borders.SolidBorder(1));
            p.SetMargin(0f);
            p.SetMarginTop(fSpace);
            p.SetMarginBottom(fSpace);

            ImageData imgData = ImageDataFactory.Create(pImgPath);
            Image     img     = new Image(imgData);

            img.SetAutoScale(true);
            img.SetPaddingTop(10f);
            p.Add(img);
            p.Add(pFootText);
            m_Document.Add(p);
        }
Example #5
0
        public virtual void ImageTest08()
        {
            String      outFileName = destinationFolder + "imageTest08.pdf";
            String      cmpFileName = sourceFolder + "cmp_imageTest08.pdf";
            PdfWriter   writer      = new PdfWriter(outFileName);
            PdfDocument pdfDoc      = new PdfDocument(writer);
            Document    doc         = new Document(pdfDoc);

            iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + "Desert.jpg"
                                                                                                      ));
            Div div = new Div();

            div.Add(image);
            div.Add(image);
            doc.Add(div);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Example #6
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();
        }
        public virtual void AddImageXObjectWithTransformationMatrixTest()
        {
            String          fileName     = "addImageXObjectWithTransformationMatrixTest.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.AddXObjectWithTransformationMatrix(imageXObject, 20, 0, 0, 40, 0, 0);
            canvas.Release();
            page.Flush();
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destPdf, cmpPdf, DESTINATION_FOLDER, "diff_"
                                                                             ));
        }
        public virtual void ShowTextAlignedTest03()
        {
            String      outFileName = destinationFolder + "showTextAlignedTest03.pdf";
            String      cmpFileName = sourceFolder + "cmp_showTextAlignedTest03.pdf";
            PdfDocument pdfDoc      = new PdfDocument(new PdfWriter(outFileName));
            Document    doc         = new Document(pdfDoc);

            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(sourceFolder + "bruno.jpg"));
            float          width           = img.GetImageScaledWidth();
            float          height          = img.GetImageScaledHeight();
            PdfFormXObject template        = new PdfFormXObject(new Rectangle(width, height));

            iText.Layout.Canvas canvas = new iText.Layout.Canvas(template, pdfDoc);
            canvas.Add(img).ShowTextAligned("HELLO BRUNO", width / 2, height / 2, TextAlignment.CENTER);
            doc.Add(new iText.Layout.Element.Image(template));
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Example #9
0
 public virtual void Png_imageTransparancy_8bitDepthImage() {
     String outFileName = destinationFolder + "png_imageTransparancy_8bitDepthImage.pdf";
     String cmpFileName = sourceFolder + "cmp_png_imageTransparancy_8bitDepthImage.pdf";
     PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName, new WriterProperties().SetCompressionLevel
         (CompressionConstants.NO_COMPRESSION)));
     PdfPage page = pdfDocument.AddNewPage(PageSize.A4);
     PdfCanvas canvas = new PdfCanvas(page);
     canvas.SetFillColor(ColorConstants.LIGHT_GRAY).Fill();
     canvas.Rectangle(80, 0, PageSize.A4.GetWidth() - 80, PageSize.A4.GetHeight()).Fill();
     canvas.SaveState().BeginText().MoveText(116, 800).SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts.HELVETICA
         ), 14).SetFillColor(ColorConstants.MAGENTA).ShowText("8 bit depth PNG").MoveText(0, -20).ShowText("This image should not have a black rectangle as background"
         ).EndText().RestoreState();
     ImageData img = ImageDataFactory.Create(sourceFolder + "manualTransparency_8bit.png");
     canvas.AddImage(img, 100, 450, 200, false);
     canvas.Release();
     pdfDocument.Close();
     NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
         , "diff_"));
 }
Example #10
0
        private iText.Layout.Element.Image QrCode(string code, string language)
        {
            ICode qrCode = new Code();

            Bitmap image = qrCode.GetImage($"https://pdf.ricardogaefke.com/check/{code}/{language}");

            MemoryStream ms = new MemoryStream();

            image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);

            ImageData imageData = ImageDataFactory.Create(ms.ToArray());

            ms.Close();

            iText.Layout.Element.Image finalImage = new iText.Layout.Element.Image(imageData);
            finalImage.SetWidth(50);

            return(finalImage);
        }
Example #11
0
        public void CreateWatermark()
        {
            var tranState = new PdfExtGState();

            tranState.SetFillOpacity(Opacity);

            var reader = new PdfReader(PdfFileName);
            var writer = new PdfWriter(OutFile);
            var pdf    = new PdfDocument(reader, writer);

            var document = new Document(pdf);


            for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
            {
                var page = pdf.GetPage(i);
                page.SetIgnorePageRotationForContent(false);
                var canvas = new PdfCanvas(pdf.GetPage(i));
                canvas.SaveState();
                canvas.SetExtGState(tranState);

                if (ImageFileName != null)
                {
                    var img = ImageDataFactory.Create(ImageFileName);
                    img.SetWidth(ImageWidth);
                    img.SetHeight(ImageHeight);
                    canvas.AddImage(img, AlignX, AlignY, false);
                }

                if (WatermarkText != null)
                {
                    var verticalWatermark = new Paragraph(WatermarkText).SetFontSize(FontSize);
                    document.ShowTextAligned(verticalWatermark, AlignX, AlignY, i, TextAlignment.LEFT, VerticalAlignment.BOTTOM, RadAngle);
                }


                canvas.RestoreState();
            }
            WatermarkText = null;
            ImageFileName = null;
            pdf.Close();
            document.Close();
        }
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF writer
            PdfWriter writer = new PdfWriter(dest);
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(writer);
            // Initialize document
            Document document = new Document(pdf);

            // Compose Paragraph
            iText.Layout.Element.Image fox = new Image(ImageDataFactory.Create(FOX));
            iText.Layout.Element.Image dog = new iText.Layout.Element.Image(ImageDataFactory.Create(DOG));
            Paragraph p = new Paragraph("The quick brown ").Add(fox).Add(" jumps over the lazy ").Add(dog);

            // Add Paragraph to document
            document.Add(p);
            //Close document
            document.Close();
        }
Example #13
0
        public virtual void ImageWidthTest01()
        {
            String      outFileName = destinationFolder + "imageWidthTest01.pdf";
            String      cmpFileName = sourceFolder + "cmp_imageWidthTest01.pdf";
            PdfWriter   writer      = new PdfWriter(outFileName);
            PdfDocument pdfDoc      = new PdfDocument(writer);
            Document    doc         = new Document(pdfDoc);

            doc.Add(new Paragraph(new Text("First Line")));
            Paragraph       p       = new Paragraph();
            PdfImageXObject xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "Desert.jpg"));

            iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject);
            image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(100));
            p.Add(image);
            doc.Add(p);
            doc.Add(new Paragraph(new Text("Second Line")));
            p       = new Paragraph();
            xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "itis.jpg"));
            image   = new iText.Layout.Element.Image(xObject);
            image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(100));
            p.Add(image);
            doc.Add(p);
            doc.Add(new Paragraph(new Text("Third Line")));
            p       = new Paragraph();
            xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "Desert.jpg"));
            image   = new iText.Layout.Element.Image(xObject);
            image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePercentValue(100));
            image.SetProperty(Property.MAX_HEIGHT, UnitValue.CreatePointValue(200f));
            p.Add(image);
            doc.Add(p);
            doc.Add(new Paragraph(new Text("Fourth Line")));
            p       = new Paragraph();
            xObject = new PdfImageXObject(ImageDataFactory.Create(imageFolder + "itis.jpg"));
            image   = new iText.Layout.Element.Image(xObject);
            image.SetProperty(Property.MAX_WIDTH, UnitValue.CreatePointValue(100));
            p.Add(image);
            doc.Add(p);
            doc.Add(new Paragraph(new Text("Fifth Line")));
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc    = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Document    doc       = new Document(pdfDoc);
            PdfFont     font      = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            Paragraph   paragraph = new Paragraph("My watermark (text)")
                                    .SetFont(font)
                                    .SetFontSize(30);
            ImageData img = ImageDataFactory.Create(IMG);

            float w = img.GetWidth();
            float h = img.GetHeight();

            PdfExtGState gs1 = new PdfExtGState().SetFillOpacity(0.5f);

            // Implement transformation matrix usage in order to scale image
            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
            {
                PdfPage   pdfPage  = pdfDoc.GetPage(i);
                Rectangle pageSize = pdfPage.GetPageSizeWithRotation();

                // When "true": in case the page has a rotation, then new content will be automatically rotated in the
                // opposite direction. On the rotated page this would look as if new content ignores page rotation.
                pdfPage.SetIgnorePageRotationForContent(true);

                float     x    = (pageSize.GetLeft() + pageSize.GetRight()) / 2;
                float     y    = (pageSize.GetTop() + pageSize.GetBottom()) / 2;
                PdfCanvas over = new PdfCanvas(pdfDoc.GetPage(i));
                over.SaveState();
                over.SetExtGState(gs1);
                if (i % 2 == 1)
                {
                    doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
                }
                else
                {
                    over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), false);
                }
                over.RestoreState();
            }

            doc.Close();
        }
Example #15
0
        public virtual void ContainWithRotationObjectsFitTest()
        {
            // TODO DEVSIX-4286 object-fit property combined with rotation is not processed correctly
            String outFileName = destinationFolder + "objectFit_test_with_rotation.pdf";
            String cmpFileName = sourceFolder + "cmp_objectFit_test_with_rotation.pdf";

            using (PdfWriter writer = new PdfWriter(outFileName)) {
                using (Document doc = new Document(new PdfDocument(writer))) {
                    PdfImageXObject            xObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg"));
                    iText.Layout.Element.Image image   = new iText.Layout.Element.Image(xObject).SetWidth(200).SetHeight(600).SetRotationAngle
                                                             (45).SetBorder(new SolidBorder(new DeviceGray(0), 1)).SetObjectFit(ObjectFit.CONTAIN);
                    Paragraph p = new Paragraph();
                    p.Add(image);
                    doc.Add(p);
                }
            }
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Example #16
0
        public virtual void ContainWithEffectsObjectsFitTest()
        {
            String outFileName = destinationFolder + "objectFit_test_with_effects.pdf";
            String cmpFileName = sourceFolder + "cmp_objectFit_test_with_effects.pdf";

            using (PdfWriter writer = new PdfWriter(outFileName)) {
                using (Document doc = new Document(new PdfDocument(writer))) {
                    PdfImageXObject            xObject = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg"));
                    iText.Layout.Element.Image image   = new iText.Layout.Element.Image(xObject).SetWidth(200).SetHeight(600).SetBorder
                                                             (new SolidBorder(new DeviceGray(0), 5)).SetBorderRadius(new BorderRadius(100)).SetObjectFit(ObjectFit.
                                                                                                                                                         CONTAIN);
                    Paragraph p = new Paragraph();
                    p.Add(image);
                    doc.Add(p);
                }
            }
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
        public ActionResult MergeImages()
        {
            string imgFolder = @"C:\Users\Habiba\Desktop\APWDN\FileUpload\FileUpload\UploadedImages\";
            string pdfFolder = @"C:\Users\Habiba\Desktop\APWDN\FileUpload\FileUpload\Generated PDFs\demo.pdf";

            //Getting Images
            var           imageFiles = Directory.GetFiles(imgFolder, "*.jpg");
            List <string> imageList  = new List <string>();

            foreach (string image in imageFiles)
            {
                imageList.Add(image);
            }

            //Creating the pdf document
            PdfWriter   writer   = new PdfWriter(pdfFolder);
            PdfDocument pdf      = new PdfDocument(writer);
            Document    document = new Document(pdf);
            //Header
            Paragraph header = new Paragraph("DEMO").SetTextAlignment(TextAlignment.CENTER).SetFontSize(20);
            //New Line
            Paragraph newline = new Paragraph(new Text("\n"));

            //Adding Header & New Line
            document.Add(newline);
            document.Add(header);
            //Line Separator
            LineSeparator ls = new LineSeparator(new SolidLine());

            document.Add(ls);

            //Adding the Images
            foreach (string i in imageList)
            {
                Image img = new Image(ImageDataFactory.Create(i)).SetTextAlignment(TextAlignment.CENTER);
                document.Add(img);
            }

            //Closing the document & returning content
            document.Close();
            return(Content("PDF Generation Successful! <a href=" + "./Home/MergeImages" + ">Generate Another</a>"));
        }
Example #18
0
        /// <exception cref="System.IO.IOException"/>
        public virtual void CreatePdf(String dest)
        {
            //Initialize PDF writer
            PdfWriter writer = new PdfWriter(dest);
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(writer);
            // Initialize document
            Document document = new Document(pdf);

            //Add paragraph to the document
            document.Add(new Paragraph("Hello World!"));

            Image yorkie = new Image(ImageDataFactory.Create(@"/Users/mariosky/Desktop/york.jpg"));
            Image blood  = new Image(ImageDataFactory.Create(@"/Users/mariosky/Desktop/blood.jpg"));

            yorkie.SetWidth(200);
            Paragraph p = new Paragraph(" ").Add(yorkie).Add("     ").Add(yorkie);

            // Add Paragraph to document
            document.Add(p);

            Paragraph p2 = new Paragraph(" ").Add(yorkie).Add("     ").Add(yorkie);

            // Add Paragraph to document
            document.Add(p2);
            float[] w = { 200, 250 };

            Table table = new Table(w, false);
            Cell  cell  = new Cell(1, 1).Add(yorkie.SetAutoScale(true));
            Cell  cell2 = new Cell(1, 2).Add(blood.SetAutoScale(true));
            Cell  cell3 = new Cell(2, 2).Add(blood.SetAutoScale(true));

            table.AddCell(cell);
            table.AddCell(cell2);
            document.Add(table);

            //Close document
            document.Close();

            //Close document
            document.Close();
        }
Example #19
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            Image     dog = new Image(ImageDataFactory.Create(DOG));
            Image     fox = new Image(ImageDataFactory.Create(FOX));
            Paragraph p   = new Paragraph("quick brown fox jumps over the lazy dog.");

            p.Add("Or, to say it in a more colorful way: quick brown ");
            p.Add(fox);
            p.Add(" jumps over the lazy ");
            p.Add(dog);
            p.Add(".");

            p.SetWidth(350);
            doc.ShowTextAligned(p, 50, 650, TextAlignment.LEFT);

            doc.Close();
        }
Example #20
0
        public virtual void RotatedBordersTest()
        {
            fileName = "rotatedBordersTest.pdf";
            Document doc = CreateDocument();

            doc.SetMargins(0, 0, 0, 0);
            Paragraph p = new Paragraph("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas porttitor congue massa. Fusce posuere, magna sed pulvinar ultricies, purus lectus malesuada libero, sit amet commodo magna eros quis urna.\n"
                                        + "Nunc viverra imperdiet enim. Fusce est. Vivamus a tellus.\n" + "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Proin pharetra nonummy pede. Mauris et orci.\n"
                                        );

            p.SetBorder(new SolidBorder(50));
            p.SetRotationAngle(Math.PI / 6);
            doc.Add(p);
            iText.Layout.Element.Image img = new Image(ImageDataFactory.Create(sourceFolder + "Desert.jpg"));
            img.SetBorder(new SolidBorder(50));
            img.SetRotationAngle(Math.PI / 6);
            doc.Add(img);
            doc.Close();
            CloseDocumentAndCompareOutputs(doc);
        }
Example #21
0
        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc    = new Document(pdfDoc);

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();

            table.AddCell("Behold a cell with an image pattern:");

            Cell      cell = new Cell();
            ImageData img  = ImageDataFactory.Create(IMG);

            cell.SetNextRenderer(new TiledImageBackgroundRenderer(cell, img));
            cell.SetHeight(60);
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
Example #22
0
        public virtual void DivTest01()
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "divTest01.pdf"));

            pdfDocument.SetTagged();
            Document document = new Document(pdfDocument);
            Div      div      = new Div();

            div.Add(CreateParagraph1());
            iText.Layout.Element.Image image = new iText.Layout.Element.Image(ImageDataFactory.Create(sourceFolder + imageName
                                                                                                      ));
            image.SetAutoScale(true);
            div.Add(image);
            div.Add(CreateParagraph2());
            div.Add(image);
            div.Add(CreateParagraph2());
            document.Add(div);
            document.Close();
            CompareResult("divTest01.pdf", "cmp_divTest01.pdf");
        }
Example #23
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            ImageData   image  = ImageDataFactory.Create(IMG);

            // Translation defines the position of the image on the page and scale transformation sets image dimensions
            // Please also note that image without scaling is drawn in 1x1 rectangle. And here we draw image on page using
            // its original size in pixels.
            AffineTransform affineTransform = AffineTransform.GetTranslateInstance(36, 300);

            // Make sure that the image is visible by concatenating a scale transformation
            affineTransform.Concatenate(AffineTransform.GetScaleInstance(image.GetWidth(), image.GetHeight()));

            PdfCanvas canvas = new PdfCanvas(pdfDoc.GetFirstPage());

            float[] matrix = new float[6];
            affineTransform.GetMatrix(matrix);
            canvas.AddImageWithTransformationMatrix(image, matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
            pdfDoc.Close();
        }
Example #24
0
        public virtual void ImageInRotatedBlockTest01()
        {
            String      outFileName = destinationFolder + "imageInRotatedBlockTest01.pdf";
            String      cmpFileName = sourceFolder + "cmp_imageInRotatedBlockTest01.pdf";
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(outFileName));
            Document    doc         = new Document(pdfDocument);

            iText.Layout.Element.Image image = new Image(ImageDataFactory.Create(sourceFolder + "Desert.jpg"));
            image.SetWidth(200);
            Div div = new Div();

            div.SetRotationAngle(Math.PI / 2);
            div.SetBorder(new SolidBorder(ColorConstants.BLUE, 1));
            div.Add(image);
            doc.Add(div);
            doc.Add(new Paragraph("Hello!!!").SetBackgroundColor(ColorConstants.RED));
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Example #25
0
        public virtual void ImageWithSoftMaskMatteTest()
        {
            String      outFileName = destinationFolder + "imageWithSoftMaskMatte.pdf";
            String      cmpFileName = sourceFolder + "cmp_imageWithSoftMaskMatte.pdf";
            PdfDocument pdfDoc      = new PdfDocument(new PdfWriter(outFileName));
            ImageData   mask        = ImageDataFactory.Create(sourceFolder + "matteMask.jpg");

            mask.MakeMask();
            PdfImageXObject maskXObject = new PdfImageXObject(mask);

            maskXObject.Put(new PdfName("Matte"), new PdfArray(new float[] { 1, 1, 1 }));
            ImageData       img1    = ImageDataFactory.Create(sourceFolder + "imageForMatteMask.jpg");
            PdfImageXObject xObject = new PdfImageXObject(img1, maskXObject);
            PdfCanvas       canvas  = new PdfCanvas(pdfDoc.AddNewPage(PageSize.A4));

            canvas.AddXObjectAt(xObject, 50, 500);
            pdfDoc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             ));
        }
Example #26
0
        public virtual void ImageTest01()
        {
            String          outFileName = destinationFolder + "imageTest01.pdf";
            String          cmpFileName = sourceFolder + "cmp_imageTest01.pdf";
            PdfWriter       writer      = new PdfWriter(outFileName);
            PdfDocument     pdfDoc      = new PdfDocument(writer);
            Document        doc         = new Document(pdfDoc);
            PdfImageXObject xObject     = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg"));

            iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject, 100);
            doc.Add(new Paragraph(new Text("First Line")));
            Paragraph p = new Paragraph();

            p.Add(image);
            doc.Add(p);
            doc.Add(new Paragraph(new Text("Second Line")));
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Example #27
0
        public virtual void ImageInParagraphBorderTest()
        {
            String          outFileName = destinationFolder + "imageParagraphBorderTest.pdf";
            String          cmpFileName = sourceFolder + "cmp_imageParagraphBorderTest.pdf";
            PdfWriter       writer      = new PdfWriter(outFileName);
            PdfDocument     pdfDoc      = new PdfDocument(writer);
            Document        doc         = new Document(pdfDoc);
            PdfImageXObject xObject     = new PdfImageXObject(ImageDataFactory.Create(sourceFolder + "Desert.jpg"));

            iText.Layout.Element.Image image = new iText.Layout.Element.Image(xObject, 100);
            Paragraph p = new Paragraph();

            p.SetBorder(new SolidBorder(ColorConstants.GREEN, 5));
            p.Add(image);
            image.SetBorder(new SolidBorder(ColorConstants.BLUE, 5));
            doc.Add(p);
            doc.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             , "diff"));
        }
Example #28
0
        /// <summary>
        ///
        /// </summary>
        public void Work()
        {
            //
            var dest = Settings.Default.OutputLocation + "\\" + "12345678.pdf";

            //Initialize PDF writer
            PdfWriter writer = new PdfWriter(dest);
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(writer);
            // Initialize document
            Document document = new Document(pdf);
            //Add paragraph to the document
            var para = new Paragraph("Hello World!");

            para.SetTextAlignment(TextAlignment.CENTER);
            document.Add(para);
            para = new Paragraph("Hello World! For the second time!!");
            para.SetTextAlignment(TextAlignment.LEFT);
            document.Add(para);
            para = new Paragraph("Hello World! For the third time!!!");
            para.SetTextAlignment(TextAlignment.RIGHT);
            document.Add(para);
            para = new Paragraph("Hello World! For the fourth time!!!!");
            para.SetTextAlignment(TextAlignment.JUSTIFIED_ALL);
            document.Add(para);
            // Add an image to the document.
            var imagePath = Settings.Default.ImageFilesLocation;
            var fox       = new Image(ImageDataFactory.Create(imagePath + "\\" + "desert.jpg"));

            fox.Scale((float).25, (float).25);
            var dog = new Image(ImageDataFactory.Create(imagePath + "\\" + "koala.jpg"));

            dog.Scale((float).25, (float).25);
            var p = new Paragraph()
                    .Add(fox)
                    .Add(dog);

            document.Add(p);
            //Close document
            document.Close();
        }
        protected internal virtual void CreatePdf(String dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            PageSize    ps  = PageSize.A5;
            // Initialize document
            Document document = new Document(pdf, ps);
            //Set column parameters
            float offSet       = 36;
            float columnWidth  = (ps.GetWidth() - offSet * 2 + 10) / 3;
            float columnHeight = ps.GetHeight() - offSet * 2;

            //Define column areas
            Rectangle[] columns = new Rectangle[] {
                new Rectangle(offSet - 5, offSet, columnWidth, columnHeight),
                new Rectangle(offSet + columnWidth, offSet, columnWidth, columnHeight),
                new Rectangle(offSet + columnWidth * 2 + 5, offSet, columnWidth, columnHeight)
            };
            //
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));
            Image  apple        = new Image(ImageDataFactory.Create(APPLE_IMG)).SetWidth(columnWidth);
            String articleApple = File.ReadAllText(System.IO.Path.Combine(APPLE_TXT), Encoding.UTF8);

            C03E01_NewYorkTimes.AddArticle(document, "Apple Encryption Engineers, if Ordered to Unlock iPhone, Might Resist"
                                           , "By JOHN MARKOFF MARCH 18, 2016", apple, articleApple);

            Image facebook = new Image(ImageDataFactory.Create(FACEBOOK_IMG)
                                       ).SetWidth(columnWidth);
            String articleFB = File.ReadAllText(System.IO.Path.Combine(FACEBOOK_TXT), Encoding.UTF8);

            C03E01_NewYorkTimes.AddArticle(document, "With \"Smog Jog\" Through Beijing, Zuckerberg Stirs Debate on Air Pollution"
                                           , "By PAUL MOZUR MARCH 18, 2016", facebook, articleFB);

            Image  inst             = new Image(ImageDataFactory.Create(INST_IMG)).SetWidth(columnWidth);
            String articleInstagram = File.ReadAllText(System.IO.Path.Combine(INST_TXT), Encoding.UTF8);

            C03E01_NewYorkTimes.AddArticle(document, "Instagram May Change Your Feed, Personalizing It With an Algorithm"
                                           , "By MIKE ISAAC MARCH 15, 2016", inst, articleInstagram);

            document.Close();
        }
Example #30
0
        public static void Generate(string dest)
        {
            //Initialize PDF document
            PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
            PageSize    ps  = PageSize.A5;
            // Initialize document
            Document document = new Document(pdf, ps);
            //Set column parameters
            const int offSet       = 36;
            float     columnWidth  = (ps.GetWidth() - offSet * 2 + 10) / 3;
            float     columnHeight = ps.GetHeight() - offSet * 2;

            //Define column areas
            Rectangle[] columns =
            {
                new Rectangle(offSet - 5,                   offSet, columnWidth, columnHeight),
                new Rectangle(offSet + columnWidth,         offSet, columnWidth, columnHeight),
                new Rectangle(offSet + columnWidth * 2 + 5, offSet, columnWidth, columnHeight)
            };
            //
            document.SetRenderer(new ColumnDocumentRenderer(document, columns));
            var apple        = new Image(ImageDataFactory.Create(@"data/ny_times_apple.jpg")).SetWidth(columnWidth);
            var articleApple = File.ReadAllText(@"data/ny_times_apple.txt", Encoding.UTF8);

            AddArticle(document, "Apple Encryption Engineers, if Ordered to Unlock iPhone, Might Resist"
                       , "By JOHN MARKOFF MARCH 18, 2016", apple, articleApple);

            var facebook  = new Image(ImageDataFactory.Create(@"data/ny_times_fb.jpg")).SetWidth(columnWidth);
            var articleFb = File.ReadAllText(@"data/ny_times_fb.txt", Encoding.UTF8);

            AddArticle(document, "With \"Smog Jog\" Through Beijing, Zuckerberg Stirs Debate on Air Pollution"
                       , "By PAUL MOZUR MARCH 18, 2016", facebook, articleFb);

            var inst             = new Image(ImageDataFactory.Create(@"data/ny_times_inst.jpg")).SetWidth(columnWidth);
            var articleInstagram = File.ReadAllText(@"data/ny_times_inst.txt", Encoding.UTF8);

            AddArticle(document, "Instagram May Change Your Feed, Personalizing It With an Algorithm"
                       , "By MIKE ISAAC MARCH 15, 2016", inst, articleInstagram);

            document.Close();
        }