1.首先需要依赖包:itext的jar包,我是maven项目,所以附上maven依赖
[html] view plain copy
com.itextpdf
itextpdf
5.5.10
[html] view plain copy
com.itextpdf
itext-asian
5.2.0
2.下面就是生成pdf代码了
[java] view plain copy
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
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();
}
} 如果没有模板,就行自己生成pdf文件保存到磁盘:下面的方法可以实现:
[java] view plain copy
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");
}
}
但是上述方法中包含中文时就会出现问题,所以可以使用下面这行代码实现,所使用的jar包,上面的两个依赖都包含了:
[java] view plain copy
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 {
PdfWriter.getInstance(document, new FileOutputStream("E:/2.pdf"));
document.open();
document.add(new Paragraph("hello word 你好 世界",font));//引用字体
document.close();
} catch (Exception e) {
System.out.println("file create exception");
}
}
**************************************************************************************
当然,如果你想弄的炫一点,想实现其他字体,可以去网上搜字体文件然后下载下来,放到项目里,我这里是在项目里新建了一个font文件夹,将字体文件放到了里面。
1.把华康少女的字体文件拷贝到这个文件夹里面了:
运行以下代码就能得到pdf文件
[java] view plain copy
public static void test1_2(){
BaseFont bf;
Font font = null;
try {
// bf = BaseFont.createFont("font/simsun.ttc,1", //注意这里有一个,1
// BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//宋体文字
bf = BaseFont.createFont("font/华康少女文字W5(P).TTC,1", //simsun.ttc
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);//华康少女文字
font = new Font(bf,12);
} catch (Exception e) {
e.printStackTrace();
}
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("E:/3.pdf"));
document.open();
document.add(new Paragraph("上善若水",font));
document.close();
} catch (Exception e) {
System.out.println("file create exception");
}
}
当然,如果你还想换其他字体,就去下载字体文件吧,然后把相关部分替换掉就行,上面注释的是宋体的。。。