0

以下のコードと iText ライブラリを使用して、pdf を動的に作成しました。

try {

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();
                PdfWriter.getInstance(document, fos);

                document.open();
                document.add(new Paragraph("Hello World"));
                document.add(new Paragraph(new Date().toString()));

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

976 バイトの pdf ファイルを取得します。以下のコードを使用して、このpdfファイルに画像を追加するためのコードを追加したとき:

    try {

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();
                PdfWriter.getInstance(document, fos);

                document.open();
                document.add(new Paragraph("Hello World, iText"));
                document.add(new Paragraph(new Date().toString()));


                Image image = Image.getInstance("ic_launcher.png");
                document.add(image);

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

私は0バイトのpdfファイルを取得します。

何が問題なのかわかりません。それに関連するアイデアがあれば、私と共有してください。ありがとう。

4

3 に答える 3

2

それはあなたの問題を解決します

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] bitMapData = stream.toByteArray();
        Image image = Image.getInstance(bitMapData);
于 2013-06-20T12:13:19.297 に答える
2

最後に、以下のコードを使用してpdfに画像を追加しました:

試す {

                String str_path = Environment.getExternalStorageDirectory()
                        .toString();

                File file;
                file = new File(str_path, getString(R.string.app_name)
                        + ".pdf");
                FileOutputStream fos = new FileOutputStream(file);

                Document document = new Document();

                PdfWriter.getInstance(document, fos);

                InputStream ims = getAssets().open("ic_launcher.png");
                Bitmap bmp = BitmapFactory.decodeStream(ims);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                Image image = Image.getInstance(stream.toByteArray());

                document.open();
                document.add(new Paragraph("Hello World"));
                document.add(new Paragraph(new Date().toString()));
                document.add(image);

                document.close();
                fos.close();

            } catch (Exception e) {

                e.printStackTrace();
            }

このコードを使用してください。これがお役に立てば幸いです。

于 2013-06-20T12:13:44.977 に答える