◎筱米加步枪◎.Blog

Happy coding

使用iText5.x创建PDF中文处理问题

筱米加步枪 posted @ 2010年6月13日 00:57 in [ 开源框架 ] with tags iText , 11998 阅读

今天无意中在网上看到iText这个东东,iText 是利用Java 来操作PDF 操作的一种开源API

简单说明下使用该API创建PDF文件的过程

PS:使用的是iText5.x版本

/**
	 * 创建PDF文件
	 * @param filePath  文件路径
	 * @param content   需要写入的内容
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void createPdf(String filePath ,String content) throws DocumentException, IOException{
		//1.创建Document对象
		Document document = new Document();
		FileOutputStream fos = new FileOutputStream(filePath);
		//2.创建一个PdfWriter实例
		PdfWriter.getInstance(document, fos);
		//3.打开文档
		document.open();
		Paragraph graph = new Paragraph(content);
		//4.加入段落
		document.add(graph);
		//5.关闭文档
		document.close();
	}

利用上述程序,运行结果。发现,只有英文部分被写入,中文部分无法被写入。百度得到结论:

需要加入itextasian.jar包,itextasian.jar包有实现了对中文字体的支持。因此加载itextasian.jar到classpath下。

在上述代码中加入如下代码:

BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese =  new  Font(baseFontChinese , 12 , Font.NORMAL);
		Paragraph graph = new Paragraph(content , fontChinese);

运行,得到如下异常:

Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized

还是不行,继续研究,在网上前辈们说如下原因:

  iText5.x版本以上中的font和encoding文件都是从String RESOURCE_PATH = "com/itextpdf/text/pdf/fonts/"加载的,而老itextasian.jar的包名是com.lowagie.text.pdf.fonts, 包名不一致导致路径错误,。

具体解决方法就是修改包的路径了,详细方法如下:

1.解压iTextAsian.jar
  得到如下目录:
  iTextAsian
     --com
        --lowagie
          --text
            --pdf
              --fonts
                --...(字体属性文件)
2.将解压后的com目录下的包名lowagie更改为itextpdf
3.在命令行转至iTextAsian目录,重新打包为iTextAsian.jar文件
4.打包命令如下:
  jar cvf iTextAsian.jar com/itextpdf/text/pdf/fonts/*
5.执行后,将新的iTextAsian.jar加入classpath路径

运行结果,OK,解决问题。

最终代码如下:

/**
	 * 创建PDF文件
	 * @param filePath  文件路径
	 * @param content   需要写入的内容
	 * @throws DocumentException
	 * @throws IOException
	 */
	public void createPdf(String filePath ,String content) throws DocumentException, IOException{
		//1.创建Document对象
		Document document = new Document();
		FileOutputStream fos = new FileOutputStream(filePath);
		//2.创建一个PdfWriter实例
		PdfWriter.getInstance(document, fos);
		//3.打开文档
		document.open();
		BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		Font fontChinese =  new  Font(baseFontChinese , 12 , Font.NORMAL);
		Paragraph graph = new Paragraph(content , fontChinese);
		document.add(graph);
		document.close();
	}

 继续研究ing~~

  • 无匹配
  • 无匹配
Hey 说:
2010年10月18日 03:47

请问~ itextAsian只要进行buildpath就ok了么? 我还是会出现not found的情况

筱米加步枪 说:
2010年10月18日 06:08

@Hey: 是的,在Build Path中加入itextasian.jar或者把这个包引用进来,你的可能是版本问题。

Martin_Bonnie 说:
2012年3月09日 03:07

我的修改了也不行 奇怪了

civaget 说:
2023年12月07日 09:28

Don't overlook the significance of 백링크. They are the lifeblood of SEO. Focus on acquiring links from reputable sources to bolster your website's credibility.

civaget 说:
2023年12月14日 02:54

휴식 is more than a break; it's a journey to rediscover inner vitality and restore life's balance.

SEO 说:
2023年12月14日 03:08

메이저사이트 and Toto Match: A winning combination! Count on them for secure, exhilarating betting ventures.

civaget 说:
2023年12月18日 04:37

인천출장마사지: Your ticket to relaxation. Experience customized massages at your location, designed for your well-being.

civaget 说:
2023年12月25日 06:10

epl무료중계's expert insights bring the beautiful game to life for passionate fans.

civaget 说:
2024年1月02日 00:36

As a frequent traveler, I appreciate that 누누티비 is accessible worldwide.

civaget 说:
2024年1月10日 03:36

안전한 바카라 사이트 is the real deal for safe baccarat gaming.

civaget 说:
2024年1月14日 03:53

I absolutely adore your site! You aggressive me as able-bodied as all the others actuality and your broiled PS is absolutely great! เว็บคืนยอดเสีย

civaget 说:
2024年1月15日 05:07

A really exciting examine, I may possibly not concur entirely, but you do make some really legitimate points. live slot138

klxgfnogfjo 说:
2024年4月18日 03:07

It seems like you're referring to the "Glock G-FLX" trigger. The Glock G-FLX trigger is a product offered by a few different aftermarket companies, designed to provide an enhanced trigger feel and performance for Glock pistols. Glock Switch for Sale


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter