我正在尝试从现有的JPEG图像生成PDF但是无法成功,pdf正在生成破损的JPEG图像,只有JPEG的前1/4来到PDF
以下是我的代码
public void createPDFFromBitmap(Bitmap bitmap){
File appDir = new File(Environment.getExternalStorageDirectory()+File.separator+getResources().getString(R.string.app_name));
appDir.mkdir();
String imageFile = appDir.getAbsolutePath() + File.separator+ "history.jpeg"; // add permission in your manifest...
String pdfFile = appDir.getAbsolutePath() + File.separator+ "history.pdf"; // add permission in your manifest...
File mPdfFile = new File(pdfFile);
if(mPdfFile.exists()){
mPdfFile.delete();
}
try
{
OutputStream os = new BufferedOutputStream(new FileOutputStream(imageFile));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.close();
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
document.open();
Image image = Image.getInstance(imageFile);
document.add(image);
document.close();
writer.close();
shareViaEmail(mPdfFile);
}
catch (Exception e)
{
e.printStackTrace();
}
}
我正在创建第一个JPEG,然后我使用该JPEG文件路径并希望将其转换为PDF,但遗憾的是无法正确生成PDF
答案 0 :(得分:0)
我通过以下代码
成功完成了Image image = Image.getInstance(imageFile);
document.setPageSize(image);
document.newPage();
image.setAbsolutePosition(0, 0);
document.add(image);