pdfBox 将 pdf 转换为具有透明度的图像

pdfBox convert pdf to image with transparency

目前为止一切正常,但如何为生成的图像添加透明度?

for (img <- 0 until f.length) {
    val inputPdf = PDDocument.load(f(img).getPath).getDocumentCatalog.getAllPages.get(0).asInstanceOf[PDPage]

    val outputfile = new File(f(img).getName + ".png")
    ImageIO.write(inputPdf.convertToImage(), "png", outputfile)
}

此致 托尔斯滕

尝试使用 convertToImage(type, resolution) with TYPE_INT_ARGB.

您可以查看 convertToImage 的代码:http://codenav.org/code.html?project=/org/apache/pdfbox/pdfbox/1.8.4&path=/Source%20Packages/org.apache.pdfbox.pdmodel/PDPage.java (1.8.4) or https://svn.apache.org/repos/asf/pdfbox/tags/1.8.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPage.java (1.8.8-current)

public BufferedImage convertToImage() throws IOException
{
    //note we are doing twice as many pixels because
    //the default size is not really good resolution,
    //so create an image that is twice the size
    //and let the client scale it down.
    return convertToImage(8, 2 * DEFAULT_USER_SPACE_UNIT_DPI);
}

您可能想使用:

convertToImage(BufferedImage.TYPE_INT_ARGB, 2 * DEFAULT_USER_SPACE_UNIT_DPI);

注意:PDF 支持透明对象。但是,正如@mkl 所述,它与 pdf 参考不兼容。