【问题标题】:iTextSharp Footer Gets Progressively Bolder with Each PageiTextSharp 页脚随着每一页逐渐变粗
【发布时间】:2014-11-10 13:26:20
【问题描述】:

使用OnEndPage,我向使用iTextSharp 创建的PDF 添加页脚。每页页脚字体逐渐变粗。

如何在页脚中创建一致的 NORMAL 字体?

这是我的代码:

 public override void OnEndPage(PdfWriter writer, Document doc)
    {
        iTextSharp.text.Image gif = null;

        if (FooterImage)
        {

            if (File.Exists(PathImages))
            {
                gif = iTextSharp.text.Image.GetInstance(PathImages);
                gif.ScaleToFit(75f, 75f);
                gif.SetAbsolutePosition(0, 0);

            }

        }
        string sFooter = string.Empty;
        if (FooterURL != null && FooterURL.Length > 0)
        {
            sFooter = FooterURL + "                    ";
        }

        if (FooterDate != null && FooterDate.Length > 0)
        {
            sFooter += FooterDate + "                    ";
        }
        if (FooterPage)
        {
            sFooter += "Page " + doc.PageNumber.ToString();
        }

        PdfPTable footerTbl = new PdfPTable(1);
        footerTbl.TotalWidth = 900;
        footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;

        Phrase ph = new Phrase(sFooter, FontFactory.GetFont(FontFactory.TIMES, 10, iTextSharp.text.Font.NORMAL));
        PdfPCell cell = new PdfPCell(ph);

        cell.Border = 0;
        cell.PaddingLeft = 10;

        footerTbl.AddCell(cell);

        if (FooterImage)
        {
            PdfContentByte cbfoot = writer.DirectContent;
            PdfTemplate tpl = cbfoot.CreateTemplate(gif.Width / 5, gif.Height / 5);
            tpl.AddImage(gif);
            cbfoot.AddTemplate(tpl, doc.PageSize.Width - 100, 10);
        }

        footerTbl.WriteSelectedRows(0, -1, 10, 30, writer.DirectContent);

    }

【问题讨论】:

  • 你能展示你如何使用页面事件对象吗?你能分享一个结果PDF文件吗?

标签: java itextsharp footer


【解决方案1】:

在过去,在字体方面没有今天那么多的选择时,人们使用变通方法来创建粗体字体。使字体加粗的一种方法是在同一位置一遍又一遍地添加相同的文本。我认为这发生在你身上。

当您正确使用页面事件时,onEndPage() 方法会在每次页面结束时自动触发。我的猜测是你做错了很多次触发onEndPage() 的事情。也许您在代码中被称为onEndPage(),也许您不止一次将页面事件添加到编写器(并且页面事件是累积的)。

如果我必须猜测,我猜你正在做后者。我的猜测是基于您在 onEndPage() 方法中使用诸如 FooterImage 之类的变量这一事实。您如何设置该变量。如果您在页面事件的构造函数中设置它,并且一遍又一遍地将新页面事件添加到编写器,那么您做错了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 2014-08-20
    • 1970-01-01
    • 2010-11-05
    • 2011-05-07
    • 1970-01-01
    • 2013-05-17
    • 1970-01-01
    相关资源
    最近更新 更多