我有一个小应用程序,使用意图访问相机。该应用程序将捕获的图像返回到imageview

时间:2017-05-25 10:37:33

标签: android pdf-generation

我有一个小应用程序,使用意图访问相机。该应用程序将捕获的图像返回到imageview。我想添加一项功能,将imageview中的图像转换为pdf文件。有人如何帮助解决这个问题

1 个答案:

答案 0 :(得分:0)

建议您使用itext pdf库。这是gradle:'com.itextpdf:itextpdf:5.1.1'

Document document=new Document();
 String dirpath=android.os.Environment.getExternalStorageDirectory().toString();
 PdfWriter.getInstance(document,new FileOutputStream(dirpath+"/example.pdf")); //  Change pdf's name.
 document.open();
 Image img =Image.getInstance(dirpath+"/"+"example.jpg");  // Change image's name and extension.

 float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
               - document.rightMargin() - 0) / img.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
 img.scalePercent(scaler);
 img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_TOP); 
 //img.setAlignment(Image.LEFT| Image.TEXTWRAP); 

 /* float width = document.getPageSize().width() - document.leftMargin() - document.rightMargin();
 float height = document.getPageSize().height() - document.topMargin() - document.bottomMargin();
 img.scaleToFit(width, height)*/ Or try this.

 document.add(img);
 document.close();