您的位置:首页 > 其它

itext生成pdf文件---图片插入

2018-01-31 14:54 746 查看
1,导入jar包

2,参考代码

protected static void creatPDF() throws DocumentException, FileNotFoundException, Exception, IOException, BadElementException, MalformedURLException {
// Document document = new Document(PageSize.A4);
Rectangle r = new Rectangle(0, 0, 600, 600);
Document document = new Document(PageSize.A4, 15, 15, 15, 15);

PdfWriter.getInstance(document, new FileOutputStream(new File("D:\\test.pdf")));

document.open();

document.newPage();

PdfPTable ptable = new PdfPTable(6);
System.out.println(document.getPageSize().getWidth() + "," + document.getPageSize().getWidth() / 8);
String imagetemp = "D:\\temp.jpg";

for (int j = 0; j < 100; j++) {
OutputStream stream = new FileOutputStream(imagetemp);

BufferedImage bufimage = QRCodeUtil.createImage("1018043486", "http://localhost/images/tb_top_logo.png", 270, 300, 4, true, "1018043486");
ImageIO.write(bufimage, QRCodeUtil.FORMAT_NAME, stream);
Image jpeg = Image.getInstance(imagetemp);
jpeg.scaleAbsolute(100, 100);
jpeg.setAlignment(Image.ALIGN_CENTER);
PdfPCell cell = new PdfPCell(jpeg);
ptable.addCell(cell);
stream.flush();
stream.close();
}
ptable.setWidthPercentage(100);//去掉table边距全屏操作
document.add(ptable);
document.close();
}


3,参照:https://www.cnblogs.com/linjiqin/p/3539283.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: