如做pdf建议http://www.xdocin.com/index.html
以下方法仅供参考
首先制作一个pdf模板:
1.先用word做出模板界面
2.文件另存为pdf格式文件
3.通过Adobe Acrobat pro软件打开刚刚用word转换成的pdf文件(注:如果没有这个软件可以通过我的百度云下载,链接:如果无法下载可以联系博主。
链接:https://pan.baidu.com/s/1d06bQA 密码:gxma
4.点击右边的"准备表单"按钮,选择"测试.pdf"选择开始
进去到编辑页面,打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框(其实无需任何操作,一般情况下不需要修改什么东西。如果你想修改fill1等信息,可以进行修改,字段与你查出的数据字段保持一致)
5.做完上面的工作后,直接"另存为"将pdf存储就可以
*****************************************************************************
以上部分是制作pdf模板操作,上述完成后,就开始通过程序来根据pdf模板生成pdf文件了,上java程序:
1.首先需要依赖包:itext的jar包,我是maven项目,所以附上maven依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
2.下面就是生成pdf代码了
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
public class GeneratePDFUtil {
// 利用模板生成pdf
public static void interviewReportPDF(Map<String, String> map) {
// 模板路径
String templatePath = UtilPath.getWEB_INF() + "pdf/面试报告模板.pdf";
// 生成的新文件路径
String newPDFPath ="F:/pdf/interviewReport/"+map.get("phone")+"-"+FileNameUtil.newFileName()+".pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
out = new FileOutputStream(newPDFPath);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
// 给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
BaseFont bf = BaseFont.createFont(UtilPath.getRootPath() + "fonts/simsun.ttc,0", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
form.addSubstitutionFont(bf);
//遍历map装入数据
for (Entry<String, String> entry : map.entrySet()) {
form.setField(entry.getKey(), entry.getValue());
//System.out.println("插入PDF数据----> key= " + entry.getKey() + " and value= " + entry.getValue());
}
stamper.setFormFlattening(true);// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (IOException e) {
} catch (DocumentException e) {
}
}
// //测试
// public static void main(String[] args) {
// Map<String,Object> map=new HashMap<>();
// map.put("jobtitle", "Java工程师面试报告");
// map.put("name", "李四");
// map.put("gender", "男");
// map.put("age", "22");
// /*map.put("phone", "151515151");
// map.put("marriage", "未婚");
// map.put("email", "1294608448@qq.com");*/
// map.put("jobyear", "10");
// map.put("education", "院士");
// map.put("location", "天堂");
//
// System.out.println(map);
// interviewReportPDF(map);
//
// }
}
3.导入字体包,防止中文不显示。
链接:https://pan.baidu.com/s/1bqWseQV 密码:1luc (无法访问联系我)
simsun.ttc