基本方法,创建带图片的pdf:
public static void createImgPDF(String outPath) throws IOException, DocumentException {
File file = new File(outPath);
file.getParentFile().mkdirs();
Document document = new Document();
// 创建PdfWriter对象
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outPath));
// 设置图片精确放置
writer.setStrictImageSequence(true);
// 打开文档
document.open();
// 添加本地图片
Image image1 = Image.getInstance(IMG1);
// 设置位置,以左下角为原点
image1.setAbsolutePosition(50f, 700f);
// 设置居中
image1.setAlignment(Image.MIDDLE);
// 设置居左
image1.setAlignment(Image.LEFT);
// 设置居右
image1.setAlignment(Image.RIGHT);
// 设置文字绕图形显示
image1.setAlignment(Image.TEXTWRAP);
// 设置图形作为文字的背景显示
image1.setAlignment(Image.UNDERLYING);
// 也可以综合上面使用
image1.setAlignment(Image.RIGHT | Image.TEXTWRAP);
// 按百分比缩放
image1.scalePercent(50);
// 根据宽高分别缩放
image1.scalePercent(50, 70);
// 设置到绝对大小
image1.scaleAbsolute(100, 100);
document.add(image1);
// 添加网络图片,设置同上
Image image2 = Image.getInstance(new URL(IMGURL));
document.add(image2);
// low level
PdfContentByte cb = writer.getDirectContent();
cb.fill();
cb.sanityCheck();
// 关闭文档
document.close();
}
基本思路跟添加段落是一样的,上面代码有详细注释,支持本地路径和网络地址,可以设置图片大小、位置等等,添加图片的基本使用就是这样,下面列举几个itext官方比较实用的方法:
1、对存在的pdf添加图片
public static void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
Image image = Image.getInstance(IMG1);
PdfImage stream = new PdfImage(image, "", null);
stream.put(new PdfName("ITXT_SpecialId"), new PdfName("123456789"));
PdfIndirectObject ref = stamper.getWriter().addToBody(stream);
image.setDirectReference(ref.getIndirectReference());
image.setAbsolutePosition(36, 400);
image.scaleAbsolute(100, 100);
PdfContentByte over = stamper.getOverContent(1);
over.addImage(image);
stamper.close();
reader.close();
}
2、创建带水印的图片
public static void createWatermarkedPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfContentByte cb = writer.getDirectContentUnder();
document.add(getWatermarkedImage(cb, Image.getInstance(IMG1), "watermark1"));
Image img = Image.getInstance(IMG2);
img.scaleToFit(100, 100);
document.add(getWatermarkedImage(cb, img, "watermark2"));
document.close();
}
步骤跟添加图片一样,关键方法是生成带水印的图片:
public static Image getWatermarkedImage(PdfContentByte cb, Image img, String watermark) throws DocumentException {
// 获取原图片的宽
float width = img.getScaledWidth();
// 获取原图片的高
float height = img.getScaledHeight();
// 创建模板
PdfTemplate template = cb.createTemplate(width, height);
// 添加图片
template.addImage(img, width, 0, 0, height, 0, 0);
// 设置文本以及位置、偏移、旋转
ColumnText.showTextAligned(template, Element.ALIGN_CENTER, new Phrase(watermark, FONT), width / 2, height / 2,
30);
Image newImage = Image.getInstance(template);
newImage.scaleAbsolute(100, 100);
return newImage;
}
关于字体的设置在上一篇已经介绍,这里就不再重复了。效果如下:
水印一般用白色,这里为了看起来明显就用的黑色。
3、创建背景图
public static void createBackgroundPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("大家好,这里是源代码社区!", FONT));
PdfContentByte canvas = writer.getDirectContentUnder();
Image image = Image.getInstance(BACKGROUD_IMG);
image.scaleAbsolute(PageSize.A4.rotate().getWidth(), PageSize.A4.rotate().getHeight());
image.setAbsolutePosition(0, 0);
canvas.addImage(image);
document.close();
}
就是将图片大小设置为A4文档大小,从起点开始铺满整个页面,效果如下:
还可以将背景设置半透明,弱化下背景效果:
public static void createBackgroundTransparentPdf(String dest) throws IOException, DocumentException {
Document document = new Document(PageSize.A4.rotate());
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(new Paragraph("大家好,这里是源代码社区!", FONT));
PdfContentByte canvas = writer.getDirectContentUnder();
Image image = Image.getInstance(BACKGROUD_IMG);
image.scaleAbsolute(PageSize.A4.rotate().getWidth(), PageSize.A4.rotate().getHeight());
image.setAbsolutePosition(0, 0);
canvas.saveState();
PdfGState state = new PdfGState();
state.setFillOpacity(0.6f);
canvas.setGState(state);
canvas.addImage(image);
canvas.restoreState();
document.close();
}
效果如下:
效果是不是弱了许多呢,上面介绍的一些方法都是非常实用的代码,当然这个库里面还有很多操作图片的功能,详细用法可以itextpdf官网看API。