c# - 如何使用 iTextSharp 设置 PDF 段落的字体?

标签 c# pdf fonts 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 的尝试不会编译(“'iTextSharp.text.FontFactory.GetFont(string, float, iTextSharp.text.BaseColor)' 的最佳重载方法匹配有一些无效参数"), 所以我通过一次添加一个 arg 来让智能感知“帮助”我。因为对于第一个 arg,它说它是字体名称,一个字符串,所以我添加了“Segoe UI”;下一个参数是字体大小,一个 float ,所以我添加了 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。当我随后添加这些参数时,它改变了它的“想法”并说它真正想要的是字符串、字符串和 bool 值?

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

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);

最佳答案

GetFont14 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 被解释为 double ,因为没有重载。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")

关于c# - 如何使用 iTextSharp 设置 PDF 段落的字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29521515/

相关文章:

c# - 如何使用 Visual Studio 或 Resharper(或任何其他工具!)将 HTML 正确粘贴到 C# 代码中?

c# - 在 C# 中使用 iTextSharp 将 PDF 旋转 90 度

fonts - 禁用 gnuplot 的字体抗锯齿

c# - 从 Asp.Net 取消注册客户端脚本

c# Selenium WebDriver - 如何禁用 Facebook 页面上的通知?

android - 如何实现异步加载页面的 PDF 查看器

ios - 在 iOS 开发中我应该使用我喜欢的字体还是动态字体(系统)

mysql - 在cmd控制台中显示utf-8的字体

c# - 如何解决 "Service not available, closing transmission channel. The server response was: Server busy, too many connections"

android - 在 phonegap 中将 base64 字符串转换为 pdf