如何使用 iTextSharp 设置 PDF 段落的字体?
How can I set a PDF paragraph's font using iTextSharp?
尝试按照示例 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 的尝试不会编译(The best overloaded method match for 'iTextSharp.text.FontFactory.GetFont(string, float, iTextSharp.text.BaseColor)' has some invalid arguments”),所以我让 intellisenseless "help" 我一次添加一个参数。因为对于第一个 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。然后当我添加这些参数时,它改变了它的 "mind" 并说它真正想要的是 string、string 和 bool?
此外,该示例显示了然后将段落添加到文档中,如下所示:
doc.Add(docTitle, titleFont);
...但这也行不通,因为“方法 'Add' 没有重载需要 2 个参数 ”
我可以做什么来安抚 iTextSharp?不管我跳jig还是唱挽歌,它都不想跟着玩
更新
好的,这编译:
var docTitle = new Paragraph("UCSC Direct - Direct Payment Form");
var titleFont = FontFactory.GetFont("Courier", 18, BaseColor.BLACK);
docTitle.Font = titleFont;
doc.Add(docTitle);
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 is string 将其转换为字符串。
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")
尝试按照示例 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 的尝试不会编译(The best overloaded method match for 'iTextSharp.text.FontFactory.GetFont(string, float, iTextSharp.text.BaseColor)' has some invalid arguments”),所以我让 intellisenseless "help" 我一次添加一个参数。因为对于第一个 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。然后当我添加这些参数时,它改变了它的 "mind" 并说它真正想要的是 string、string 和 bool?
此外,该示例显示了然后将段落添加到文档中,如下所示:
doc.Add(docTitle, titleFont);
...但这也行不通,因为“方法 'Add' 没有重载需要 2 个参数 ”
我可以做什么来安抚 iTextSharp?不管我跳jig还是唱挽歌,它都不想跟着玩
更新
好的,这编译:
var docTitle = new Paragraph("UCSC Direct - Direct Payment Form");
var titleFont = FontFactory.GetFont("Courier", 18, BaseColor.BLACK);
docTitle.Font = titleFont;
doc.Add(docTitle);
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 is string 将其转换为字符串。
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")