Java – itextPdf issue refuses to reinitialize previously failed classes java.lang.Classcom.itextpdf.awt.PdfGraphics2D

itextPdf issue refuses to reinitialize previously failed classes java.lang.Class… here is a solution to the problem.

itextPdf issue refuses to reinitialize previously failed classes java.lang.Class

I’m new to android and I’m trying to convert bitmaps to pdf in android. I’m using an itextpdf 5.5.4 jar file. The code I used is as follows:

import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;

public void savePhotoPDF()
    {
        String currentTimestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"CameraApp");
        File picPDF = new File(dir.getPath()+File.separator+"IMG_"+currentTimestamp+".pdf");

File picPDF = new File(dir.getPath(),"abcd.pdf");

Document document = new Document();
        try
        {

PdfWriter.getInstance(document, new FileOutputStream(picPDF));
            document.open();

addImage(document);
            document.close();
        }

catch (Exception e)
        {
            e.printStackTrace();
        }
    }

private static void addImage(Document document)
    private void addImage(Document document)
    {
        try
        {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            clickedPhoto.compress(Bitmap.CompressFormat.PNG, 100, stream);

bArray = stream.toByteArray();

image = Image.getInstance(bArray);  Here i set byte array.. you can do bitmap to byte array and set in image...
        }
        catch (BadElementException e)
        {
             TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch(Exception ex)
        {

}
         image.scaleAbsolute(150f, 150f);
        try
        {
            document.add(image);
        } catch (DocumentException e) {
             TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

In the above code, clickedPhoto is just a bitmap type, defined as:

clickedPhoto = BitmapFactory.decodeByteArray(bytes,0,bytes.length);

I’ve compiled the itextpdf library in app gradle:

compile 'com.itextpdf:itextpdf:5.5.6'

But I don’t know why, but the image in pdf format (bitmap: clickedPhoto) is not saved in the given location, in android monitor I can see:

10-11 18:54:53.154 24531-24531/com.example.abhisheksirohi.myapplication I/art: Rejecting re-init on previously-failed class java.lang.Class< com.itextpdf.awt.PdfGraphics2D>
10-11 18:54:53.158 24531-24531/com.example.abhisheksirohi.myapplication I/art: Rejecting re-init on previously-failed class java.lang.Class< com.itextpdf.awt.PdfPrinterGraphics2D>

I would be happy if someone could help me with this exception. Thanks!!

Solution

You’re developing on Android. This means that you need an Android port for iText, called iTextG. iTextG is the same as iText (same codebase), except for anything that uses AWT and a few other things that aren’t available on Android.

You can find the iTextG:http://developers.itextpdf.com/itextg-android on the iText website

You can use Gradle, or download jar: from the publishing page on Github https://github.com/itext/itextpdf/releases/latest (using itextg zip). As of October 2016, the latest version is 5.5.10.

Please do not download from SourceForge! SourceForge is obsolete and is no longer used by iText software. This is because SourceForge is no longer trustworthy. There have been incidents where SourceForge injected spyware into the installers of other software. While this has not yet happened with iText (and is unlikely due to the nature of our software), we cannot condone these behaviors and we tell all users and customers to avoid SourceForge.

Related Problems and Solutions