Itext中文处理:
在Itext中的中文处理随着Itext的版本变化,采取的方法也在变化。
首先我来演示一下利用Itext-2.1.3.jar与iTextAsian.jar两个包来解决中文问题。
附件中有所需要的包:
示例代码:
Java代码 <SPAN style="FONT-SIZE: small">package com.lwf.pdf.test;import java.awt.Color;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Font;import com.lowagie.text.Paragraph;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.pdf.PdfWriter; public class HelloWord { public static void main(String[] args) { try { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("c://good.pdf")); doc.open(); BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" , "UniGB-UCS2-H" , false ); Font fontChinese = new Font(bfChinese , 12 , Font.NORMAL, Color.GREEN); Paragraph pf = new Paragraph("eee"); pf.add(new Paragraph("我们的家好大的家",fontChinese)); pf.add(new Paragraph("sfsfsf")); doc.add(pf); doc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } </SPAN>上面代码测试环境是:JDK1.4编译环境也为1.4,当然在以后的JDK版本中测试也是没问题的。
好了,现在最新的Itext版本为Itext 5.0,附件中iText.jar即为该版本。以下为Itext5.0的修改:
http://itextpdf.com/history.php
关注两句话:
From now on, the iText.jar is compiled using Java 5 (instead of with the JDK 1.4).
The package names have changed from com.lowagie to com.itextpdf.
上面两句话第一说明itext5.0用于JDK1.5以及之后的版本。第二句更改了包名。
好了,如果我现在的程序使用iText.jar+iTextAsian.jar运行下面程序:
Java代码 <SPAN style="FONT-SIZE: small">package com.lwf.pdf.test; import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException; import com.itextpdf.text.BaseColor;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Font;import com.itextpdf.text.Paragraph;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.PdfWriter; public class HelloWord { public static void main(String[] args) { try { Document doc = new Document(); PdfWriter.getInstance(doc, new FileOutputStream("c://good.pdf")); doc.open(); BaseFont bfChinese = BaseFont.createFont( "STSongStd-Light" , "UniGB-UCS2-H" , false ); //Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN); Font fontChinese = new Font(bfChinese , 12 , Font.NORMAL, BaseColor.GREEN);
Paragraph pf = new Paragraph("eee"); pf.add(new Paragraph("我们的家好大的家",fontChinese)); pf.add(new Paragraph("sfsfsf")); doc.add(pf); doc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
}
</SPAN>
会报以下错误:
Java代码 : <SPAN style="FONT-SIZE: small">com.itextpdf.text.DocumentException: No message found for font.1.with.2.is.not.recognized at com.itextpdf.text.pdf.BaseFont.createFont(Unknown Source) at com.itextpdf.text.pdf.BaseFont.createFont(Unknown Source) at com.itextpdf.text.pdf.BaseFont.createFont(Unknown Source) at com.lwf.pdf.test.HelloWord.main(HelloWord.java:24)</SPAN>
这是为什么呢?
不难发现上面两个测试只是更改了iText包。而iTextAsian.jar只是包含了亚洲国家的文字信息。打开iTextAsian.jar发现它采用了与原来iText-2.1.3.jar一样的包名:com.lowagie.text.Font而现在的iText5.0包名已经改为了com.itextpdf.text.Font,显然我们更一下包名就可以解决问题。在winrar中把对应包名重命名加进来即可。我把改好的jar放在附件的更改.rar中。好了,那么现在使用iText.jar+更改.rar中的iTextAsian.jar测试就不会报错了。
其实这个问题也有文章谈到,如:http://xiaolongfeixiang.javaeye.com/blog/577816
我在itext官方网站上,查iTextAsian,发现这样的文章http://www.itextpdf.com/examples/index.php?page=example&id=214
显然,它是一个示例。那么同样使用iText5.0+更改后的iTextAsian包,下面有另一种实现方法:
Java代码 : /* * This class is part of the book "iText in Action - 2nd Edition" * written by Bruno Lowagie (ISBN: 9781935182610) * For more info, go to: http://itextpdf.com/examples/ * This example only works with the AGPL version of iText. */ import java.io.FileOutputStream;import java.io.IOException; import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.FontFactory;import com.itextpdf.text.PageSize;import com.itextpdf.text.Paragraph;import com.itextpdf.text.Phrase;import com.itextpdf.text.pdf.BaseFont;import com.itextpdf.text.pdf.FontSelector;import com.itextpdf.text.pdf.PdfWriter; /** * This example was written by Bruno Lowagie. It is part of the book 'iText in * Action' by Manning Publications. * ISBN: 1932394796 * http://www.1t3xt.com/docs/book.php * http://www.manning.com/lowagie/ */ public class FontSelectionExample { /** The resulting PDF file. */ public static final String RESULT = "results/part3/chapter11/font_selection.pdf"; /** Some text */ public static final String TEXT = "These are the protagonists in 'Hero', a movie by Zhang Yimou:/n" + "/u7121/u540d (Nameless), /u6b98/u528d (Broken Sword), " + "/u98db/u96ea (Flying Snow), /u5982/u6708 (Moon), " + "/u79e6/u738b (the King), and /u9577/u7a7a (Sky)." + "中华人民共和国万岁劳动人民是光荣的,岁月峥嵘,百舸争流" ; /** * Creates a PDF document. * @param filename the path to the new PDF document * @throws DocumentException * @throws IOException * @throws DocumentException * @throws IOException */ public void createPdf(String filename) throws IOException, DocumentException { // step 1 Document document = new Document(PageSize.A4); // step 2 PdfWriter.getInstance(document, new FileOutputStream(filename)); // step 3: we open the document document.open(); // step 4: FontSelector selector = new FontSelector(); selector.addFont(FontFactory.getFont(FontFactory.TIMES_ROMAN, 12));// selector.addFont(FontFactory.getFont("MSung-Light",// "UniCNS-UCS2-H", BaseFont.NOT_EMBEDDED));// selector.addFont(FontFactory.getFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED)); Phrase ph = selector.process(TEXT); document.add(new Paragraph(ph)); // step 5: we close the document document.close(); } /** * Main method. * * @param args no arguments needed * @throws DocumentException * @throws IOException */ public static void main(String[] args) throws IOException, DocumentException { new FontSelectionExample().createPdf("c:/good.pdf"); }}