【问题标题】:How can I set a PDF paragraph's font using iTextSharp?如何使用 iTextSharp 设置 PDF 段落的字体?
【发布时间】:2015-06-13 19:35:34
【问题描述】:

尝试遵循示例here,我添加了以下代码来创建 PDF 文档的标题:

using (var doc = new Document(PageSize.A4, 50, 50, 25, 25))
{
    using (var writer = PdfWriter.GetInstance(doc, ms))
    {
        doc.Open();

        var docTitle = new Paragraph("UCSC Direct - Direct Payment Form");                        
        var titleFont = FontFactory.GetFont("Lucida Sans", 18, Font.Bold);
        doc.Add(docTitle);

但是,创建 titleFont 的尝试无法编译(“'iTextSharp.text.FontFactory.GetFont(string, float, iTextSharp.text.BaseColor)'的最佳重载方法匹配有一些无效参数"),所以我通过一次添加一个 arg 让智能感知“帮助”我。因为对于第一个参数它说它是字体名称,一个字符串,所以我添加了“Segoe UI”;下一个参数是字体大小,一个浮点数,所以我添加了 18.0;最后,它调用了字体颜色,一个BaseColor类型,所以我添加了BaseColor.Black,最后是:

var titleFont = FontFactory.GetFont("Segoe UI", 18.0, BaseColor.BLACK);

...但这也不会编译,说“'iTextSharp.text.FontFactory.GetFont(string, string, bool)' 的最佳重载方法匹配有一些无效参数

所以当我复制示例并使用 string、int 和 Font 样式时,它说不,它需要 string、float 和 BaseColor。然后当我添加这些参数时,它改变了它的“想法”并说它真正想要的是字符串、字符串和布尔值?

此外,该示例显示然后将段落添加到文档中,如下所示:

doc.Add(docTitle, titleFont);

...但这也行不通,因为“'Add' 方法没有重载需要 2 个参数

我可以做些什么来安抚 iTextSharp?不管我跳吉格舞还是唱挽歌,它都不想跟着玩。

更新

好的,这样编译:

var docTitle = new Paragraph("UCSC Direct - Direct Payment Form");
var titleFont = FontFactory.GetFont("Courier", 18, BaseColor.BLACK);
docTitle.Font = titleFont;
doc.Add(docTitle);

【问题讨论】:

  • 您需要在段落的构造函数中设置字体,而不是在文档中。至于 GetFont 方法,我认为您只是缺少颜色参数。 IOW,请同时使用粗体和黑色,而不是仅使用其中一种。
  • 相关,可能被stackoverflow.com/a/9145164/2589202欺骗

标签: c# pdf fonts itextsharp


【解决方案1】:

GetFont 拥有14 possible overloads currently

public static Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color)
public static Font GetFont(string fontname, string encoding, bool embedded, float size, int style, BaseColor color, bool cached)
public static Font GetFont(string fontname, string encoding, bool embedded, float size, int style)
public static Font GetFont(string fontname, string encoding, bool embedded, float size)
public static Font GetFont(string fontname, string encoding, bool embedded)
public static Font GetFont(string fontname, string encoding, float size, int style, BaseColor color)
public static Font GetFont(string fontname, string encoding, float size, int style)
public static Font GetFont(string fontname, string encoding, float size)
public static Font GetFont(string fontname, string encoding)
public static Font GetFont(string fontname, float size, int style, BaseColor color)
public static Font GetFont(string fontname, float size, BaseColor color)
public static Font GetFont(string fontname, float size, int style)
public static Font GetFont(string fontname, float size)
public static Font GetFont(string fontname)

所以第 1 步,选择最适合您的。

下面一行不起作用的原因:

FontFactory.GetFont("Segoe UI", 18.0, BaseColor.BLACK);

如果because per the c# spec,没有后缀,18.0 将被解释为双精度,并且由于没有重载,.Net 是字符串将其转换为字符串。

FontFactory.GetFont("Segoe UI", 18.0f, BaseColor.BLACK)

至于段落本身,您可以在构造函数中设置字体,也可以只设置段落的Font 属性,任何一种都可以。

var p1 = new Paragraph("Hello", myFont);

var p2 = new Paragraph();
p2.Font = myFont;
p2.Add("Hello")

【讨论】:

  • 谢谢,克里斯;顺便说一句,我看到你住在拉克罗斯;我在布鲁克菲尔德和奥康诺沃克住了 15 年。
  • 这个答案措辞不好。 18.0 将永远在 C# 中自动转换为字符串。在 C# 中,唯一一次将数字隐式转换为字符串是使用字符串 + 运算符,这在此处不起作用。正在发生的事情是重载解决方案正在选择它认为最接近的重载并且由于类型不匹配而失败。
猜你喜欢
  • 2011-02-07
  • 2012-02-27
  • 2011-08-14
  • 1970-01-01
  • 2016-07-29
  • 2012-03-22
  • 2012-12-21
  • 2016-04-02
  • 2015-06-14
相关资源
最近更新 更多