user1942609
user1942609

Reputation: 33

How can extract images from pdf file using itext library in my android application

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.

Upvotes: 1

Views: 4668

Answers (1)

cjds
cjds

Reputation: 8426

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

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

Instead try saving to SD Card.

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

To customize for 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) {
   }
}

Upvotes: 3

Related Questions