itextpdf / PdfContentByte / Font - 如何组合两个或多个粗体、斜体和下划线
itextpdf / PdfContentByte / Font - How to combine 2 or more of BOLD, ITALICS AND UNDERLINE
作为我正在进行的项目的一部分,我需要使用数据库中的内容、位置、文本样式来创建 PDF 文件。我正在使用 PdfContentByte
向文档添加文本。内容、位置和文本大小工作正常,但我无法理解如何合并可能是粗体、斜体和下划线中的一种或多种组合的文本样式。我已经查看了许多关于 SOF 的其他问题,但无法使其正常工作。
我试图做的是使用数据库中的文本样式创建一个 Font
,然后使用 Font.getBaseFont()
获得一个 BaseFont
,如下所示。
Font custFont = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.BOLDITALIC, BaseColor.BLACK);
// "/fonts/arial.ttf", Font.BOLDITALIC, BaseColor.BLACK ETC BASED ON THE FONT STYLE FROM DB
BaseFont baseFont = custFont.getBaseFont();
pdfContentByte.setFontAndSize(baseFont, fontSizeFromDB);
pdfContentByte.showTextAligned(Element.ALIGN_LEFT, "Text From DB", xPosFromDB, yPosFromDB, 0);
这不起作用。只有字体系列被复制到 baseFont
对象中。
但是,当我在如下字体样式的段落中使用字体时,效果很好。 (但我不能使用它,因为我需要将内容插入到我需要使用的特定位置PdfContentByte
)
Font custFontTimes = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD | Font.UNDERLINE);
Paragraph paragraphCusTime = new Paragraph("Paragraph text in custFontTimes", custFontTimes);
document.add(paragraphCusTime);
您可以通过使用组合 BoldItalic
样式修饰符创建基本字体来实现此目的。使用 pdfContentByte.setFontAndSize
的限制是您只能使用 14 种内置类型中的一种,而 TIMES_BOLDITALIC
是唯一符合您条件的类型。
看来您使用的是 iText 2.1.7,我没有它的副本来测试它是否有效,但是使用内置字体的简单解决方案可能如下所示:
BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_BOLDITALIC, BaseFont.CP1257, BaseFont.EMBEDDED);
或者您可以尝试使用带有样式修饰符的 TrueType 字体。您需要使用它来找到同时具有粗体和斜体的字体,但是当您找到一种字体时,它可能看起来像这样:
BaseFont baseFont = BaseFont.createFont("Arial-CE-Bold-Italic", BaseFont.CP1257, BaseFont.EMBEDDED);
请注意,嵌入字体可能会出现异常,您可能需要将 embedded 设置为 false。
我已经有很长时间没有使用 iText 2.1.7 了,但它确实有很多这样的限制,我强烈建议以后使用最新版本的 iText,尤其是考虑到它很难找到旧版本的示例。
API 引用:
BaseFont createFont(java.lang.String name,
java.lang.String encoding,
boolean embedded,
boolean forceRead)
throws DocumentException,
java.io.IOException
Creates a new font. This font can be one of the 14 built in types, a
Type1 font referred to by an AFM or PFM file, a TrueType font (simple
or collection) or a CJK font from the Adobe Asian Font Pack. TrueType
fonts and CJK fonts can have an optional style modifier appended to
the name. These modifiers are: Bold, Italic and BoldItalic. An example
would be "STSong-Light,Bold".
作为我正在进行的项目的一部分,我需要使用数据库中的内容、位置、文本样式来创建 PDF 文件。我正在使用 PdfContentByte
向文档添加文本。内容、位置和文本大小工作正常,但我无法理解如何合并可能是粗体、斜体和下划线中的一种或多种组合的文本样式。我已经查看了许多关于 SOF 的其他问题,但无法使其正常工作。
我试图做的是使用数据库中的文本样式创建一个 Font
,然后使用 Font.getBaseFont()
获得一个 BaseFont
,如下所示。
Font custFont = FontFactory.getFont("/fonts/arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 0.8f, Font.BOLDITALIC, BaseColor.BLACK);
// "/fonts/arial.ttf", Font.BOLDITALIC, BaseColor.BLACK ETC BASED ON THE FONT STYLE FROM DB
BaseFont baseFont = custFont.getBaseFont();
pdfContentByte.setFontAndSize(baseFont, fontSizeFromDB);
pdfContentByte.showTextAligned(Element.ALIGN_LEFT, "Text From DB", xPosFromDB, yPosFromDB, 0);
这不起作用。只有字体系列被复制到 baseFont
对象中。
但是,当我在如下字体样式的段落中使用字体时,效果很好。 (但我不能使用它,因为我需要将内容插入到我需要使用的特定位置PdfContentByte
)
Font custFontTimes = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD | Font.UNDERLINE);
Paragraph paragraphCusTime = new Paragraph("Paragraph text in custFontTimes", custFontTimes);
document.add(paragraphCusTime);
您可以通过使用组合 BoldItalic
样式修饰符创建基本字体来实现此目的。使用 pdfContentByte.setFontAndSize
的限制是您只能使用 14 种内置类型中的一种,而 TIMES_BOLDITALIC
是唯一符合您条件的类型。
看来您使用的是 iText 2.1.7,我没有它的副本来测试它是否有效,但是使用内置字体的简单解决方案可能如下所示:
BaseFont baseFont = BaseFont.createFont(BaseFont.TIMES_BOLDITALIC, BaseFont.CP1257, BaseFont.EMBEDDED);
或者您可以尝试使用带有样式修饰符的 TrueType 字体。您需要使用它来找到同时具有粗体和斜体的字体,但是当您找到一种字体时,它可能看起来像这样:
BaseFont baseFont = BaseFont.createFont("Arial-CE-Bold-Italic", BaseFont.CP1257, BaseFont.EMBEDDED);
请注意,嵌入字体可能会出现异常,您可能需要将 embedded 设置为 false。
我已经有很长时间没有使用 iText 2.1.7 了,但它确实有很多这样的限制,我强烈建议以后使用最新版本的 iText,尤其是考虑到它很难找到旧版本的示例。
API 引用:
BaseFont createFont(java.lang.String name, java.lang.String encoding, boolean embedded, boolean forceRead) throws DocumentException, java.io.IOException
Creates a new font. This font can be one of the 14 built in types, a Type1 font referred to by an AFM or PFM file, a TrueType font (simple or collection) or a CJK font from the Adobe Asian Font Pack. TrueType fonts and CJK fonts can have an optional style modifier appended to the name. These modifiers are: Bold, Italic and BoldItalic. An example would be "STSong-Light,Bold".