关于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,而是可以加载第三方的字体库来支持中文字体的;
本人是刚出生的小猿,有什么不足之处,请大神提点.