【问题标题】:How to embed an standard font into generated PDF with PDFBox如何使用 PDFBox 将标准字体嵌入到生成的 PDF 中
【发布时间】:2018-12-18 00:46:54
【问题描述】:

我需要使用适用于 Java 的 Apache PDFBox 库向 PDF/A 文件添加一些文本。问题是,因为它需要是一个有效的 PDF/A 文件,所有使用的字体都必须嵌入其中。我知道我可以使用 PDFBox 嵌入 TTF 字体,但我想避免在应用程序中提供字体文件,所以我想知道是否有办法嵌入 PDFBox 中可用的标准字体之一,就好像它一样是外部的。

例如,当我使用其中一种标准字体编写内容时,PDF 验证器会抱怨:

我使用以下代码来编写文本:

  PDFont standardFont = PDType1Font.HELVETICA_BOLD;
  
  PDPage pag = new PDPage();
  
  pag.setResources(new PDResources());
  
  PDPageContentStream contentStream = new PDPageContentStream(pdfFile, pag);
  
  //Begin the Content stream 
  contentStream.beginText();       
  
  //Setting the font to the Content stream  
  contentStream.setFont(standardFont, 12);

  //Setting the position for the line 
  contentStream.newLineAtOffset(25, 500);

  //Adding text in the form of string 
  contentStream.showText("JUST A SAMPLE STRING");      

  //Ending the content stream
  contentStream.endText();

  //Closing the content stream
  contentStream.close();
  
  pdfFile.addPage(pag);
  
  pdfFile.save(file);
  
  pdfFile.close();

设置时有没有强制嵌入字体的选项?

提前致谢,

【问题讨论】:

  • “我想知道是否有办法嵌入 PDFBox 中可用的标准字体之一,就好像它是外部的一样。” - 你的意思是可用的那些字体在 pdfbox 中,因为它们在每个 pdf 查看器中都可用。 Pdfbox 并不完全包含这些字体,它只知道它们的一些元数据。因此,要嵌入字体,您确实需要字体文件。

标签: java pdfbox


【解决方案1】:

PDFBox 中只嵌入了一种字体。你可以这样使用它:

PDFont font = PDType0Font.load(doc, SomePdfboxClass.class.getResourceAsStream(
                   "/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    相关资源
    最近更新 更多