最近做一个项目需求,客户说要求将数据导出数据做纸质存档,我一听一个头两个大,不知道该怎么做。经过一番交流要求将用户信息,填入指定表格,然后转pdf文件打印出纸质材料。我简单应付了下,然后就开始了百度了。看了很多很多,都只是说了下怎么用java答应pdf文件,但是他说的模板表格,都是啥玩意!为此记录下我完成这个需求的经历。
首先使用adobe acrobat来制作表单,这是工具教程连接,创建一个类似于英语完考试形填空的文章;然后通过pdf的工具itextpdf(这里使用的语言是java);最后我就直接上代码了!
maven依赖如下:
<!-- pdf文件依赖包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.2.0</version>
</dependency>
<!-- pdf字体依赖包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
java代码如下:
public static void main(String[] args) throws IOException, DocumentException {
mergePdf();
}
/**
* 向pdf中写入一个简单的段落
* @throws FileNotFoundException
* @throws DocumentException
*/
public static void createPdf() throws FileNotFoundException, DocumentException {
/**
* 创建文档
*/
Document doc = new Document();
/**
* 将文档与输出流关联起来
*/
PdfWriter.getInstance(doc, new FileOutputStream("demo.pdf"));
/**
* 打开输入流
*/
doc.open();
/**
* 设置字体,这里字体是获取itext-asian 包中的
* 也有其他的方法,例如BaseFont.createFont()来获取
*/
com.itextpdf.text.Font kai = FontFactory.getFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED ,7.5f);
/**
* 向文档中输入一个段落
*/
doc.add(new Paragraph(new Chunk("demoshibushiniaaaa",kai)));
/**
* 关闭输入流
*/
doc.close();
}
/**
* 合并多个pdf文件,这里是将上个例子中的pdf文件进行复制合并
* @throws DocumentException
* @throws IOException
*/
public static void mergePdf() throws DocumentException, IOException {
Document doc = new Document();
PdfCopy pdfCopy = new PdfCopy(doc,new FileOutputStream("demo2.pdf"));
doc.open();
for (int i = 0; i < 3; i++) {
doc.newPage();
PdfReader pdfReader = new PdfReader("demo.pdf");
PdfImportedPage importedPage = pdfCopy.getImportedPage(pdfReader, 1);
pdfCopy.addPage(importedPage);
}
doc.close();
}
/**
* 表单编辑
*/
public static void editPdf() throws IOException, DocumentException {
Document doc = new Document();
PdfCopy pdfCopy = new PdfCopy(doc,new FileOutputStream("demo2.pdf"));
doc.open();
for (int i = 0; i < 3; i++) {
PdfReader pdfReader = new PdfReader("temp.pdf");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfStamper pdfStamper = new PdfStamper(pdfReader, bos);
BaseFont kai = BaseFont.createFont("simsun.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
/**
* 获取表单对象
*/
AcroFields form = pdfStamper.getAcroFields();
form.addSubstitutionFont(kai);
Map<String, AcroFields.Item> fields = form.getFields();
for (Map.Entry<String, AcroFields.Item> entry : fields.entrySet()) {
String key = entry.getKey();
// AcroFields.Item value = entry.getValue();
String fieldValue = "demo text";
List<AcroFields.FieldPosition> fieldPositions = form.getFieldPositions(key);
if (fieldPositions != null && fieldPositions.size() != 0){
/**
* 默认文字大小
*/
float fontSize = 14;
/**
* 获取表单框
*/
Rectangle position = fieldPositions.get(0).position;
/**
* 获取表单宽度
*/
float borderBoxWidth = position.getWidth();
/**
* 获取内容文本宽度
*/
float textWidth = kai.getWidthPoint(fieldValue, fontSize);
while (textWidth > borderBoxWidth){
fontSize --;
textWidth = kai.getWidthPoint(fieldValue,fontSize);
}
/**
* 设置字段大小
*/
form.setFieldProperty(key,"textsize",fontSize,null);
/**
* 设置文字
*/
form.setField(key,fieldValue);
}
}
}
doc.close();
}
谢谢观看,如有不足请指正!