【问题标题】:iTextSharp change font of all text in columniTextSharp 更改列中所有文本的字体
【发布时间】:2016-04-21 09:32:03
【问题描述】:

我正在创建一个 PDF,它采用一串 html,然后使用 HTMLWorker.ParseToList 并将每个元素添加到一列。无论如何我都无法改变字体。

我在添加时尝试将字体添加到每个元素: el.Font = 次; 但这会引发错误。

以下是我当前的代码。我希望所有文本都出现在 Times Roman 中。

var specialConditionsText = "<p>Special Conditions</p><p>A. More</p><p>B. Section B</p><p>C. Section C</p><ul><li>Bullet 1</li><li>Bullet 2</li></ul><ol><li>Number list 1</li><li>Number List 2<ol><li>Number list sub</li></ol></li></ol><p><br></p>";
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle(HtmlTags.UL, HtmlTags.INDENT, "14");
styles.LoadTagStyle(HtmlTags.OL, HtmlTags.INDENT, "14");
styles.LoadTagStyle(HtmlTags.LI, HtmlTags.LEADING, "18");

BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font times = new Font(bfTimes, 12, Font.NORMAL);

using (StringReader sr = new StringReader(specialConditionsText))
{
//Get our raw PdfContentByte object letting us draw "above" existing content
PdfContentByte cb = pdfStamper.GetOverContent(11);

//Create a new ColumnText object bound to the above PdfContentByte object
ColumnText ct = new ColumnText(cb);

//Create a single column object spanning the entire page
ct.SetSimpleColumn(60, 190, 570, 720);

//Convert our HTML to iTextSharp elements
List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);

//Loop through each element
foreach (IElement el in elements)
{
      //Add the element to the ColumnText
      el.Font = times;
      ct.AddElement(el);
}

ct.Go();
}   

【问题讨论】:

  • 为什么不用iTextSharp.text.FontFactory.GetFont 而不是BaseFont.CreateFont
  • 我刚刚添加了这个:Font times = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 12, Font.NORMAL);加载字体,但我仍然不确定如何将该字体应用于文本。
  • 仅供参考,如果您使用的是HTMLWorker,则意味着您使用的是至少 7 年历史的 iTextSharp 版本。在当前版本中,您将使用 XMLWorker

标签: css fonts pdf-generation itextsharp


【解决方案1】:

我找到的答案是:

FontFactory.RegisterDirectories();

styles.LoadTagStyle("body", "face", "times new roman");
styles.LoadTagStyle("body", "encoding", "Identity-H");

我使用的最终代码如下所示。希望它可以帮助某人。

specialConditionsText = HttpUtility.HtmlDecode(specialConditionsText);
                        specialConditionsText = specialConditionsText.Replace("<br>", "<br />");
                        specialConditionsText = specialConditionsText.Replace("<p>&nbsp;</p>", "<br />");

                        FontFactory.RegisterDirectories();

                        StyleSheet styles = new StyleSheet();
                        styles.LoadTagStyle(HtmlTags.UL, HtmlTags.INDENT, "14");
                        styles.LoadTagStyle(HtmlTags.OL, HtmlTags.INDENT, "14");
                        styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.LEADING, "13");
                        styles.LoadTagStyle(HtmlTags.BODY, HtmlTags.FONTSIZE, "11");
                        styles.LoadTagStyle("body", "face", "times new roman");
                        styles.LoadTagStyle("body", "encoding", "Identity-H");

                        using (StringReader sr = new StringReader(specialConditionsText))
                        {
                            //Get our raw PdfContentByte object letting us draw "above" existing content
                            PdfContentByte cb = pdfStamper.GetOverContent(11);

                            //Create a new ColumnText object bound to the above PdfContentByte object
                            ColumnText ct = new ColumnText(cb);

                            //Create a single column object spanning the entire page
                            ct.SetSimpleColumn(85, 192, 530, 720);

                            //Convert our HTML to iTextSharp elements
                            List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, styles);

                            //Loop through each element
                            foreach (IElement el in elements)
                            {
                                //Add the element to the ColumnText
                                ct.AddElement(el);
                            }

                            ct.Go();
                        }

【讨论】:

  • 不确定,但看起来这将对整个文档应用相同的字体。您甚至可以使用 Phrase、Chunks 自定义最小文本块的字体外观。
【解决方案2】:

您可以使用以下代码添加所需字体的文本。这里使用 addtext 函数而不是 addelement 然后使用短语(甚至可以使用块)。

ct.AddText(new Phrase(el.ToString(), times));

您可以使用任何一种方法(FontFactory.GetFont、BaseFont.CreateFont)来创建所需的字体样式。

查看以下链接以获取有关使用 itextsharp 格式化的更多详细信息: http://www.mikesdotnetting.com/article/89/itextsharp-page-layout-with-columns

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多