PDFBox True Type 字体加粗
PDFBox True Type font bold
我正在开发一个应用程序,该应用程序必须创建具有不同字体样式(有时是粗体、有时是斜体、有时是常规字体)的 PDF 文件。我必须使用的字体是 Eras Medium BT (True Type),我使用名为 "erasm.TTF" 的本地文件加载它。我的问题是,如何使用我的 Eras 字体文件绘制粗体或斜体文本?
我有一个使用 iText 生成类似 PDF 的遗留代码,要获得粗体字体,我只需要调用此函数:
public Font getFontErasMDBTBold9(){
FontFactory.register(fontPath + "erasm.TTF", "ERASM");
fontErasMDBT9 = FontFactory.getFont("ERASM", 9, Font.BOLD, Color.BLACK);
return fontErasMDBT9;
}
编辑:
我在其他问题中看到可以使用不同的字体变体或使用原始命令人为地完成。我想要的是使用原始字体并将一些文本设置为粗体,其他文本设置为斜体,其余的只是常规。
是否可以像 iText 一样以粗体或斜体打开字体?
感谢您的意见和建议。最后,我使用 PDFPageContentStream class 的 setRenderingMode 方法来设置我的文本的不同样式。这是一个私有方法,可以用所需的呈现模式编写一些文本:
private void writeText(PDPageContentStream contentStream, String text, PDFont font,
int size, float xPos, float yPos, RenderingMode renderMode = RenderingMode.FILL) {
contentStream.beginText()
contentStream.setFont(font, size)
contentStream.newLineAtOffset(xPos, yPos)
contentStream.setRenderingMode(renderMode)
contentStream.showText(text)
contentStream.endText()
}
这里是编写常规文本和粗体文本的代码。
private void addFrontPage(PDDocument document) {
PDPage frontPage = newPage()
PDPageContentStream contentStream = new PDPageContentStream(document, frontPage)
// Write text
String text = "This is a bold text"
writeText(contentStream, text, eras, 18, 25, 500, RenderingMode.FILL_STROKE)
text = "and this is a regular text"
writeText(contentStream, text, eras, 9, 25, 480)
contentStream.close()
document.addPage(frontPage)
}
注意:代码是用Groovy语言编写的。
这里有一个完整的例子来解释如何将使用的字体呈现为斜体和粗体:
String message = "This is a message in the page.";
PDDocument document = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream = new PDPageContentStream( document, page, AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.setFont( font, fontSize ); // set font and font size.
contentStream.setNonStrokingColor( 1f, 0, 0 ); // set text color to red
// Modify font to appear in Italic:
Matrix matrix = new Matrix( 1, 0, .2f, 1, 7, 5 );
contentStream.setTextMatrix( matrix );
// Modify the font to appear in bold:
contentStream.setRenderingMode( RenderingMode.FILL_STROKE );
contentStream.setStrokingColor( 1f, 0, 0 );
// Write text:
contentStream.showText( message );
contentStream.endText();
contentStream.close();
document.addPage( page );
document.save( PDF_FILE_PATH );
document.close();
我正在开发一个应用程序,该应用程序必须创建具有不同字体样式(有时是粗体、有时是斜体、有时是常规字体)的 PDF 文件。我必须使用的字体是 Eras Medium BT (True Type),我使用名为 "erasm.TTF" 的本地文件加载它。我的问题是,如何使用我的 Eras 字体文件绘制粗体或斜体文本?
我有一个使用 iText 生成类似 PDF 的遗留代码,要获得粗体字体,我只需要调用此函数:
public Font getFontErasMDBTBold9(){
FontFactory.register(fontPath + "erasm.TTF", "ERASM");
fontErasMDBT9 = FontFactory.getFont("ERASM", 9, Font.BOLD, Color.BLACK);
return fontErasMDBT9;
}
编辑: 我在其他问题中看到可以使用不同的字体变体或使用原始命令人为地完成。我想要的是使用原始字体并将一些文本设置为粗体,其他文本设置为斜体,其余的只是常规。
是否可以像 iText 一样以粗体或斜体打开字体?
感谢您的意见和建议。最后,我使用 PDFPageContentStream class 的 setRenderingMode 方法来设置我的文本的不同样式。这是一个私有方法,可以用所需的呈现模式编写一些文本:
private void writeText(PDPageContentStream contentStream, String text, PDFont font,
int size, float xPos, float yPos, RenderingMode renderMode = RenderingMode.FILL) {
contentStream.beginText()
contentStream.setFont(font, size)
contentStream.newLineAtOffset(xPos, yPos)
contentStream.setRenderingMode(renderMode)
contentStream.showText(text)
contentStream.endText()
}
这里是编写常规文本和粗体文本的代码。
private void addFrontPage(PDDocument document) {
PDPage frontPage = newPage()
PDPageContentStream contentStream = new PDPageContentStream(document, frontPage)
// Write text
String text = "This is a bold text"
writeText(contentStream, text, eras, 18, 25, 500, RenderingMode.FILL_STROKE)
text = "and this is a regular text"
writeText(contentStream, text, eras, 9, 25, 480)
contentStream.close()
document.addPage(frontPage)
}
注意:代码是用Groovy语言编写的。
这里有一个完整的例子来解释如何将使用的字体呈现为斜体和粗体:
String message = "This is a message in the page.";
PDDocument document = new PDDocument();
PDPage page = new PDPage();
PDPageContentStream contentStream = new PDPageContentStream( document, page, AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.setFont( font, fontSize ); // set font and font size.
contentStream.setNonStrokingColor( 1f, 0, 0 ); // set text color to red
// Modify font to appear in Italic:
Matrix matrix = new Matrix( 1, 0, .2f, 1, 7, 5 );
contentStream.setTextMatrix( matrix );
// Modify the font to appear in bold:
contentStream.setRenderingMode( RenderingMode.FILL_STROKE );
contentStream.setStrokingColor( 1f, 0, 0 );
// Write text:
contentStream.showText( message );
contentStream.endText();
contentStream.close();
document.addPage( page );
document.save( PDF_FILE_PATH );
document.close();