Java 如何在使用pdfbox将pdf转换为html时添加自定义字体?
我使用的是pdfbox-2.0.9,我编写了一个将pdf转换为PDDocument的代码,并使用PDFDomTree解析dom树并生成一个html文档,但在输出html时字体变得混乱。我意识到有14种标准字体,而我需要的字体不在标准列表中 那么,在Java 如何在使用pdfbox将pdf转换为html时添加自定义字体?,java,html,pdf,pdfbox,Java,Html,Pdf,Pdfbox,我使用的是pdfbox-2.0.9,我编写了一个将pdf转换为PDDocument的代码,并使用PDFDomTree解析dom树并生成一个html文档,但在输出html时字体变得混乱。我意识到有14种标准字体,而我需要的字体不在标准列表中 那么,在PDDocument.load(pdfFile)method调用之前或之后,是否有任何方法可以为自定义字体添加种子,或者使用自定义字体将pdf转换为html的其他解决方案 public String convertToHtml(File pdfFile
PDDocument.load(pdfFile)
method调用之前或之后,是否有任何方法可以为自定义字体添加种子,或者使用自定义字体将pdf转换为html的其他解决方案
public String convertToHtml(File pdfFile, String destinationDir) {
if (!destinationDir.endsWith("/")) {
destinationDir += "/";
}
String outfile = "";
String fileName = pdfFile.getName();
if (fileName.toLowerCase().endsWith(".pdf"))
fileName = fileName.substring(0, fileName.length() - 4);
outfile = destinationDir + fileName + ".html";
PDDocument document = null;
try {
File outputHtml = new File(outfile);
outputHtml.getParentFile().mkdirs();
outputHtml.createNewFile();
document = PDDocument.load(pdfFile);
PDFDomTree parser = new PDFDomTree(PDFDomTreeConfig.createDefaultConfig());
Writer output = new PrintWriter(outfile, "utf-8");
parser.writeText(document, output);
output.close();
} catch (Exception e) {
logger.log(Level.SEVERE, "Error: ", e.getMessage());
} finally {
if (document != null) {
try {
document.close();
} catch (IOException e) {
logger.log(Level.SEVERE, "Error: " + e.getMessage());
}
}
}
return outfile;
}
提前谢谢。您说您“已经编写了将pdf转换为html的代码,但字体越来越乱”。那么,我们应该如何帮助您,而您甚至不显示关键代码?啊,那么您正在使用解析器。这个解析器不是PDFBox的一部分,它只是基于它。您可能想让您使用的解析器在您的问题中更加突出。@mkl感谢您指出,我已经更新了我的问题,您现在能帮我找到解决方案吗?不幸的是,我根本不知道Pdf2DOM,但这很可能是问题的起因,所以我不知道。这就是为什么我要你在你的问题中指出它是关于Pdf2DOM的。。。