【问题标题】:Add a Bitmap image in a PDF document in Android在 Android 中的 PDF 文档中添加位图图像
【发布时间】:2017-04-04 18:30:40
【问题描述】:

请问,我怎样才能将位图图像直接传递给 pdf 文件。我用 GraphView 制作了一个图表,最后我将它转换为位图,在 OnClickListener 内:

write.setOnClickListener(new View.OnClickListener() 
{
        @Override
        public void onClick(View arg0) {
            Bitmap bitmap;
            graph.setDrawingCacheEnabled(true);
            bitmap = Bitmap.createBitmap(graph.getDrawingCache());
            graph.setDrawingCacheEnabled(false);
            String filename = "imagen";
            FileOperations fop = new FileOperations();
            fop.write(filename, bitmap);
            if (fop.write(filename,bitmap)) {
                Toast.makeText(getApplicationContext(),
                        filename + ".pdf created", Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(getApplicationContext(), "I/O error",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

问题是在 FileOperations 类中:

public FileOperations() {
}

public Boolean write(String fname, Bitmap bm) {
    try {
        String fpath = "/sdcard/" + fname + ".pdf";
        File file = new File(fpath);
        if (!file.exists()) {
            file.createNewFile();
        }
        Document document = new Document();
        PdfWriter.getInstance(document,
                new FileOutputStream(file.getAbsoluteFile()));
        document.open();
        String filename = bm.toString();
        com.itextpdf.text.Image image  =com.itextpdf.text.Image.getInstance(filename);
        document.add(image);
        document.add(new Paragraph("Hello World2!"));
        // step 5
        document.close();

        Log.d("Suceess", "Sucess");
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}
}

我想知道如何传递位图图像以将其添加到 pdf 文档中,我这样做了,但我认为这只有在我给它一个路径时才有效。

字符串文件名 = bm.toString(); com.itextpdf.text.Image image =com.itextpdf.text.Image.getInstance(filename);

【问题讨论】:

    标签: pdf


    【解决方案1】:

    我只是解决它:

            document.open();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bm.compress(Bitmap.CompressFormat.JPEG, 100 , stream);
            Image myImg = Image.getInstance(stream.toByteArray());
            myImg.setAlignment(Image.MIDDLE);
            document.add(myImg);
    

    在 FileOperations 类中

    【讨论】:

    • 位图 bm = BitmapFactory.decodeResource(getResources(), R.mipmap.img_logo);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-16
    • 2013-04-04
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    相关资源
    最近更新 更多