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

时间:2015-01-09 15:19:11

标签: java pdfbox

到目前为止这个工作正常,但是如何为生成的图像添加透明度?

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)
}

祝你好运 托

1 个答案:

答案 0 :(得分:2)

尝试将convertToImage(type, resolution)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)或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参考不兼容。