【发布时间】: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 并不完全包含这些字体,它只知道它们的一些元数据。因此,要嵌入字体,您确实需要字体文件。