gpt4 book ai didi

java - itextPdf 的问题,拒绝重新初始化先前失败的类 java.lang.Class

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:24 26 4
gpt4 key购买 nike

我是 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);

我已经在 app gradle 中编译了 itextpdf 库:

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。

关于java - itextPdf 的问题,拒绝重新初始化先前失败的类 java.lang.Class<com.itextpdf.awt.PdfGraphics2D>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39972333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com