itextPdf 的问题,拒绝重新初始化先前失败的 class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>

problems with itextPdf, Rejecting re-init on previously-failed class java.lang.Class<com.itextpdf.awt.PdfGraphics2D>

我是 android 的新手,我正在尝试将位图转换为 android 中的 Pdf。我正在使用 itextpdf 5.5.4 jar 文件。我使用的代码如下:

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();
        }
    }

在上面的代码中,clickedPhoto 只是一个位图类型,定义为:

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

我在应用程序中也编译了 itextpdf 库 gradle:

compile 'com.itextpdf:itextpdf:5.5.6'

但我不知道为什么,但 pdf 格式的图像(位图:clickedPhoto)没有保存在给定位置,在 android 监视器中我可以看到:

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>

如果有人能帮我解决这个异常,我会很高兴。谢谢!!

您正在开发 Android。这意味着您需要 iText 的 Android 端口,称为 iTextG。 iTextG 与 iText 相同(相同的代码库),除了使用 AWT 的任何内容和 Android.

上不可用的其他内容。

您可以在 iText 网站上找到 iTextG:http://developers.itextpdf.com/itextg-android

您可以使用 Gradle,或从 Github 的发布页面下载 jar:https://github.com/itext/itextpdf/releases/latest(使用 itextg zip)。截至 2016 年 10 月,最新版本为 5.5.10。

请不要从 SourceForge 下载! SourceForge 已过时,iText 软件不再使用它。这是因为 SourceForge 不再值得信赖。 There have been incidents where SourceForge injected spyware into the installers of other software. 虽然 iText 尚未发生这种情况(并且由于我们软件的性质,不太可能发生),但我们不能宽恕这些行为,我们告诉所有用户和客户避免使用 SourceForge。