使用iText和Java保存tiff CCITTFaxDecode(来自PDF页面) [英] Save tiff CCITTFaxDecode (from PDF page) using iText and Java

查看:210
本文介绍了使用iText和Java保存tiff CCITTFaxDecode(来自PDF页面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iText提取嵌入的图像并将它们保存为单独的文件。 .jpg和.png文件出来了,但是我无法提取具有CCITTFaxDecode编码的tiff图像。

I'm using iText to extract embedded images and save them as separate files. The .jpg and .png files come out ok, but I cannot extract tiff images that have the CCITTFaxDecode encoding.

有没有人有办法保存tiff文件?

Does anyone have a way of saving the tiff files?

我发现了一些使用iTextSharp的样本C#代码,价格为
使用/ CCITTFaxDecode过滤器从PDF中提取图像
它表示需要一个单独的tiff库来写出结果。根据那篇文章,CCITTFaxDecode压缩是针对tiff库的Compression.CCITTFAX4。

I found some sample C# code that uses iTextSharp at Extracting image from PDF with /CCITTFaxDecode filter It indicates a separate tiff library is needed to write out the results. According to that article, the "CCITTFaxDecode" compression is Compression.CCITTFAX4 for the tiff library.

要使用该文章的方法,我需要:
1. get一个tiff图书馆。
Java Image I / O API允许您以其他格式读写TIFF文件。
BufferedImage image = ImageIO.read(new File(image.tif));

To use that article's method, I need: 1. get a tiff library. The Java Image I/O API will allow you to read and write TIFF files among other formats. BufferedImage image = ImageIO.read( new File( "image.tif" ) );


  1. 查找用于从PDF获取位图属性的相应代码,例如:
    pd.Get(PdfName.WIDTH).ToString()(在C#中)


推荐答案

我通过以下方式从扫描的pdf(即每页作为图像)中提取了一个tiff图像:

I extracted a tiff image from scanned pdf (that is the every page as image) in the following way:

...
PdfReader reader = new PdfReader("source.pdf");
PdfReaderContentParser parser = new PdfReaderContentParser(reader);
MyImageRenderListener listener = new MyImageRenderListener("destination.jpg");
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
parser.processContent(i, listener);
 }
...

MyImageRenderListener.class的代码:

code of MyImageRenderListener.class:

class MyImageRenderListener implements RenderListener {
    protected String path = "";

    public MyImageRenderListener(String path) {
        this.path = path;
    }

    public void beginTextBlock() {
    }

    public void endTextBlock() {
    }

    public void renderImage(ImageRenderInfo renderInfo) {
        try {
            String filename;
            FileOutputStream os;
            PdfImageObject image = renderInfo.getImage();
            PdfName filter = (PdfName) image.get(PdfName.FILTER);

                   if (PdfName.CCITTFAXDECODE.equals(filter)) {
                      BufferedImage bufferedImage = image.getBufferedImage();
                  ImageIO.write(bufferedImage, "jpg", new FileOutputStream(filename));// save tif image as jpg


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void renderText(TextRenderInfo renderInfo) {
    }
}

这篇关于使用iText和Java保存tiff CCITTFaxDecode(来自PDF页面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆