Search code examples
androiditext

convert pdf to image with itext in android


i search to convert pdf file to image in android , itry with itextpdf-5.3.1.jar

but i have exception in my appliction. i aded the library in libs folder .

ther is my code

    package com.example.zakiexemple;


import java.io.File;

import java.io.FileOutputStream;


import android.app.Activity;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;


import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfName;

import com.itextpdf.text.pdf.PdfObject;

import com.itextpdf.text.pdf.PdfReader;

import com.itextpdf.text.pdf.PdfStream;

public class MainActivity extends Activity {

    PdfReader reader;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button bt = (Button) findViewById(R.id.Button);

        //final WebView wb = (WebView) findViewById(R.id.webView1);



       bt.setOnClickListener(new  OnClickListener() {



@Override

        public void onClick(View arg0) {


            File file = new File("/sdcard/003_overview.pdf");
            try{
                reader = new PdfReader(file.getAbsolutePath());

                for (int i = 0; i < reader.getXrefSize(); i++) {
                    PdfObject pdfobj= reader.getPdfObject(i);
                    if (pdfobj == null || !pdfobj.isStream()) {
                        continue;
                    }

                    PdfStream stream = (PdfStream) pdfobj;
                    PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);

                    if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
                        byte[] img = PdfReader.getStreamBytesRaw((PRStream) stream);
                        FileOutputStream out = new FileOutputStream(new 
                        File(file.getParentFile(),String.format("%1$05d", i) + ".jpg"));
                        out.write(img); out.flush(); out.close();
                        Toast.makeText(getBaseContext(), "howa hadak", Toast.LENGTH_SHORT).show();
                    }
                }
            }
            catch (Exception e) { }


        }
    })   ; 
    } 

    }

and for log error

03-15 08:12:35.835: E/dalvikvm(937): Could not find class 'org.bouncycastle.cert.X509CertificateHolder', referenced from method com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj 03-15 08:12:35.835: W/dalvikvm(937): VFY: unable to resolve new-instance 1533 (Lorg/bouncycastle/cert/X509CertificateHolder;) in Lcom/itextpdf/text/pdf/PdfReader; 03-15 08:12:35.835: D/dalvikvm(937): VFY: replacing opcode 0x22 at 0x030e 03-15 08:12:35.946: D/dalvikvm(937): DexOpt: unable to opt direct call 0x318a at 0x318 in Lcom/itextpdf/text/pdf/PdfReader;.readDecryptedDocObj 03-15 08:12:35.946: D/dalvikvm(937): DexOpt: unable to opt direct call 0x31a5 at 0x33d in Lcom/itextpdf/text/pdf/PdfReader;.readDecryptedDocObj 03-15 08:12:36.505: D/dalvikvm(937): GC_CONCURRENT freed 743K, 14% free 5345K/6212K, paused 71ms+85ms, total 216ms

i think i havent the good itext library for android.

can you give the good link .


Solution

  • iText 5 cannot be directly used on Android in most cases. There are several technical incompatibilities which forced us to make a port with changes specifically made to support Android. One of these changes is the use of SpongyCastle instead of BouncyCastle.

    This version can be found on Maven central or the iText Repository. To make use of this, you will need a license. Or you can try out the software with a trial license which can be requested on the iText website.