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 static void RotarPDF(string ficheroDestino, string ficheroOrigen, int angulo)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(ficheroOrigen), new PdfWriter(ficheroDestino));
            int         margin = 72;

            for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
            {
                PdfPage page = pdfDoc.GetPage(i);
                // change page size
                iText.Kernel.Geom.Rectangle mediaBox    = page.GetMediaBox();
                iText.Kernel.Geom.Rectangle newMediaBox = new iText.Kernel.Geom.Rectangle((int)mediaBox.GetLeft() - margin, (int)mediaBox.GetBottom() - margin, (int)mediaBox.GetWidth() + margin * 2, (int)mediaBox.GetHeight() + margin * 2);
                page.SetMediaBox(newMediaBox);
                // add border
                PdfCanvas over = new PdfCanvas(page);
                over.Rectangle(mediaBox.GetLeft(), mediaBox.GetBottom(), mediaBox.GetWidth(), mediaBox.GetHeight());
                over.Stroke();
                // change rotation of the even pages
                page.SetRotation(angulo);
            }
            pdfDoc.Close();
        }
Esempio n. 3
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();
        }
Esempio n. 4
0
        public Task <byte[]> ManipulatePdf(
            StampSimple fileSource)
        {
            using MemoryStream pdfDest   = new();
            using MemoryStream pdfStream = new(fileSource.ImageSource);
            var pdfDoc = new PdfDocument(new PdfReader(pdfStream), new PdfWriter(pdfDest));
            var doc    = new Document(pdfDoc);

            foreach (TextProperties text in fileSource.Texts)
            {
                var font = PdfFontFactory.CreateFont(FontProgramFactory.CreateFont(text.FontName));

                iText.Kernel.Colors.Color fontColor = iText.Kernel.Colors.ColorConstants.CYAN;
                switch (text.Color)
                {
                case "WHITE": fontColor = iText.Kernel.Colors.ColorConstants.WHITE; break;

                case "BLACK": fontColor = iText.Kernel.Colors.ColorConstants.BLACK; break;

                case "BLUE": fontColor = iText.Kernel.Colors.ColorConstants.BLUE; break;

                case "DARKGRAY": fontColor = iText.Kernel.Colors.ColorConstants.DARK_GRAY; break;

                case "GRAY": fontColor = iText.Kernel.Colors.ColorConstants.GRAY; break;

                case "GREEN": fontColor = iText.Kernel.Colors.ColorConstants.GREEN; break;

                case "MAGENTA": fontColor = iText.Kernel.Colors.ColorConstants.MAGENTA; break;

                case "ORANGE": fontColor = iText.Kernel.Colors.ColorConstants.ORANGE; break;

                case "PINK": fontColor = iText.Kernel.Colors.ColorConstants.PINK; break;

                case "RED": fontColor = iText.Kernel.Colors.ColorConstants.RED; break;

                case "YELLOW": fontColor = iText.Kernel.Colors.ColorConstants.YELLOW; break;
                }

                var paragraph = new Paragraph(text.Text).SetFont(font).SetFontSize(text.FontSize).SetFontColor(fontColor);

                var gs1 = new PdfExtGState().SetFillOpacity(text.Opacity);

                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;
                    var       over     = new PdfCanvas(pdfPage);
                    over.SaveState();
                    over.SetExtGState(gs1);

                    doc.ShowTextAligned(paragraph, text.PosX, text.PosY, i, TextAlignment.CENTER, VerticalAlignment.TOP, text.Radio);
                    over.RestoreState();
                }
            }

            doc.Close();

            return(Task.FromResult(pdfDest.ToArray()));
        }