Esempio n. 1
0
        public void ManipulatePdf(
            string pdfSrc,
            string pdfDest,
            string text,
            string imageName,
            float fontSize,
            string fontName)
        {
            MemoryStream pdfStream = new(File.ReadAllBytes(pdfSrc));
            PdfDocument  pdfDoc    = new(new PdfReader(pdfStream), new PdfWriter(pdfDest));
            Document     doc       = new(pdfDoc);
            PdfFont      font      = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(fontName));
            Paragraph    paragraph = new Paragraph(text).SetFont(font).SetFontSize(fontSize).SetFontColor(iText.Kernel.Colors.ColorConstants.CYAN);

            //DrawText(text, new Font(StandardFonts.HELVETICA, 14.0f), System.Drawing.Color.Aqua, 500, IMG + imageName);
            //ImageData img = ImageDataFactory.Create(IMG + imageName);

            //float w = img.GetWidth();
            //float h = img.GetHeight();
            float radio = 20.2f;

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

            // 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.GetPageSize();
                float     x        = (pageSize.GetLeft() + pageSize.GetRight()) / 2;
                float     y        = (pageSize.GetTop() + pageSize.GetBottom()) / 2;
                PdfCanvas over     = new PdfCanvas(pdfPage);
                over.SaveState();
                over.SetExtGState(gs1);

                //if (i % 2 == 1)
                //{
                //    doc.ShowTextAligned(paragraph, x, y-200, i, TextAlignment.CENTER, VerticalAlignment.TOP, 10);
                //    doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, 10);
                // }
                // else
                //{

                doc.ShowTextAligned(paragraph, x - 200, y + 160, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio);
                doc.ShowTextAligned(paragraph, x - 100, y + 80, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio);
                doc.ShowTextAligned(paragraph, x, y, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio);
                doc.ShowTextAligned(paragraph, x + 100, y - 80, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio);
                doc.ShowTextAligned(paragraph, x + 200, y - 160, i, TextAlignment.CENTER, VerticalAlignment.TOP, radio);

                //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 80), true);
                //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 60), true);
                //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 40), true);
                //over.AddImageWithTransformationMatrix(img, w, 0, 0, h, x - (w / 2), y - (h / 2), true);
                //}

                over.RestoreState();
            }

            doc.Close();
        }
Esempio n. 2
0
        public void HandleEvent(Event @event)
        {
            PdfDocumentEvent docEvent = (PdfDocumentEvent)@event;
            PdfPage          page     = docEvent.GetPage();
            int pageNumber            = docEvent.GetDocument().GetNumberOfPages();
            var bgColor   = ((SolidColorBrush)Pages[pageNumber - 1].Canvas.Background).Color;
            var pageColor = new iText.Kernel.Colors.DeviceRgb(
                Convert.ToInt32(bgColor.R),
                Convert.ToInt32(bgColor.G),
                Convert.ToInt32(bgColor.B)
                );

            PdfCanvas canvas = new PdfCanvas(page);

            iText.Kernel.Geom.Rectangle rect = page.GetPageSize();
            canvas.SaveState()
            .SetFillColor(pageColor)
            .Rectangle(rect.GetLeft(), rect.GetBottom(), rect.GetWidth(), rect.GetHeight())
            .FillStroke()
            .RestoreState();
        }