使用 PDFBox 正确渲染 PDF 到图像

Rendering PDF to image with PDFBox properly

因此,在此应用程序中,我们使用 iText 填写 PDF 表单,使用 PDFBox 加载填写好的 PDF 并将其转换为图像到我们的系统中。

问题出在图像转换时。所有的信息都在那里,但是复选框……很奇怪?而不是在 PDF 上设置的样式复选框 "check mark",复选框内部有一个奇怪的 "empty box"。

应该如何:

PDFBox 版本 2.0.11 iText 版本为 5.5.13

这是发生转换的一小段代码:

PDDocument pdf = PDDocument.load(byteArrayInputStream);
PDFRenderer renderer = new PDFRenderer(pdf);
BufferedImage[] images = new BufferedImage[pdf.getNumberOfPages()];
PDPage page = null;
BufferedImage image = null;
for (int i = 0; i < images.length; i++) {
        try {
            image = renderer.renderImageWithDPI(i, 300,org.apache.pdfbox.rendering.ImageType.RGB);
            ...

转换后我也感觉到了 "loss of quality"。之前,我们使用的是 PDFBox 1.8,转换质量很低,并且丢失了一些字体格式和样式。自从升级后它变得更好了,但仍然有问题。

填充发生的位置:

PdfReader reader = new PdfReader(filePath);

ByteArrayOutputStream lStr = new ByteArrayOutputStream();
PdfStamper stamper = new PdfStamper(reader, lStr);
AcroFields acroFields = stamper.getAcroFields();

for (Entry<String, Item> map : acroFields.getFields().entrySet()) {
    String key = map.getKey();

    if (!fields.has(key))
        continue;

    if (fields.isNull(key))
        continue;

    acroFields.setField(key, fields.getString(key), true);
}
stamper.setFormFlattening(true);

stamper.close();
reader.close();

...

你们知道这是什么吗?

谢谢!

感谢 Tilman Hausherr 的建议,它开始工作了。问题确实是服务器 运行 应用程序中缺少字体。 (Zapf Dingbats and/or MS Gothic)。

在目录“./fonts”或“/usr/share/fonts”(Linux)/“/Windows/Fonts”(Windows)目录中安装缺少的字体就成功了!