我正在尝试使用 PDFBox 1.8.9 将 PDF 转换为图像。字体越来越重叠。我知道这个问题不会出现在 PDFBox 2.0 SNAPSHOT 中。但在正式发布之前不能用于生产。
下面是代码:
PDDocument pdDocument = PDDocument.load(new File("test.pdf"));
List<PDPage> pages = pdDocument.getDocumentCatalog().getAllPages();
int pageCounter = 1;
for (PDPage page : pages) {
BufferedImage bufferedImage = page.convertToImage();
File imageFile = new File(String.format("/tmp/pdf-image-%s.jpg", pageCounter));
ImageIO.write(bufferedImage, "jpg", imageFile);
pageCounter++;
}
除了重叠之外,字体看起来与 PDF 类似?
当我尝试通过命令行使用 pdfbox-app-1.8.9.jar 将 PDF 转换为图像时,图像会使用不同的字体生成。 PDFBox 是否有任何选项可以使用 .ttf 指定自定义字体?那么,我该如何指定呢?
实际 PDF:
使用上述代码转换的图像:
使用命令行转换图像:
最佳答案
当在服务器上找不到 Helvetica 和 Times New Roman 等字体时,PDFBox 会替换该字体。在我的Linux机器上安装上述字体后,问题就解决了。
关于使用 PDFBox 1.8.9 文本重叠的 PDF 到图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30725009/