Search code examples
androideclipsecrashitextitextg

iTextG causing application crash(Android)


My objective is to superimpose contents of a pdf onto another PDF.

this is the code that I have used:

AssetManager assetManager = getAssets();
    InputStream istr = null;
    PdfReader reader = null;
    String str = null;
    int n = 0;
    try
    {                       
        istr =(InputStream) assetManager.open("Chemistry.pdf");

        reader=new PdfReader(istr);     




        Document document = new Document();
      PdfWriter writer=  PdfWriter.getInstance(document, new FileOutputStream(Environment.getExternalStorageDirectory()+ "/newhelloPrayToGod.pdf"));


        document.open();

        document.add(new Paragraph("Hello World"));
        document.newPage();
        PdfContentByte canvas = writer.getDirectContent();
        PdfImportedPage page;

        page = writer.getImportedPage(reader, 23);
        canvas.addTemplate(page, 1, 0, 0, 0, 0, 0);



        document.close();

        Log.d("OK", "done");
    }
    catch (FileNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

I have used itextG 5.4.3. I have even added a copy of the .jar in my libs folder. The code compiles, but on running, it just crashes. This is the error log:

03-28 16:27:36.693: E/AndroidRuntime(1037): FATAL EXCEPTION: main 03-28 16:27:36.693: E/AndroidRuntime(1037): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.teest/com.example.teest.MainActivity}: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to com.itextpdf.text.pdf.codec.Base64$InputStream 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.access$600(ActivityThread.java:141) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.os.Handler.dispatchMessage(Handler.java:99) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.os.Looper.loop(Looper.java:137) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.main(ActivityThread.java:5039) 03-28 16:27:36.693: E/AndroidRuntime(1037): at java.lang.reflect.Method.invokeNative(Native Method) 03-28 16:27:36.693: E/AndroidRuntime(1037): at java.lang.reflect.Method.invoke(Method.java:511) 03-28 16:27:36.693: E/AndroidRuntime(1037): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 03-28 16:27:36.693: E/AndroidRuntime(1037): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 03-28 16:27:36.693: E/AndroidRuntime(1037): at dalvik.system.NativeStart.main(Native Method) 03-28 16:27:36.693: E/AndroidRuntime(1037): Caused by: java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to com.itextpdf.text.pdf.codec.Base64$InputStream 03-28 16:27:36.693: E/AndroidRuntime(1037): at com.example.teest.MainActivity.onCreate(MainActivity.java:36) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.Activity.performCreate(Activity.java:5104) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 03-28 16:27:36.693: E/AndroidRuntime(1037): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

P.S. I have not used any license file(.xml) or anything of that sort. the above code is JUST what I have used.

I tried using the iText jar (not iTextG) too. In that, the code worked, but if I used this line in the code

canvas.addTemplate(page, 1, 0, 0, 0, 0, 0);

It would not compile with this error

The type java.awt.geom.AffineTransform cannot be resolved. It is indirectly referenced from required .class files

What do I do to get the canvas,addtemplate() running on android? I want to superimpose pdfs on another.


Solution

  • I assume the exception occurs here:

    istr =(InputStream) assetManager.open("Chemistry.pdf");
    

    The exception

    java.lang.ClassCastException: android.content.res.AssetManager$AssetInputStream cannot be cast to com.itextpdf.text.pdf.codec.Base64$InputStream
    

    indicates that the InputStream in that code line is not the expected java.io variant.

    Most likely you have an import like

    import com.itextpdf.text.pdf.codec.Base64.InputStream
    

    while you actually wanted

    import java.io.InputStream