如何使用 iTextSharp 创建"链接外观"字体?



我想使用这个 GetFont 重载:

GetFont(string fontname, string encoding, float size, int style, BaseColor color)

。这里列举了。但是,当我尝试时,它没有编译:

var linkFont = FontFactory.GetFont(FontFactory.HELVETICA, 9, Font.Underline, BaseColor.BLUE);

我得到,"'iTextSharp.text.FontFactory.GetFont(string, float, int, iTextSharp.text.BaseColor)的最佳重载方法匹配有一些无效的参数"

但是哪一个,为什么?

我还得到,"参数 3:无法从'bool'转换为'int'"

为什么它认为第三个参数(Font.Underline,一个"int")应该是一个布尔值?那是布尔;我的意思是,这不是(a)布尔。

注意:我遇到同样的错误:

var linkFont = FontFactory.GetFont(FontFactory.HELVETICA, 9.0f, Font.Underline, BaseColor.BLUE);

我必须做什么才能创建一个看起来像链接的字体。我让它工作正常,具有:

var linkFont = FontFactory.GetFont(FontFactory.HELVETICA, 9, BaseColor.BLUE);
Anchor anchor = new Anchor("Adobe Reader", linkFont);
anchor.Reference = "http://www.adobe.com";
PdfPTable tbl = new PdfPTable(1);
tbl.WidthPercentage = 50;
tbl.HorizontalAlignment = Element.ALIGN_LEFT;
var par = new Paragraph();
par.Add(boldpart);
par.Add(ini);
par.Add(anchor);

。但是"锚点"只是蓝色文本,没有下划线,因此显然不是链接/可点击的。

两个问题。

首先,您尝试使用 5 个参数重载,但只传递了 4 个参数。

其次,当你尝试使用Font.Underline时,你实际上是在用System.Drawing.Font.Underline而不是iText。

除非需要将编码开关指定为:

var linkFont = FontFactory.GetFont(FontFactory.HELVETICA, 9, iTextSharp.text.Font.UNDERLINE, BaseColor.BLUE);

它看起来是正确的。但是,当我在我的机器上尝试它时,Font.Underline抛出了一个错误。唯一可用的常量是字体。大写划线

var linkFont = FontFactory.GetFont(FontFactory.HELVETICA, 9.0f, Font.UNDERLINE, BaseColor.BLUE);

你能检查字体类的命名空间吗?看起来这就是错误所在。它应该来自 namepance iTextSharp.text.Font

您正在使用 Control.Font.Underline,这是一个布尔值并且不正确。如上所述,使用 iTextSharp 中的 Font 类。

它认为第三个参数是布尔值的原因是Font.Underline是布尔值! 您需要使用FontStyle.Underline

编辑:FontStyle.Underline是System.Drawing.Font.Underline,iTextSharp不使用。 它具有为字体样式定义的常量,应改用这些常量:

  /// <summary> this is a possible style. </summary>
    public const int NORMAL        = 0;
    /// <summary> this is a possible style. </summary>
    public const int BOLD        = 1;
    /// <summary> this is a possible style. </summary>
    public const int ITALIC        = 2;
    /// <summary> this is a possible style. </summary>
    public const int UNDERLINE    = 4;
    /// <summary> this is a possible style. </summary>
    public const int STRIKETHRU    = 8;
    /// <summary> this is a possible style. </summary>
    public const int BOLDITALIC    = BOLD | ITALIC;

最新更新