2019-09-24更新https://www.cnblogs.com/LUA123/p/11580525.html

废话少说,不懂itext干啥用的直接去百度吧。

***************制作模板*******************

1.先用word做出界面


2.再转换成pdf格式

3.用Adobe Acrobat 打开你刚刚用word转换成的pdf

会出现如下界面

下一步

点击浏览,选择刚才你转换好的pdf

下一步

4.打开后它会自动侦测并命名表单域,右键表单域,点击属性,出现文本域属性对话框,有的人说要改成中文字体,可是我没有改一样成功啦

5.一般情况下不需要修改什么东西,至少我没有修改哦


6.直接另存为就行了

************************上程序********************************

准备:itext的jar包包:官网:http://sourceforge.net/projects/itext/files/

因为是利用模板,所以不需要建立字体来解决中文不显示的问题

public void fillTemplate(){//利用模板生成pdf
//模板路径
String templatePath = "pdfFolder/template_demo.pdf";
//生成的新文件路径
String newPDFPath = "pdfFolder/newPdf.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","小鲁","男","1994-00-00",
"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);
} }

模板如图:

程序结果如图:

可以看到,少了一个鲁......于是我把模板的表单域的字体改成了宋体,结果中文一个也不显示了,所以我判断是他那个字体支持的中文比较少吧,先不管这个了

现在都两点多了(不是下午两点多啊。。。)

到此时,利用模板生成pdf已经over了,我再说说入门的hello word 级别的程序吧,反正闲着也是闲着

程序一:

 public void test1(){//生成pdf
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/1.pdf"));
document.open();
document.add(new Paragraph("hello word"));
document.close();
} catch (FileNotFoundException | DocumentException e) {
System.out.println("file create exception");
}
}

结果:

可是如果要输出中文呢,上面这个就不行了,就要用到语言包了

最新亚洲语言包:http://sourceforge.net/projects/itext/files/extrajars/

 public 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 (DocumentException | IOException e) {
e.printStackTrace();
}
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/2.pdf"));
document.open();
document.add(new Paragraph("hello word 你好 世界",font));//引用字体
document.close();
} catch (FileNotFoundException | DocumentException e) {
System.out.println("file create exception");
}
}

结果如下:

另外一种方法:我不用第三方语言包:

我是在工程目录里面新建了一个字体文件夹Font,然后把宋体的字体文件拷贝到这个文件夹里面了

上程序:

 public void test1_2(){
BaseFont bf;
Font font = null;
try {
bf = BaseFont.createFont("Font/simsun.ttc,1", //注意这里有一个,1
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
font = new Font(bf,12);
} catch (DocumentException | IOException e) {
e.printStackTrace();
}
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("pdfFolder/3.pdf"));
document.open();
document.add(new Paragraph("使用中文另外一种方法",font));
document.close();
} catch (FileNotFoundException | DocumentException e) {
System.out.println("file create exception");
}
}

结果“:

我如果换成:华康少女文字W5(P).TTC,即

bf = BaseFont.createFont("Font/华康少女文字W5(P).TTC,1", //simsun.ttc
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

哈哈,我喜欢图文并茂的教程02:44:58

新知识:Java 利用itext填写pdf模板并导出(昨天奋战到深夜四点,知道今天两点终于弄懂)的更多相关文章

  1. java操作Excel之POI(5)利用POI实现使用模板批量导出数据

    后台导出方法: 在源文件夹src下面放个准备好的模板:/com/cy/template/userExportTemplate.xls,这个模板有头部一行: /** * 后台导出方法 * 利用POI实现 ...

  2. PDF模板报表导出(Java+Acrobat+itext)

    1. 首先要安装Adobe Acrobat,装好之后用Acrobat从一个word,excel或者pdf中转换一个pdf模板,我做的模板很简单,直接写一个简单的word再生成一个pdf表单,之后编辑文 ...

  3. Itext读取PDF模板文件渲染数据后创建新文件

    Maven导入依赖 <properties> <itextpdf.version>5.5.0</itextpdf.version> <itext-asian. ...

  4. java利用itext导出pdf

    项目中有一功能是导出历史记录,可以导出pdf和excel,这里先说导出pdf.在网上查可以用那些方式导出pdf,用itext比较多广泛. 导出pdf可以使用两种方式,一是可以根据已有的pdf模板,进行 ...

  5. 【PDF】java使用Itext生成pdf文档--详解

    [API接口]  一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...

  6. java之数据填充PDF模板

    声明:由于业务场景需要,所以根据一个网友的完成的. 1.既然要使用PDF模板填充,那么就需要制作PDF模板,可以使用Adobe Acrobat DC,下载地址:https://carrot.ctfil ...

  7. Android iText向pdf模板插入数据和图片

    一.需求 这些日志在写App程序,有这么一个需求,就是需要生成格式统一的一个pdf文件,并向固定表格中填充数据,并且再在pdf中追加两页图片. 二.方案 手工设计一个pdf模板,这个具体步骤就不再赘述 ...

  8. java利用iTextWorker生成pdf

    使用itext生成pdf,在linux环境下,中文全部失踪,因为itext要在linux下支持中文字体需要引入itext-asian, 并添加一个字体类. public static class Pd ...

  9. Java利用IText导出PDF(更新)

    我很久以前写的还是上大学的时候写的:https://www.cnblogs.com/LUA123/p/5108007.html ,今天心血来潮决定更新一波. 看了下官网(https://itextpd ...

随机推荐

  1. Android:理解Fragment

    最近都在公司搞测试,静不下心来学android.今天就把Fragment搞懂吧. Fragment的几点要点: 1.用于大屏幕平板,容纳更多组件,可复用2.Fragment必须嵌入Activity中 ...

  2. hdu Code Lock

    题意是说有N个字母组成的密码锁, 如[wersdfj],   每一位上的字母可以转动, w可转动变成x, z变成a.但是题目规定, 只能同时转动某个区间上的所有字母, 如[1,3], 那么第1到第3个 ...

  3. Kmeans方法

    基本Kmeans算法介绍及其实现 http://blog.csdn.net/qll125596718/article/details/8243404/ kmeans++ http://www.52ml ...

  4. 禁止北京地区IP访问站点

    <script type="text/javascript" src="http://counter.sina.com.cn/ip" charset=&q ...

  5. Android WebApp开发使用Genymotion连接Fiddler2/Charles代理调试

    1.       目的 在模拟器的浏览器或app hybrid开发中遇到chrome调试代码为线上代码或者混淆代码时,可以利用fiddler/charles为genymotion配置代理, 可以方便的 ...

  6. Population Mean

    Probability and Statistics > Moments > History and Terminology > Disciplinary Terminology & ...

  7. KVC和KVO大优点

    都是动态的,运行时检查,给予了极大的方便

  8. paramiko模块,线程,进程

    关于paramiko模块 paramiko是基于Python实现的ssh2远程安全连接,支持认证及密钥方式远程执行命令.文件传输,中间ssh代理等 paramiko的安装: 安装好之后,用parami ...

  9. http cache 原理实战演习

    有篇博文介绍的原理已经比较清楚了,见下面链接, 本文给出实验结果. http://www.cnblogs.com/cocowool/archive/2011/08/22/2149929.html La ...

  10. 对于家政020 APP平台如何走出资本寒冬?

    成都亿合科技小编了解到,随着O2O烧钱大战过去,网络上流传的一份O2O项目死亡名单上显示,近年来,汽车.社区.旅游.教育等16个领域的多个O2O项目关门大吉,仅外卖餐饮O2O项目倒闭的就有十几个.只有 ...