为什么用ITEXT生成含汉字的pdf总是不成功,已下载itextasian.jar,并设置了CLASSPATH
E:\> javac pp.java
E:\> java pp
null
源码如下
import java.io.*;
import
java.io.IOException;
import java.io.FileOutputStream;
import java.lang.*;
import java.lang.String;
import java.util.*;
import java.text.*;
import java.awt.Color;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.PageSize;
import com.lowagie.text.Document;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.Phrase;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Element;
import com.lowagie.text.pdf.PdfPageEventHelper;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.DocumentException;
import com.lowagie.text.ExceptionConverter;
import com.lowagie.text.Image;
public class pp extends PdfPageEventHelper
{
public static void main(String[] args) throws Exception
{
Document document = new Document(PageSize.A4, 20, 20, 90, 30);
try
{
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream( "aa.pdf "));
writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);
writer.setPageEvent(new pdf());
document.open();
BaseFont bfChinese = BaseFont.createFont( "STSong-Light ", "UniGB-UCS2-H ", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese,12,Font.NORMAL);
Paragraph chunk = new Paragraph( "我们是害虫 ", FontChinese);
document.add(chunk);
document.close();
}catch (Exception e) {System.out.println(e.getMessage());}
}
}
------解决方案--------------------呵呵!!因为你要加载windows里面的汉字包
如:
BaseFont bfChinese = BaseFont.createFont( "../webapps/CMCS3.1/business/overDraft/SIMYOU.TTF ", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
SIMYOU.TTF这个文件在windows的系统目录中可以找到的,
------解决方案--------------------//writer.setPageEvent(new pdf());
找不到pdf类,所以将此句引掉。
然后执行,能够正确地生成aa.pdf。
我的环境运行环境是:jdk1.5.0。是不是jdk版本问题?