如何使用 PDFBox 将标准字体嵌入到生成的 PDF 中
How to embed an standard font into generated PDF with PDFBox
我需要使用 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 内嵌的字体只有一种。你可以这样使用它:
PDFont font = PDType0Font.load(doc, SomePdfboxClass.class.getResourceAsStream(
"/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));
我需要使用 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 内嵌的字体只有一种。你可以这样使用它:
PDFont font = PDType0Font.load(doc, SomePdfboxClass.class.getResourceAsStream(
"/org/apache/pdfbox/resources/ttf/LiberationSans-Regular.ttf"));