示例#1
0
        public static void Overlay(PdfDocument pdf, int pageNum, float x, float y, float width, float height, float fontSize, string background, string foreground, string text)
        {
            PdfFont font = PdfFontFactory.CreateFont(iText.IO.Font.Constants.StandardFonts.HELVETICA, iText.IO.Font.PdfEncodings.UTF8, true);
            PdfPage page = pdf.GetPage(pageNum);

            float fWidth  = font.GetWidth(text, fontSize);
            float fHeight = fontSize * 1.1f;

            Rectangle r  = new Rectangle(x, y, Math.Max(fWidth, width), Math.Max(fHeight, height));
            Color     bg = new Color(background);
            Color     fg = new Color(foreground);

            PdfCanvas pdfCanvas = new PdfCanvas(page.NewContentStreamAfter(), page.GetResources(), page.GetDocument());

            pdfCanvas.SetFillColorRgb(bg.r, bg.g, bg.b);
            pdfCanvas.Rectangle(r);
            pdfCanvas.Fill();

            if (text != "")
            {
                pdfCanvas.SetFillColorRgb(fg.r, fg.g, fg.b);
                Canvas canvas = new Canvas(pdfCanvas, pdf, new Rectangle(r.GetLeft(), r.GetBottom(), r.GetWidth(), r.GetHeight()));

                iText.Layout.Element.Paragraph p = new iText.Layout.Element.Paragraph(text);

                p.SetMarginLeft((r.GetWidth() - fWidth) * 0.5f);
                p.SetMarginTop((r.GetHeight() - fHeight) * 0.5f);
                p.SetFont(font);
                p.SetFontSize(fontSize);

                canvas.Add(p);
            }
        }
示例#2
0
        /// <summary>
        /// Aggiunge una pagina con il testo specificato
        /// </summary>
        /// <param name="text"></param>
        /// <param name="margintop"></param>
        /// <param name="marginleft"></param>
        public void AddBlankPageWithText(string text, float margintop = 20f, float marginleft = 20f, bool alignCenter = false)
        {
            //Aggiunge pagina vuota per evitare eccezione
            var page = this.mDocument.AddNewPage(PageSize.A4);

            iText.Kernel.Pdf.Canvas.PdfCanvas pdfCanvas = new iText.Kernel.Pdf.Canvas.PdfCanvas(page);
            var canvas = new Canvas(pdfCanvas, page.GetCropBox());
            var p      = new iText.Layout.Element.Paragraph(text);

            if (alignCenter)
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER);
            }
            else
            {
                p.SetTextAlignment(iText.Layout.Properties.TextAlignment.LEFT);
            }
            p.SetMarginTop(margintop);
            p.SetMarginLeft(marginleft);
            canvas.Add(p);
            canvas.Close();

            this.NumPagesMerged++;
            this.NumDocumentsMerged++;
        }