我在我的安卓项目中添加了PdfBox安卓端口。
我写了以下代码
try
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
// page.set
document.addPage(page);
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myapp");
File phone = new File(mediaStorageDir.getPath() + File.separator + "image.jpg");
FileInputStream mInput = new FileInputStream(phone);
PDStream steam1 = new PDStream(document, mInput);
PDResources resource1 = new PDResources();
PDImageXObject img = new PDImageXObject(steam1, resource1);
PDPageContentStream contentStream = new PDPageContentStream(
document, page);
contentStream.drawImage(img, 100, 100);
contentStream.close();
document.save("Hello World.pdf");
document.close();
}
catch(Exception e)
{
}当我执行时,它执行得很好,直到下面一行
PDImageXObject img = new PDImageXObject(steam1, resource1);我得到跟随错误
java.io.IOException:空流未被读取
如何解决这个问题?我想我漏掉了什么。请帮帮我!
发布于 2015-05-25 10:14:09
使用JPEGFactory,而不是PDImageXObject:
PDImageXObject img = JPEGFactory.createFromStream(document, mInput);用PDResources和PDStream删除行。因此,您的代码将如下所示:
try
{
PDDocument document = new PDDocument();
PDPage page = new PDPage();
// page.set
document.addPage(page);
// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myapp");
File phone = new File(mediaStorageDir.getPath() + File.separator + "image.jpg");
FileInputStream mInput = new FileInputStream(phone);
PDImageXObject img = JPEGFactory.createFromStream(document, mInput);
PDPageContentStream contentStream = new PDPageContentStream(document, page);
contentStream.drawImage(img, 100, 100);
contentStream.close();
document.save(mediaStorageDir.getPath() + File.separator + "Hello World.pdf");
document.close();
}
catch(Exception e)
{
}(这个答案只适用于Android版本)
https://stackoverflow.com/questions/30434748
复制相似问题