11

先给大家介绍一下我的项目吧。我有一个 pdf,我需要使用 API 将其转换为图像(一页一张图像),并使用API 本身PDFBox将所有这些图像写入一个新的 pdf 。PDFBox基本上,将pdf转换为pdf,我们称之为PDF转码。

对于某些包含 JBIG2 图像的 pdf,convertToImage()方法的 PDFbox 实现无声无息地失败,没有任何异常或错误,最后生成 PDF,但这一次,只有空白内容(白色)。我在控制台上收到的消息是:

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

我需要知道如何解决这个问题?我们有类似的东西:

import org.apache.pdfbox.filter.JBIG2Filter; 

我不知道如何实施。

我正在寻找它,但无济于事。有人可以建议吗?

4

5 回答 5

11

在 PDFBox https://issues.apache.org/jira/browse/PDFBOX-1067中查看这张票。我认为你的问题的答案是:

  1. 确保您为您的 Java 版本安装了 JAI 和 JAI-ImageIO 插件:可在此处获得体面的安装说明:http ://docs.geoserver.org/latest/en/user/production/java.html
  2. 使用 JBIG2-imageio 插件,(较新的版本在 Apache2 许可下获得许可)https://github.com/levigo/jbig2-imageio/
于 2014-01-10T23:20:16.840 回答
4

我有同样的问题,我通过在我的 pom.xml 添加这个依赖来修复它:

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

祝你好运。

于 2019-07-09T18:45:41.747 回答
3

我有同样的问题。我从jbig2-imageio下载了 jar, 然后 将它包含在我项目的应用程序库中,它开箱即用。正如亚当所说,它使用 GPL3。

于 2015-02-11T16:21:58.017 回答
1

似乎不需要安装 JAI。我只需要下载 levigo-jbig2-imageio-1.6.5.jar,将其放在我的依赖项 jar 的文件夹中,然后在 eclipse 中将其添加到 java 构建路径库中。 https://github.com/levigo/jbig2-imageio/

于 2015-08-15T13:35:19.170 回答
1
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"
    }
}
于 2017-03-20T19:07:15.380 回答