如何添加图片作为Android的PDFBox的页面 [英] how to add image as page in PDFBox Android
本文介绍了如何添加图片作为Android的PDFBox的页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的Android项目中添加 PDFBOX的Android端口。
I have add PdfBox Android port in my android project.
我已经写了下面的code
I have wrote the following code
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)
{
}
当我执行一切顺利,直到下面的行
when I execute it goes well until following line
PDImageXObject img = new PDImageXObject(steam1, resource1);
我得到以下错误
java.io.IOException异常:空流未读
java.io.IOException: null stream was not read
如何解决呢?我想我失去了一些东西。请帮帮我!
how to solve it? I think I am missing something. please help me!
推荐答案
使用JPEGFactory,不PDImageXObject:
use the JPEGFactory, not PDImageXObject:
PDImageXObject img = JPEGFactory.createFromStream(document, mInput);
删除与PDResources和PDStream行。因此,你的code是这样的:
delete the lines with PDResources and with PDStream. Thus your code would look like this:
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版本)
(This answer applies only to the Android version)
这篇关于如何添加图片作为Android的PDFBox的页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文