【发布时间】:2017-10-28 03:54:30
【问题描述】:
如何设置 pdfptable 的字体?
【问题讨论】:
-
有什么比你们给我看的更简单的吗?也许只是 setfont(size 12) 之类的?
-
不行,你必须先注册一个字体。
-
就像杰森说的。 PdfPTable 对象比常规 Table 对象提供了更多的灵活性,但使用起来更复杂。
如何设置 pdfptable 的字体?
【问题讨论】:
创建短语时必须在每个单元格中设置字体:
Dim yourFont As BaseFont = BaseFont.CreateFont( _
Current.Server.MapPath("~/fonts/somefont.TTF"), _
BaseFont.WINANSI, BaseFont.EMBEDDED)
Dim mainFont As New Font(yourFont, SOME_FONT_SIZE, Font.NORMAL)
Dim cell As New PdfPCell(New Phrase("some text", mainFont))
yourTable.Add(cell)
【讨论】:
您需要创建一个与 iTextSharp 中的常规字体对象略有不同的“基本字体”对象。您将字体分配给为 PdfPTable 创建的每个元素(短语、段落等)。
Dim bfR As iTextSharp.text.pdf.BaseFont
bfR = iTextSharp.text.pdf.BaseFont.CreateFont("verdana.ttf", iTextSharp.text.pdf.BaseFont.IDENTITY_H, iTextSharp.text.pdf.BaseFont.EMBEDDED)
我在这里使用 IDENTITY_H 属性,以便启用对其他字母的支持。
【讨论】:
属性 PdfPTable 有一个属性 DefaultCell,您可以设置 PdfPCell 元素的默认属性:
//C#
tableInstance.DefaultCell.Phrase = new Phrase() { Font = yourFont };
【讨论】: