【问题标题】:PDFBox True Type font boldPDFBox True Type 字体加粗
【发布时间】:2021-02-16 20:49:36
【问题描述】:

我正在开发一个应用程序,它必须创建一个具有不同字体样式(有时是粗体,有时是斜体,有时是常规)的 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 中一样以粗体或斜体打开字体?

【问题讨论】:

  • 在操作系统级别上,粗体、斜体和斜体+粗体实际上是不同的字体文件(或作为包含这些字体的“字体集合”的一个文件)。例如,如果您使用的是 windows,请在 c:\windows\fonts 中执行“dir arial*”。
  • 关于你的编辑 - “它可以使用不同的字体变体来完成,或者通过使用原始命令人为地完成。我想要的是使用原始字体并将一些文本设置为粗体,其他文本斜体,其余的只是常规的。” - pdfbox 没有高级文本样式 API。使用其他字体文件或隐式更改图形状态是这样一个高级 API 将为您做的事情。因此,您必须明确地执行此操作。

标签: pdfbox


【解决方案1】:

感谢您的 cmets 和建议。最后我使用 PDFPageContentStream 类的 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 语言编写的。

【讨论】:

  • 我们如何应用斜体样式?
【解决方案2】:

这里有一个完整的例子来解释如何将使用的字体呈现为斜体和粗体:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-20
    • 2011-11-09
    • 1970-01-01
    • 2021-10-01
    • 2012-04-19
    • 1970-01-01
    • 2014-05-31
    • 2017-11-20
    相关资源
    最近更新 更多