简体   繁体   中英

pdfBox convert pdf to image with transparency

This works fine so far, but how to add transparency to the generated images ?

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

Best regards Torsten

Try using convertToImage(type, resolution) with TYPE_INT_ARGB .

You can peek code of 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);
}

You probably want to use:

convertToImage(BufferedImage.TYPE_INT_ARGB, 2 * DEFAULT_USER_SPACE_UNIT_DPI);

NOTE: PDF support transparent object. However, as stated by @mkl it is not compatible with pdf reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM