java根据模板生成pdf文件并导出
首先你的制作一个pdf模板:
1.先用word做出模板界面
2.文件另存为pdf格式文件
3.通过Adobe Acrobat pro软件打开刚刚用word转换成的pdf文件(注:如果没有这个软件可以通过我的百度云下载,链接:http://pan.baidu.com/s/1pL2klzt)如果无法下载可以联系博主。
4.点击右边的"准备表单"按钮,选择"测试.pdf"选择开始
进去到编辑页面,打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框(其实无需任何操作,一般情况下不需要修改什么东西,至少我没有修改哦。如果你想修改fill1等信息,可以进行修改)
5.做完上面的工作后,直接"另存为"将pdf存储就可以
*****************************************************************************
以上部分是制作pdf模板操作,上述完成后,就开始通过程序来根据pdf模板生成pdf文件了,上java程序:
1.首先需要依赖包:itext的jar包,我是maven项目,所以附上maven依赖
-
<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
-
<dependency>
-
<groupId>com.itextpdf </groupId>
-
<artifactId>itextpdf </artifactId>
-
<version>5.5.10 </version>
-
</dependency>
-
<!-- https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
-
<dependency>
-
<groupId>com.itextpdf </groupId>
-
<artifactId>itext-asian </artifactId>
-
<version>5.2.0 </version>
-
</dependency>
-
import java.io.ByteArrayOutputStream;
-
import java.io.FileOutputStream;
-
import java.io.IOException;
-
-
import com.itextpdf.text.Document;
-
import com.itextpdf.text.DocumentException;
-
import com.itextpdf.text.pdf.AcroFields;
-
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 Snippet {
-
// 利用模板生成pdf
-
public static void fillTemplate() {
-
// 模板路径
-
String templatePath = "E:/测试3.pdf";
-
// 生成的新文件路径
-
String newPDFPath = "E:/ceshi.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();
-
-
String[] str = { "123456789", "TOP__ONE", "男", "1991-01-01", "130222111133338888", "河北省保定市" };
-
int i = 0;
-
java.util.Iterator<String> it = form.getFields().keySet().iterator();
-
while (it.hasNext()) {
-
String name = it.next().toString();
-
System.out.println(name);
-
form.setField(name, str[i++]);
-
}
-
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) {
-
System.out.println( 1);
-
} catch (DocumentException e) {
-
System.out.println( 2);
-
}
-
-
}
-
-
public static void main(String[] args) {
-
fillTemplate();
-
}
-
}
![](https://i-blog.csdnimg.cn/blog_migrate/b73e12a352595e79a647b78669282cbb.png)
*********************************************************************
如果没有模板,就行自己生成pdf文件保存到磁盘:下面的方法可以实现:
-
public static void test1(){ //生成pdf
-
Document document = new Document();
-
try {
-
PdfWriter.getInstance(document, new FileOutputStream( "E:/1.pdf"));
-
document.open();
-
document.add( new Paragraph( "hello word"));
-
document.close();
-
} catch (Exception e) {
-
System.out.println( "file create exception");
-
}
-
}
-
public static void test1_1(){
-
BaseFont bf;
-
Font font = null;
-
try {
-
bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
-
BaseFont.NOT_EMBEDDED); //创建字体
-
font = new Font(bf, 12); //使用字体
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
Document document = new Document();
-
try {