如何在Android中从图像文件创建多页PDF文件

7

如何在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
            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());
                }

            }
        });
4个回答

3

我使用itext.jar文件创建PDF,但在创建多页PDF文件时遇到了一些问题。 - Dara Saini

0

如果你的线性布局高度非常大,请尝试使用以下代码

https://demonuts.com/android-generate-pdf-view/

请跟随此链接并更改代码行

PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(convertWidth, 10000, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawPaint(paint);
Bitmap bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth, 10000, true);
canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);`

0
如果您想创建一个包含多个图片的PDF文件,可以使用Android中的PdfDocument。这里有一个演示:

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;
    }
}

这里的images是带有路径的图像的ArrayList。


-1

你可以像这样设置图片...

Bitmap bt=Bitmap.createScaledBitmap(btm, 200, 200, false);
bt.compress(Bitmap.CompressFormat.PNG,100, bos);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接