我正在使用 PDFBox 3.0.0.alpha3 来操作 PDF 文件并使用 Roboto 字体添加文本。在Windows上安装字体可以正常显示,但在没有安装字体的不同机器上打开时,字符显示不正确
这是我用来加载字体和操作 pdf 文件的代码:
private PDType0Font GetFont(PDDocument document, DocumentMemberConfigDto config) throws IOException {
String fontFileName = "path to file . ttf";
if (!Files.exists(fontFileName )) {
fontFileName = DefaultFont;
}
PDType0Font Font = PDType0Font.load(document,
new File(fontFileName ));
return Font;
}
public void PutTextFromConfig(DocumentMemberConfigDto conf) throws IOException {
SelectPage((int) conf.PageNumero - 1);
PDType0Font font = GetFont(document, conf);
PDFontDescriptor fontDescriptor = font.getFontDescriptor();
float fontHeight = fontDescriptor.getFontBoundingBox().getHeight() / 1000 * conf.FondSize;
PDRectangle mediaBox = this.currentPage.getMediaBox();
float pageHeight = mediaBox.getHeight();
String Text = conf.fieldLibelle == " Liste déroulante " ? conf.Valeur.split("-")[0] : conf.Valeur;
addTextWithTooltip(Text,
(float) conf.X,
(float) (pageHeight - conf.Y - (fontHeight * 0.75)),
font,
(int) conf.FondSize,
conf.infobull,
conf.isItalic,
conf.isBold,
conf.isUnderline,
getColor(conf.Fill));
}
public PDFHandling(PDDocument document) throws IOException {
this.document = document;
}
public void init() throws IOException {
SelectPage(0);
}
public void SelectPage(int numPage) throws IOException {
if (document.getNumberOfPages() - 1 >= numPage) {
numPage = document.getNumberOfPages() - 1;
}
if (this.contentStream != null)
this.contentStream.close();
currentNumPage = numPage;
currentPage = document.getPage(numPage);
this.contentStream = new PDPageContentStream(document, currentPage, AppendMode.PREPEND, false);
}
private Color getColor(String colorCode) {
if (colorCode.equals("red"))
return Color.RED;
else if (colorCode.equals("orange"))
return Color.ORANGE;
else if (colorCode.equals("green"))
return Color.GREEN;
else if (colorCode.equals("white"))
return Color.white;
else
return Color.BLACK;
}
我像这样使用这个功能:
private byte[] AddFields(DocumentToSignDto documentToSigne) throws Exception {
byte[] content = documentToSigne.Content;
PDDocument document = Loader.loadPDF(content);
String[] fieldLibelleTexts = new String[] { "Fonction", "E-mail", "Nom", "champ de saisie", "société", "Tel",
"Texte", "Date signature", " Liste déroulante " };
try (PDFHandling pdfHandling = new PDFHandling(document)) {
pdfHandling.init();
for (DocumentMemberConfigDto conf : documentToSigne.Configs) {
if (conf.fieldLibelle != null && !conf.fieldLibelle.equals("")) {
if (Arrays.asList(fieldLibelleTexts).contains(conf.fieldLibelle)) {
pdfHandling.PutTextFromConfig(conf);
} else if (conf.fieldLibelle.equals(" Case à cocher") && conf.isChecked == true) {
pdfHandling.PutChekboxFromConfig(conf);
} else if (conf.fieldLibelle.equals(" Bouton radio ") && conf.isChecked == true) {
pdfHandling.PutRadioBoxFromConfig(conf);
} else {
}
}
}
}
ByteArrayOutputStream baosAfterFildes = new ByteArrayOutputStream();
document.saveIncremental(baosAfterFildes);
return baosAfterFildes.toByteArray();
}
当我检查生成的 PDF 文件中的字体属性时(文件 > 属性 > 字体),它显示: 机器人-常规 类型:TrueType (CID) 编码:Identity-H 实际字体:未知
什么可能导致字体无法正确嵌入 PDF 文件?如何确保 Roboto 字体在不同设备和 PDF 阅读器中正确嵌入和显示?