2017-10-06 6 views
0

ファイルコードを使用して、追加モードでpdfファイルを開きます。私はどのようにしてイメージを既存のpdfファイルにアンドロイドで追加することができますか?

String FILE = ROOT + "/PDF/" + "DemoLogix.pdf"; 
PdfWriter.getInstance(document, new FileOutputStream(FILE, true)); 
document.open(); 

しかし、私はそれはまだない作品を行い、次のコード

  PdfPTable table5 = new PdfPTable(new float[]{1}); 
      Bitmap bitmap = BitmapFactory.decodeFile(path); //path is where image is stored. 
      ByteArrayOutputStream stream1 = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream1); 
      Image image1 = Image.getInstance(stream1.toByteArray()); 
      PdfPCell cell3 = new PdfPCell(image1, false); 
      cell3.setPadding(30); 
      cell3.setBorderWidth(0.0f); 
      image1.scaleToFit(100, 500); 
      image1.setAlignment(image1.ALIGN_RIGHT); 
      table5.addCell(cell3); 
      document.add(table5); 
      document.close(); 

を使用して、私の画像を追加しています。代わりに、前のテキストを上書きします。誰か助けてください。

答えて

1

ロードあなたは(IMAGEPATHがIMAGEMファイルのフルパスです)画像をロードし、その後

PdfReader reader = new PdfReader(srcPdf); 
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPdf)); 
PdfContentByte content = stamper.getOverContent(1); 

を変更したいPDF:

Image image = Image.getInstance(imagePath); 

// scale the image to 50px height 
image.scaleAbsoluteHeight(50); 
image.scaleAbsoluteWidth((image.getWidth() * 50)/image.getHeight()); 

画像のサイズが大きいので、それはですPDFに追加する前に50ピクセルの高さにスケーリングされています。

次に、ページ座標を設定します。 Y軸の0値は、ページの最下部ではなく、トップであることに注意してください:

image.setAbsolutePosition(70, 140); 

あなたが今やらなければならないことは、ページの参照に追加し、スタンパを閉じている:

content.addImage(image); 
stamper.close(); 

これだけです!

+0

この回答は機能しています。コンテンツの最後に画像を追加する方法を提案できますか? –

関連する問題