如何使用iTextSharp设置PDF段落字体?

9

我尝试跟随这里的示例,添加了以下代码来创建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却无法编译(“The best overloaded method match for 'iTextSharp.text.FontFactory.GetFont(string, float, iTextSharp.text.BaseColor)' has some invalid arguments”)。因此,我使用智能感知自动一次添加一个参数。由于第一个参数是字体名称,即字符串,我添加了“Segoe UI”;下一个参数是字体大小,即浮点数,所以我添加了18.0;最后,需要字体颜色,为BaseColor类型,所以我添加了BaseColor.Black,结果如下:
var titleFont = FontFactory.GetFont("Segoe UI", 18.0, BaseColor.BLACK);

但是这也无法编译,显示“iTextSharp.text.FontFactory.GetFont(string, string, bool)的最佳重载方法匹配具有一些无效参数”。

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

此外,示例还展示了将段落添加到文档中的方式:

doc.Add(docTitle, titleFont);

...但这也不行,因为会出现“No overload for method 'Add' takes 2 arguments”的错误。

我该怎么做才能让iTextSharp满意呢?无论我跳蹦还是唱诵,它都不愿意按照我的方式执行。

更新

好的,这个版本可以编译:

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

1
你需要在段落的构造函数中设置字体,而不是文档。至于 GetFont 方法,我认为你只是缺少了颜色参数。换句话说,应该同时设置粗体和黑色,而不是仅设置其中一个。 - crthompson
1
可能与https://dev59.com/sGox5IYBdhLWcg3wi0vF#9145164相关或重复。 - crthompson
1个回答

14

GetFont 目前有 14 个可能的重载

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)

所以第一步,选择最适合你的。

以下行不起作用的原因是:

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

如果根据c#规范因为缺乏后缀18.0将被解释为double类型并且由于没有重载将其转换为字符串。

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年。 - B. Clay Shannon-B. Crow Raven
这个回答表述得不太好。在C#中,18.0永远不会自动转换为字符串。在C#中,数字只有在使用字符串“+”运算符时才会隐式转换为字符串,而这里并没有使用该运算符。实际上发生的是重载决议正在选择它认为最接近的重载,并因类型不匹配而失败。 - MgSam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接