0

droidtext を使用して既存の pdf に画像を挿入しようと必死です。

このプロジェクトの元のバージョンは iText で作成されました。したがって、コードは既に存在し、Android に適合するように変更されています。

私がしていることは、既存の PDF を背景として使用することです。この PDF 内の指定された位置にテキストと十字を挿入します。フォームに記入するようなものです。これは、コードを大幅に変更することなく、これまでのところ非常にうまく機能しています。

ここで、フォームに署名するためにページの下部に画像を設定したいと考えています。元のコードを使用して、少し調整しました。これはまったく機能しません。特定の位置に画像を設定しようとしました。多分それはエラーでした。

だから私はそれを「公式」の方法でやろうとしました。Pengiuns.jpg 画像は SD カードにあります。

try {
Document document = new Document();
File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf");
PdfWriter.getInstance(document,new FileOutputStream(f));
document.open();
document.add(new Paragraph("Simple Image"));
String path = Environment.getExternalStorageDirectory()+"/Penguins.jpg";

if (new File(path).exists()) {
    System.out.println("filesize: " + path + " = " + new File(path).length());
}

Image image =Image.getInstance(path);
document.add(image);
document.close();
} catch (Exception ex) {
    System.out.println("narf");
}

しかし、まだまったくイメージがありません。私が得たのは、「Simple Image」という言葉だけが書かれ​​た PDF です。写真にアクセスできます。if() で正しいファイルサイズを取得します。例外はスローされません。

私の質問は、SD カードにある画像を PDF に取り込むにはどうすればよいですか? ここでの間違いは何ですか?しかし、最も重要なのは、pdf 内のサイズで画像を特定の場所に設定するにはどうすればよいですか? 私の元のコードでは、 setAbsolutePosition( x, y ) を使用しています。Eclipse をコードで使用しても問題はありませんが、実際に動作していますか?

4

2 に答える 2

1

「単純なイメージ」を取得している理由は、Paragraph. 画像を追加するには、次を使用します。

Image myImg1 = Image.getInstance(stream1.toByteArray());

最後のページのフッターとして使用する場合は、次のコードを使用できますが、テキストで機能します。画像の操作を試みることができます:

Phrase footerText = new Phrase("THIS REPORT HAS BEEN GENERATED USING INSPECTIONREPORT APP");
HeaderFooter pdfFooter = new HeaderFooter(footerText, false);
doc.setFooter(pdfFooter);

これがサンプルコードです。ここでは、画像を Imageview にアップロードしてから、pdf に追加しました。お役に立てれば。

private String NameOfFolder = "/InspectionReport"; 

Document doc = new Document();

try {   //Path to look for App folder 
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + NameOfFolder;
    String CurrentDateAndTime= getCurrentDateAndTime();   

    // If App folder is not there then create one
    File dir = new File(path);
    if(!dir.exists())
        dir.mkdirs();


    //Creating new file and naming it
    int i = 1;  

    File file = new File(dir, "Inspection"+Integer.toString(i)+"-" + CurrentDateAndTime+".pdf");
    while(file.exists()) {
        file = new File(dir, "Inspection"+Integer.toString(i++)+"-" + CurrentDateAndTime+".pdf");}


        String filep= file.toString();
        FileOutputStream fOut = new FileOutputStream(file);

        Log.d("PDFCreator", "PDF Path: " + path);
        PdfWriter.getInstance(doc, fOut);
        Toast.makeText(getApplicationContext(),filep , Toast.LENGTH_LONG).show();

        //open the document
        doc.open();

        ImageView img1 = (ImageView)findViewById(R.id.img1);
        ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
        Bitmap bitmap1 = ((BitmapDrawable)img1.getDrawable()).getBitmap();
        bitmap1.compress(Bitmap.CompressFormat.JPEG, 100 , stream1);
        Image myImg1 = Image.getInstance(stream1.toByteArray());
        myImg1.setAlignment(Image.MIDDLE);

        doc.add(myImg1);
于 2013-09-09T17:59:30.913 に答える
0

次のコードを試してください:

/* Inserting Image in PDF */
ByteArrayOutputStream stream = new ByteArrayOutputStream();

Bitmap bitmap = BitmapFactory.decodeResource(getBaseContext().getResources(), R.drawable.android);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100 , stream);

Image myImg = Image.getInstance(stream.toByteArray());

myImg.setAlignment(Image.MIDDLE);

//add image to document
doc.add(myImg);
于 2013-10-24T06:35:18.547 に答える