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

4 回答 4

3

您通过 StackOverflow 进行的搜索较少我猜是因为我发现这些答案已经有了解决方案,是的,它包含在不同的答案中,并且查看 Q/AI 猜测他们可以解决您的问题,如果没有,请继续尝试:)

如何在android中生成带有图像的Pdf文件?

如何使用 Java 和 itext 从 Graphics 对象创建具有多个页面的 PDF

iText 示例

于 2012-11-29T06:22:11.087 回答
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。

于 2020-06-07T08:26:47.430 回答
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);`
于 2020-01-29T12:54:07.687 回答
-1

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

Bitmap bt=Bitmap.createScaledBitmap(btm, 200, 200, false);
bt.compress(Bitmap.CompressFormat.PNG,100, bos);
于 2012-11-29T06:29:01.330 回答