iTextSharp页脚背景颜色

时间:2017-08-21 03:12:04

标签: itext

我正在尝试设置页脚的背景颜色。我似乎找不到任何可以帮助这样做的代码。请参阅我的OnEndPage事件代码。

我试过cb.SetColorFill(BaseColor.LIGHT_GRAY);,但是这不起作用:/

使用哪种属性或方法向页脚添加背景颜色?

 public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
        {
            base.OnEndPage(writer, document);
            iTextSharp.text.Font baseFontNormal = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.NORMAL, iTextSharp.text.BaseColor.BLACK);
            iTextSharp.text.Font baseFontBig = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 12f, iTextSharp.text.Font.BOLD, iTextSharp.text.BaseColor.BLACK);

            var headerImagePath = System.Web.HttpContext.Current.Server.MapPath("~/Content/misc/proactive-reg-form-header.jpg");
            var headerImage = Image.GetInstance(headerImagePath);
            if (headerImage.Height > headerImage.Width)
            {
                //Maximum height is 800 pixels.
                float percentage = 0.0f;
                percentage = 700 / headerImage.Height;
                headerImage.ScalePercent(percentage * 100);
            }
            else
            {
                //Maximum width is 600 pixels.
                float percentage = 0.0f;
                percentage = 572 / headerImage.Width;
                headerImage.ScalePercent(percentage * 100);
            }

            //Create PdfTable object
            PdfPTable pdfTab = new PdfPTable(1);

            //We will have to create separate cells to include image logo and 2 separate strings
            //Row 1
            //PdfPCell pdfCell1 = new PdfPCell();
            PdfPCell pdfCell2 = new PdfPCell(headerImage);
            //PdfPCell pdfCell3 = new PdfPCell();
            String text = "Page " + writer.PageNumber + " of ";

            //Add paging to footer
            {
                cb.BeginText();
                cb.SetFontAndSize(bf, 10);
                cb.SetTextMatrix(document.PageSize.GetRight(100), document.PageSize.GetBottom(30));
                cb.ShowText(text);
                cb.EndText();
                float len = bf.GetWidthPoint(text, 10);
                cb.AddTemplate(footerTemplate, document.PageSize.GetRight(100) + len, document.PageSize.GetBottom(30));

                //add image to footer
                writer.DirectContent.AddImage(footerImage);
            }

            pdfCell2.HorizontalAlignment = Element.ALIGN_CENTER;
            pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM;

            pdfCell2.Border = 0;

            pdfTab.AddCell(pdfCell2);

            pdfTab.TotalWidth = 100f;
            pdfTab.WidthPercentage = 100f;

            //call WriteSelectedRows of PdfTable. This writes rows from PdfWriter in PdfTable
            //first param is start row. -1 indicates there is no end row and all the rows to be included to write
            //Third and fourth param is x and y position to start writing
            pdfTab.WriteSelectedRows(0, -1, 10, document.PageSize.Height - 10, writer.DirectContent);
            //set pdfContent value

            if (document.PageNumber != 1)
            {
                //Move the pointer and draw line to separate header section from rest of page
                cb.MoveTo(10, document.PageSize.Height - 60);
                cb.LineTo((document.PageSize.Width - 10), document.PageSize.Height - 60);
                cb.Stroke();
            }

        }

1 个答案:

答案 0 :(得分:0)

知道了!

所以我修改了以下代码:

//Add paging to footer
        {
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.SetTextMatrix(document.PageSize.GetRight(100), document.PageSize.GetBottom(30));
            cb.ShowText(text);
            cb.EndText();
            float len = bf.GetWidthPoint(text, 10);
            cb.AddTemplate(footerTemplate, document.PageSize.GetRight(100) + len, document.PageSize.GetBottom(30));

            //add image to footer
            writer.DirectContent.AddImage(footerImage);
        }

//Add paging to footer
        {
            cb.BeginText();
            cb.SetFontAndSize(bf, 10);
            cb.SetTextMatrix(document.PageSize.GetRight(100), document.PageSize.GetBottom(30));
            cb.ShowText(text);
            cb.EndText();
            float len = bf.GetWidthPoint(text, 10);
            cb.AddTemplate(footerTemplate, document.PageSize.GetRight(100) + len, document.PageSize.GetBottom(30));

            //this part adds the background color
            cb.SetColorFill(BaseColor.LIGHT_GRAY);
            cb.Rectangle(0, 0, document.PageSize.Width, 50);
            cb.FillStroke();

            //add image to footer
            writer.DirectContent.AddImage(footerImage);
        }
相关问题