我对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库以及app gradle:
compile 'com.itextpdf:itextpdf:5.5.6'
但我不知道为什么,但图像(位图:clickphoto)作为pdf没有被保存在给定的位置和在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平台上开发游戏。这意味着你需要Android版本的iText,它叫做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不再值得信赖。曾经发生过SourceForge将间谍软件注入其他软件安装程序的事件。虽然这种情况还没有发生在iText上(而且由于我们软件的性质,不太可能发生),但我们不能宽恕这些行为,我们告诉所有的用户和客户避免使用SourceForge。