如何在android中从图像文件创建包含多个页面的pdf文件
如何在Android中从图像文件创建包含多个页面的PDF文件?我从图片中创建了一个PDF文件。那个PDF文件只有一页。这是图像的一半。在右侧,搜索部分剪切为PDF文件。 我正在使用itext-5.3.4.jar创建PDF如何在android中从图像文件创建包含多个页面的pdf文件,android,pdf-generation,Android,Pdf Generation,如何在Android中从图像文件创建包含多个页面的PDF文件?我从图片中创建了一个PDF文件。那个PDF文件只有一页。这是图像的一半。在右侧,搜索部分剪切为PDF文件。 我正在使用itext-5.3.4.jar创建PDF wbviewnews.loadUrl("http://developer.android.com/about/index.html"); // button for create wbpage to image than image to PDF file
wbviewnews.loadUrl("http://developer.android.com/about/index.html");
// button for create wbpage to image than image to PDF file
Button btnclick =(Button)findViewById(R.id.btnclick);
btnclick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Picture p = wbviewnews.capturePicture();
bitmap=null;
PictureDrawable pictureDrawable = new PictureDrawable(p);
bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
//Bitmap bitmap = Bitmap.createBitmap(200,200, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
ImageView imgdata=(ImageView)findViewById(R.id.imgdata);
imgdata.setImageBitmap(bitmap);
String filename = "pippo.png";
File sd = Environment.getExternalStorageDirectory();
File dest = new File(sd, filename);
String pdffilename = "pippo.pdf";
File pdffilepath = new File(sd, pdffilename);
try {
FileOutputStream out = new FileOutputStream(dest);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
Log.e("Exception", e.toString());
}
Document document=new Document();
try {
Log.e("pdffilepath", pdffilepath.toString());
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream(pdffilepath));
document.open();
// URL url = new URL (Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename);
// Log.e("url", url.toString());
Image image = Image.getInstance(Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+filename) ;
document.add(image);
document.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("FileNotFoundException", e.toString());
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("DocumentException", e.toString());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("MalformedURLException", e.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("IOException", e.toString());
}
}
});
我想你通过StackOverflow的搜索较少,因为我发现这些答案已经有了解决方案,是的,它包含在不同的答案中,看看Q/A,我想他们可以解决你的问题,如果没有,那么继续尝试:)
您可以这样设置图像
Bitmap bt=Bitmap.createScaledBitmap(btm, 200, 200, false);
bt.compress(Bitmap.CompressFormat.PNG,100, bos);
如果线性布局高度非常大,请尝试此代码 按照此链接,只需更改行即可
PdfDocument document=新的PdfDocument();
PdfDocument.PageInfo PageInfo=新建PdfDocument.PageInfo.Builder(convertWidth,10000,1).create();
PdfDocument.Page=document.startPage(pageInfo);
Canvas Canvas=page.getCanvas();
油漆=新油漆();
油漆。设置颜色(颜色。蓝色);
帆布.拉丝漆(油漆);
位图位图=位图.createScaledBitmap(位图,convertWidth,10000,true);
drawBitmap(位图,0,0,null);
文件完成页(第页)`
如果您想创建一个包含多个图像的pdf文件,可以从Android上使用。下面是一个演示:
private void createPDFWithMultipleImage(){
File file = getOutputFile();
if (file != null){
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
PdfDocument pdfDocument = new PdfDocument();
for (int i = 0; i < images.size(); i++){
Bitmap bitmap = BitmapFactory.decodeFile(images.get(i).getPath());
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), (i + 1)).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawPaint(paint);
canvas.drawBitmap(bitmap, 0f, 0f, null);
pdfDocument.finishPage(page);
bitmap.recycle();
}
pdfDocument.writeTo(fileOutputStream);
pdfDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private File getOutputFile(){
File root = new File(this.getExternalFilesDir(null),"My PDF Folder");
boolean isFolderCreated = true;
if (!root.exists()){
isFolderCreated = root.mkdir();
}
if (isFolderCreated) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
String imageFileName = "PDF_" + timeStamp;
return new File(root, imageFileName + ".pdf");
}
else {
Toast.makeText(this, "Folder is not created", Toast.LENGTH_SHORT).show();
return null;
}
}
private void createPDFWithMultipleImage(){
File File=getOutputFile();
如果(文件!=null){
试一试{
FileOutputStream FileOutputStream=新的FileOutputStream(文件);
PdfDocument PdfDocument=新PdfDocument();
对于(int i=0;i
这里images是带有路径的图像的数组列表。我使用itext.jar文件创建pdf。但在创建多页PDF文件时存在一些问题。