所需jar的pom坐标
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<dependency>
<groupId>com.sun.media</groupId>
<artifactId>jai_codec</artifactId>
<version>1.1-mr</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/jai_codec-1.1-mr.jar</systemPath>
</dependency>
<dependency>
<groupId>javax.media</groupId>
<artifactId>jai_core</artifactId>
<version>1.1-mr</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/jai_core-1.1-mr.jar</systemPath>
</dependency>
<dependency>
<groupId>com.sun.medialib</groupId>
<artifactId>mlibwrapper_jar</artifactId>
<version>1.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/jar/mlibwrapper_jar-1.1.jar</systemPath>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.0-RC2</version>
</dependency>
/**
* pdf填充文字信息
*
* @param targetPath 新生成的pdf路径
* @param templatePath pdf模板路径
* @param dataMap key--value
*/
public static void fillPdfWord(String targetPath, String templatePath, Map<String, String> dataMap) {
PdfReader reader = null;
ByteArrayOutputStream bos = null;
PdfStamper ps = null;
FileOutputStream fos = null;
try {
//设置字体
BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//创建一个pdf读取对象,读取pdf模板
reader = new PdfReader(templatePath);
//创建一个输出流
bos = new ByteArrayOutputStream();
//创建pdf模板,参数reader bos
ps = new PdfStamper(reader, bos);
//封装数据,取出模板中的所有字段数据,读取文本域
AcroFields form = ps.getAcroFields();
//必须要调用这个,否则文档不会生成的
ps.setFormFlattening(true);
form.addSubstitutionFont(bf);
for (String key : dataMap.keySet()) {
String value = dataMap.get(key);
//为指定域,设置字体大小
form.setFieldProperty(key, "textsize", 7f, null);
//为指定域,填充数据
form.setField(key, value);
}
ps.close();
//指定生成的新路径
File file = new File(targetPath);
//创建文件
file.createNewFile();
//使生成的文件file生效,这个必须有
//创建文件输出流
fos = new FileOutputStream(file);
fos.write(bos.toByteArray());//写入数据
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) fos.close();
if (bos != null) bos.close();
if (reader != null) reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* pdf填充图片信息
*
* @param targetPath 新生成的pdf路径
* @param templatePath pdf模板路径
* @param imgMap key--value
*/
public static void fillPdfImage(String targetPath, String templatePath, Map<String, String> imgMap) {
PdfReader reader = null;
ByteArrayOutputStream bos = null;
PdfStamper ps = null;
FileOutputStream fos = null;
try {
//设置字体
BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
//创建一个pdf读取对象,读取pdf模板
reader = new PdfReader(templatePath);
//创建一个输出流
bos = new ByteArrayOutputStream();
//创建pdf模板,参数reader bos
ps = new PdfStamper(reader, bos);
//封装数据,取出模板中的所有字段数据,读取文本域
AcroFields form = ps.getAcroFields();
//必须要调用这个,否则文档不会生成的
ps.setFormFlattening(true);
form.addSubstitutionFont(bf);
for (String key : imgMap.keySet()) {
String imgPath = imgMap.get(key);
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 根据路径读取图片
Image image = null;
image = Image.getInstance(imgPath);
// 获取图片页面
PdfContentByte under = ps.getOverContent(pageNo);
// 图片大小自适应
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
}
ps.close();
//指定生成的新路径
File file = new File(targetPath);
//创建文件
file.createNewFile();
//使生成的文件file生效,这个必须有
//创建文件输出流
fos = new FileOutputStream(file);
fos.write(bos.toByteArray());//写入数据
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) fos.close();
if (bos != null) bos.close();
if (reader != null) reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}