关于itext生成pdf的新的demo(包含简单的提取txt文件的内容 和xml内容转化为pdf)
一.用的iText版本为7.0.2版本,maven的配置如下:
<dependencies>
<!-- always needed -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.0.3</version>
</dependency>
<!-- always needed -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.0.3</version>
</dependency>
<!-- always needed -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.0.3</version>
</dependency>
<!-- only needed for forms -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.0.3</version>
</dependency>
<!-- only needed for PDF/A -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>pdfa</artifactId>
<version>7.0.3</version>
</dependency>
<!-- only needed for digital signatures -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>sign</artifactId>
<version>7.0.3</version>
</dependency>
<!-- only needed for barcodes -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>barcodes</artifactId>
<version>7.0.3</version>
</dependency>
<!-- only needed for Asian fonts -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>font-asian</artifactId>
<version>7.0.3</version>
</dependency>
<!-- only needed for hyphenation -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>hyph</artifactId>
<version>7.0.3</version>
</dependency>
</dependencies>
二.以下就是本次更新的demo代码
package com.cn.shupu.util; import java.awt.Font;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List; import org.apache.ibatis.datasource.pooled.PooledDataSourceFactory;
import org.apache.poi.hslf.model.textproperties.TextAlignmentProp;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import com.itextpdf.io.codec.Base64;
import com.itextpdf.io.font.FontProgram;
import com.itextpdf.io.font.FontProgramFactory;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.font.otf.Glyph;
import com.itextpdf.io.util.DecimalFormatUtil;
import com.itextpdf.kernel.events.Event;
import com.itextpdf.kernel.events.IEventHandler;
import com.itextpdf.kernel.events.PdfDocumentEvent;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.draw.ILineDrawer;
import com.itextpdf.kernel.pdf.filespec.PdfDictionaryFS;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.font.FontFamilySplitter;
import com.itextpdf.layout.font.FontSelector;
import com.itextpdf.layout.font.FontSet;
import com.itextpdf.layout.property.TabAlignment;
import com.itextpdf.layout.property.TextAlignment;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException; public class ItextForPDF { public static final String FONT = "C:\\Windows\\Fonts\\simsun.ttc,1";// 利用windows自带的字体,对中文和特殊字符处理
static DecimalFormat df = new DecimalFormat(); public static OutputStream createFile(String fileName) { File file = new File(fileName); if (!new File(file.getParent()).exists()) new File(file.getParent()).mkdirs(); try {
file.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} OutputStream os = null;
try {
os = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return os; } public static void txtToPdf(String txtPath, String pdfPath) { PdfWriter writer = new PdfWriter(createFile(pdfPath));
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = PageSize.A4;
// 中文处理和特殊字符处理
PdfFont font = null;
try {
font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Document document = new Document(pdf, pageSize); BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(txtPath), "GBK"));
String line = null;
while ((line = br.readLine()) != null) { Text text = new Text(line.trim());
Paragraph p = new Paragraph("\n").setFont(font).setFontSize(10f);
p.add(text);
document.add(p); } } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally { if (document != null) {
document.close();
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pdf != null) {
pdf.close();
}
}
} } public static void xmlToPDF(String xmlPath, String pdfPath) { PdfWriter writer = new PdfWriter(createFile(pdfPath));
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = PageSize.A4;
// 中文处理和特殊字符处理
PdfFont font = null;
try {
font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Document document = new Document(pdf, pageSize); BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(xmlPath), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) { Text text = new Text(line.trim());
Paragraph p = new Paragraph("\n").setFont(font).setFontSize(10f);
p.add(text);
document.add(p); } } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally { if (document != null) {
document.close();
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pdf != null) {
pdf.close();
}
}
}
} public static void wordToPdf(String wordPath,String pdfPath){ PdfWriter writer = new PdfWriter(createFile(pdfPath));
PdfDocument pdf = new PdfDocument(writer);
PageSize pageSize = PageSize.A4;
// 中文处理和特殊字符处理
PdfFont font = null;
try {
font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} Document document = new Document(pdf, pageSize); BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(wordPath), "GBK"));
String line = null;
while ((line = br.readLine()) != null) { Text text = new Text(line.trim());
Paragraph p = new Paragraph("\n").setFont(font).setFontSize(10f);
p.add(text);
document.add(p); } } catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally { if (document != null) {
document.close();
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pdf != null) {
pdf.close();
}
}
}
}
三.关于对于中文的处理:
1.对于中文的处理需要我添加一个itext-asian-5.2.0.jar包,可以对中文进行处理。而你可能下载不能是这个版本,可能会引起错误的。
一下是对之前版本的处理:
假如你加入的iTextAsian.jar的版本为5.0.6;会 出现异常 Font 'STSong-Light' with 'UniGB-UCS2-H'
我使用的是iText 5.0.6 加入了iTextAsian.jar就报错.
com.itextpdf.text.DocumentException: Font 'STSongStd-Light' with 'UniGB-UCS2-H' is not recognized.
参考博客:x
找的原因和解决方法:
原因:iText5以上就改了命名空间了.是 com/itextpdf/text/pdf/fonts/
但是iTextAsian还没有改.他的命名空间是 com/lowagie /text/pdf/fonts/
所以报错..
解决方法.:
1.用winrar解压iTextAsian.jar
2. 将com文件夹下面的lowagie 修改为itextpdf .
3.进入cmd . 切换到iTextAsian目录.
4.执行命令 jar cvf iTextAsian.jar com/itextpdf/text/pdf/fonts/*
5.将生成的iTextAsian.jar文件替换原来的.
问题解决.
//解决中文的代码:
PdfFont font = null;
try {
font = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
以上我用Itext7对一小段文字是没有问题,但是当对一个txt文档进行转化时,系统就会报错,大概意思就是pdfFont为null之类的。(具体的报错信息不记得)
2.这次对iText7版本来说:中文处理就没有用到了第三方的jar包
public static final String FONT = "C:\\Windows\\Fonts\\simsun.ttc,1";// 利用windows自带的字体,对中文和特殊字符处理 这个是再类中定义的全局的字体路径
// 中文处理和特殊字符处理
PdfFont font = null;
try {
font = PdfFontFactory.createFont(FONT, PdfEncodings.IDENTITY_H, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这个是iText7中不使用了itext-asian-5.2.0.jar之类的jar,而是可以加载第三方的字体库来支持中文字体的;
本人是刚出生的小猿,有什么不足之处,请大神提点.
关于itext生成pdf的新的demo(包含简单的提取txt文件的内容 和xml内容转化为pdf)的更多相关文章
- 【PDF】java使用Itext生成pdf文档--详解
[API接口] 一.Itext简介 API地址:javadoc/index.html:如 D:/MyJAR/原JAR包/PDF/itext-5.5.3/itextpdf-5.5.3-javadoc/ ...
- java使用iText生成pdf表格
转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...
- Itext生成pdf文件
来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...
- 【Java】使用iText生成PDF文件
iText介绍 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...
- 利用itext生成pdf的简单例子
一.itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...
- 在linux环境下使用itext生成pdf
转载请注明出处 https://www.cnblogs.com/majianming/p/9537173.html 项目中需要对订单生成pdf文件,在不断的尝试之后,终于生成了比较满意的pdf文档. ...
- Java Itext 生成PDF文件
利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...
- 用itext生成PDF报错:Font 'STSong-Light1' with 'UniGB-UCS2-H' is not recognized.
用itext生成PDF报错,加上try catch捕获到异常是 BaseFont bFont = BaseFont.createFont("STSong-Light1", &quo ...
- IText 生成简单表格(报表)doc文档 单元居中
IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下所示: import com.lowagie ...
随机推荐
- 模块_pip、os模块
一个python文件就是一个模块 1.标准模块 python自带的模块就是标准模块,也就是可以直接import进来的就是标准模块 import json import random import da ...
- linux系统(rpm与deb环境),JAVA JDK的配置
步骤一:(配置 JAVA JDK DEB系列linux系统) 1,下载JAVA JDK 1.1.官网下载java JDK (最好为1.7及以上版本) 下载地址http://www.oracle.co ...
- PHP防止网页快速刷新+代理ip访问
前几天网站收到了一些CC攻击,比较郁闷...这里分享一下,防止网页自动刷新的方法以及阻止代理IP访问网站的方法,代码是分开的,两个功能,需要那个用那个,可以自定义时间间隔,这个代码不止可以防CC攻击, ...
- APScheduler(Python化的Cron)使用总结 定时任务
APScheduler(Python化的Cron)使用总结 简介 APScheduler全程为Advanced Python Scheduler,是一款轻量级的Python任务调度框架.它允许你像Cr ...
- Docker Swarm bind 数据持久化
Docker Swarm bind 数据持久化 bind:主要将工作节点宿主级文件或目录,同步挂载到容器中. 环境: 系统:Centos 7.4 x64 应用版本:Docker 18.09.0 管理节 ...
- Python 新式类与经典类
新式类,经典类 查询匹配 广度查询 横着对每个类进行查询 深度查询 无视平级类,直接寻找下级类 #python 3.0 #新式类 广度查询 #经典类 广度查询 #python 2.0 #新式类 广度查 ...
- Python新手入门英文词汇(1-1)
英文词汇总结一.交互式环境与print输出 1.print:打印/输出2.coding:编码3.syntax:语法4.error:错误5.invalid:无效6.identifier:名称/标识符7. ...
- LINQ之路11:LINQ Operators之过滤(Filtering)
在本系列博客前面的篇章中,已经对LINQ的作用.C# 3.0为LINQ提供的新特性,还有几种典型的LINQ技术:LINQ to Objects.LINQ to SQL.Entity Framework ...
- 虚拟现实外包公司—VR游戏你不知道的以及你该知道的WebVR
VR基础——原理其实很简单 我们地球人之所以能够看到立体的景物,是因为双眼可以各自独立看东西,也就是左眼只能看到左眼的景物,而右眼只能看到右眼的景物.因为人类左右两眼有间距,造成两眼的视角有些细微的差 ...
- gevent-协程用法
文章介绍了一种采用循环的方式生产协程列表,并可以向协程函数传递参数... # 协程引用import gevent from gevent import monkey, pool monkey.patc ...