如何在我的Android应用程序中使用itext库从pdf文件提取图像 [英] How can extract images from pdf file using itext library in my android application

查看:125
本文介绍了如何在我的Android应用程序中使用itext库从pdf文件提取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用itext库从pdf文件中提取图像,我将pdf文件放在android应用程序的资产文件夹中,并使用itext库在android中显示这些图像.

I want to extract images from pdf file using itext library where i put my pdf files in asset folder on android application and display these images in android using itext library.

推荐答案

iText 是一个Java库,因此可以在android中使用.

iText is a java library so it can be used in android.

您可以保存到资产文件夹吗?.它是只读的

Can you save to the assets folder. NO. Its read Only

请尝试保存到SD卡.

请查看 https://github.com/itext/i7js-samples/tree/develop/publications/book/src/test/java/com/itextpdf/samples/book/part4/chapter15 尤其是文件:Listing_15_30/31 * .java他们应该教你如何在iText中提取图像

Please have a look at https://github.com/itext/i7js-samples/tree/develop/publications/book/src/test/java/com/itextpdf/samples/book/part4/chapter15 esp. files: Listing_15_30/31*.java they should teach you how to extract images in iText

为android定制

import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.PdfImageObject;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;

public class MyImageRenderListener implements RenderListener {

/** The new document to which we've added a border rectangle. */
protected String path = "";

/**
 * Creates a RenderListener that will look for images.
 */
public MyImageRenderListener(String path) {
    this.path = path;
}

/**
 * @see com.itextpdf.text.pdf.parser.RenderListener#beginTextBlock()
 */
public void beginTextBlock() {
}

/**
 * @see com.itextpdf.text.pdf.parser.RenderListener#endTextBlock()
 */
public void endTextBlock() {
}

/**
 * @see com.itextpdf.text.pdf.parser.RenderListener#renderImage(
 *     com.itextpdf.text.pdf.parser.ImageRenderInfo)
 */
public void renderImage(ImageRenderInfo renderInfo) {
    try {
        String filename;
        FileOutputStream os;
        PdfImageObject image = renderInfo.getImage();
        if (image == null) return;
        filename = String.format(path, renderInfo.getRef().getNumber(), image.getFileType());
        os = new FileOutputStream(filename);
        os.write(image.getImageAsBytes());
        os.flush();
        os.close();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}


   public void renderText(TextRenderInfo renderInfo) {
   }
}

这篇关于如何在我的Android应用程序中使用itext库从pdf文件提取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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