PDF Box generating blank images due to JBIG2 Images in it

爷,独闯天下 提交于 2019-12-08 17:34:30

问题


Let me give you an overview of my project first. I have a pdf which I need to convert into images(One image for one page) using PDFBox API and write all those images onto a new pdf using PDFBox API itself. Basically, converting a pdf into a pdf, which we refer to as PDF Transcoding.

For certain pdfs, which contain JBIG2 images, PDFbox implementation of convertToImage() method is failing silently without any exceptions or errors and finally, producing a PDF, but this time, just with blank content(white). The message I am getting on the console is:

Dec 06, 2013 5:15:42 PM org.apache.pdfbox.filter.JBIG2Filter decode
SEVERE: Can't find an ImageIO plugin to decode the JBIG2 encoded datastream.
Dec 06, 2013 5:15:42 PM org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap getRGBImage
SEVERE: Something went wrong ... the pixelmap doesn't contain any data.
Dec 06, 2013 5:15:42 PM org.apache.pdfbox.util.operator.pagedrawer.Invoke process
WARNING: getRGBImage returned NULL

I need to know how to resolve this issue? We have something like:

import org.apache.pdfbox.filter.JBIG2Filter; 

which I don't know how to implement.

I am searching on that, but to no avail. Could anyone please suggest?


回答1:


Take a look at this ticket in PDFBox https://issues.apache.org/jira/browse/PDFBOX-1067 . I think the answer to your question is:

  1. to make sure that you have JAI and the JAI-ImageIO plugins installed for your version of Java: decent installation instructions are available here: http://docs.geoserver.org/latest/en/user/production/java.html
  2. to use the JBIG2-imageio plugin, (newer versions are licensed under the Apache2 license) https://github.com/levigo/jbig2-imageio/



回答2:


I had the exact same problem. I downloaded the jar from jbig2-imageio and I just included it in my project's application libraries, and it worked right out of the box. As adam said, it uses GPL3.




回答3:


Installing the JAI seems not needed. I only needed to download the levigo-jbig2-imageio-1.6.5.jar, place it in the folder of my dependency-jars and in eclipse add it to the java build path libraries. https://github.com/levigo/jbig2-imageio/




回答4:


I had the same problem and I fixed it by adding this dependency in my pom.xml :

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>jbig2-imageio</artifactId>
    <version>3.0.2</version>
</dependency>

Good luck.




回答5:


import java.awt.image.BufferedImage
import org.apache.pdfbox.cos.COSName

import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPage
import org.apache.pdfbox.pdmodel.PDPageTree
import org.apache.pdfbox.pdmodel.PDResources
import org.apache.pdfbox.pdmodel.graphics.PDXObject
import org.apache.pdfbox.rendering.ImageType
import org.apache.pdfbox.rendering.PDFRenderer
import org.apache.pdfbox.tools.imageio.ImageIOUtil


import javax.imageio.ImageIO
import javax.imageio.spi.IIORegistry
import javax.imageio.spi.ImageReaderSpi
import javax.swing.*
import javax.swing.filechooser.FileNameExtensionFilter

public class savePDFAsImage{

    String path = "c:/pdfImage/"

    //allow pdf file selection for extracting
    public static File selectPDF() {
        File file = null
        JFileChooser chooser = new JFileChooser()
        FileNameExtensionFilter filter = new FileNameExtensionFilter("PDF", "pdf")
        chooser.setFileFilter(filter)
        chooser.setMultiSelectionEnabled(false)
        int returnVal = chooser.showOpenDialog(null)
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile()
           println "Please wait..."
        }
        return file
    }

    public static void main(String[] args) {
        try {
 // help to view list of plugin registered. check by adding JBig2 plugin and JAI plugin
            ImageIO.scanForPlugins()
            IIORegistry reg = IIORegistry.getDefaultInstance()
            Iterator spIt = reg.getServiceProviders(ImageReaderSpi.class, false)
            spIt.each(){
                println it.getProperties()
            }
            testPDFBoxSaveAsImage()
            testPDFBoxExtractImagesX()
        } catch (Exception e) {
            e.printStackTrace()
        }
    }    

    public static void testPDFBoxExtractImagesX() throws Exception {
        PDDocument document = PDDocument.load(selectPDF())
        PDPageTree list = document.getPages()
        for (PDPage page : list) {
            PDResources pdResources = page.getResources()
            for (COSName c : pdResources.getXObjectNames()) {
                PDXObject o = pdResources.getXObject(c)
                if (o instanceof org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) {
                    File file = new File( + System.nanoTime() + ".png")
                    ImageIO.write(((org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject) o).getImage(), "png", file)
                }
            }
        }
        document.close()
        println "Extraction complete"
    }
    public static void testPDFBoxSaveAsImage() throws Exception {
        PDDocument document = PDDocument.load(selectPDF().getBytes())
        PDFRenderer pdfRenderer = new PDFRenderer(document)
        for (int page = 0; page < document.getNumberOfPages(); ++page) {
            BufferedImage bim = pdfRenderer.renderImageWithDPI(page,300, ImageType.BINARY)
            // suffix in filename will be used as the file format
            OutputStream fileOutputStream = new FileOutputStream(+ System.nanoTime() + ".png")
            boolean b = ImageIOUtil.writeImage(bim, "png",fileOutputStream,300)
        }
        document.close()
        println "Extraction complete"
    }
}


来源:https://stackoverflow.com/questions/20424796/pdf-box-generating-blank-images-due-to-jbig2-images-in-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!