PDF框由于JBIG2图像而生成空白图像

时间:2013-12-06 13:12:48

标签: pdf-generation pdfbox jbig2

让我先介绍一下我的项目。我有一个pdf,我需要使用PDFBox API将其转换为图像(一页图像),并使用PDFBox API本身将所有这些图像写入新的pdf。基本上,将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; 

我不知道如何实施。

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

5 个答案:

答案 0 :(得分:10)

在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/

答案 1 :(得分:3)

我遇到了完全相同的问题。 我从中下载了jar jbig2-imageio 我只是将它包含在我的项目的应用程序库中,它开箱即用。正如亚当所说,它使用的是GPL3。

答案 2 :(得分:1)

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

答案 3 :(得分:1)

我遇到了同样的问题,并通过在pom.xml中添加此依赖项来解决它:

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

祝你好运。

答案 4 :(得分:0)

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