我尝试跟随这里的示例,添加了以下代码来创建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);