如何使用itextpdf将图像附加到android中的现有pdf文件
我正在使用文件代码以附加模式打开pdf文件如何使用itextpdf将图像附加到android中的现有pdf文件,android,itext,Android,Itext,我正在使用文件代码以附加模式打开pdf文件 String FILE = ROOT + "/PDF/" + "DemoLogix.pdf"; PdfWriter.getInstance(document, new FileOutputStream(FILE, true)); document.open(); 但是当我使用以下代码添加图像时 PdfPTable table5 = new PdfPTable(new float[]{1}); Bitma
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();
它仍然不起作用。相反,它会覆盖上一个文本。请有人帮帮我。加载要修改的PDF
PdfReader reader = new PdfReader(srcPdf);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(destPdf));
PdfContentByte content = stamper.getOverContent(1);
然后,加载图像(imagePath是imagem文件的完整路径):
因为图像尺寸很大,所以在我们将其添加到PDF之前,它会缩放到50像素的高度
然后在需要的位置设置页面坐标。请注意,Y轴的0值是页面的底部,而不是顶部:
image.setAbsolutePosition(70, 140);
现在只需将其添加到页面引用并关闭母版:
content.addImage(image);
stamper.close();
就这样 这个答案是有效的。你能建议我在内容末尾添加图片的方法吗。
content.addImage(image);
stamper.close();