一.用的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)的更多相关文章

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

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

  2. java使用iText生成pdf表格

    转载地址:http://www.open-open.com/code/view/1424011530749 首先需要你自己下载itext相关的jar包并添加引用,或者在maven中添加如下引用配置: ...

  3. Itext生成pdf文件

    来源:https://my.oschina.net/lujianing/blog/894365 1.背景 在某些业务场景中,需要提供相关的电子凭证,比如网银/支付宝中转账的电子回单,签约的电子合同等. ...

  4. 【Java】使用iText生成PDF文件

    iText介绍 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文件转 ...

  5. 利用itext生成pdf的简单例子

    一.itext简介 iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库.通过iText不仅可以生成PDF或rtf的文档,而且可以将XML.Html文 ...

  6. 在linux环境下使用itext生成pdf

    转载请注明出处 https://www.cnblogs.com/majianming/p/9537173.html 项目中需要对订单生成pdf文件,在不断的尝试之后,终于生成了比较满意的pdf文档. ...

  7. Java Itext 生成PDF文件

    利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...

  8. 用itext生成PDF报错:Font 'STSong-Light1' with 'UniGB-UCS2-H' is not recognized.

    用itext生成PDF报错,加上try catch捕获到异常是 BaseFont bFont = BaseFont.createFont("STSong-Light1", &quo ...

  9. IText 生成简单表格(报表)doc文档 单元居中

    IText生成doc文档需要三个包:iTextAsian.jar,iText-rtf-2.1.4.jar,iText-2.1.4.jar 亲测无误,代码如下所示: import com.lowagie ...

随机推荐

  1. Python 多进程基本语法

    需求:  在有多线程的情况下,我们可以使用线程帮我们处理一些事情,但是在python这里 由于RSA锁的缘故,我们只能够用到一个cpu帮我们处理事情,一个cpu在处理多个线程时,是通过上下文的切换使我 ...

  2. 最短路径之Bellman-Ford算法的队列优化及邻接表

    参考链接:https://blog.csdn.net/qq_40626497/article/details/81139344

  3. Thinkphph 使用RelationModel的三表关联查询机制

    有如下三个表 a表 b表 c表id bid other id cid other id other a表的bid关联b表的id,b表的cid关联c表的id 现在需要查询a表的时候顺带把b表和c表的相关 ...

  4. python requests 上传文件

    token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoiZWM2ZWFjZjFjM2UzYzEyOWU3ODA4YjgwNzkxNGI ...

  5. AngularJS服务及注入--Provider

    Provider简介 在AngularJS中,app中的大多数对象通过injector服务初始化和连接在一起. Injector创建两种类型的对象,service对象和特别对象. Service对象由 ...

  6. 通过RequestContextHolder直接获取HttpServletRequest对象

    问题 朋友遇到一个问题:他想在Service方法中使用HttpServletRequest的API,但是又不想把HttpServletRequest对象当作这个Service方法的参数传过来,原因是这 ...

  7. 安卓MVP框架

    一.理解MVP 原文地址 我的Demo 效果图: 项目结构: 实现 一.Model层 首先定义一个实体类User package app.qcu.pmit.cn.mvpdemo.model; /** ...

  8. 开源项目Zookeeper、Doozer、etcd进行总结

    Jason Wilder的一篇博客对分别对常见的服务发现开源项目Zookeeper.Doozer.etcd进行了总结介绍: Zookeeper是一个用户维护配置信息.命名.分布式同步以及分组服务的集中 ...

  9. Log4Net 添加自定义字段并保存到数据库

    Log4Net是常用的功能强大的日志插件,该插件提供了几个默认字段 大家可能都用过Log4Net插件来记录日志,该插件默认提供了这几个字段@log_date, @thread, @log_level, ...

  10. Codeforces Beta Round #19C. Deletion of Repeats

    题意:给一个数组,每次会删去连续重复两次的左侧部分及前面,有多个重复部分找长度最小和最靠左的部分,重复的数字最多10次 题解:根据重复数字只有10次,我们离散化后,以每两个相同数字作为起点能确定这重复 ...