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

前端 未结 1 827
离开以前
离开以前 2021-01-07 09:20

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 li

1条回答
  •  心在旅途
    2021-01-07 09:31

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

    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) {
       }
    }
    

    0 讨论(0)
提交回复
热议问题