* STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀
* UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀
* H 代表文字版式是 横版, 相应的 V 代表 竖版
*/
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);
// 打开文档,将要写入内容
document.open();
// 插入一个段落
Paragraph par = new Paragraph("我们",fontChinese);
document.add(par);
}
catch (DocumentException de)
{
System.err.println(de.getMessage());
}
catch (IOException ioe)
{
System.err.println(ioe.getMessage());
}
// 关闭打开的文档
document.close();
}
}
就可以显示中文了。
四、其他问题:(应导入相应的包)
1. 换页:
document.newPage();
2. 表格:
// 设置 Table
Table aTable = new Table(3);
int width[] = {25,25,50};
aTable.setWidths(width);
aTable.setWidth(80); // 占页面宽度 80%aTable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT);
aTable.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);
aTable.setAutoFillEmptyCells(true); //自动填满
aTable.setPadding(1);
aTable.setSpacing(1);
aTable.setDefaultCellBorder(0);
aTable.setBorder(0);Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese ));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setRowspan(3);
aTable.addCell(cell);aTable.addCell(new Cell("#1"));
aTable.addCell(new Cell("#2"));
aTable.addCell(new Cell("#3"));aTable.addCell(new Cell("#4"));
aTable.addCell(new Cell("#5"));
aTable.addCell(new Cell("#6"));document.add(aTable);
3. 图片:
// 可以是绝对路径,也可以是URL
Image img = Image.getInstance("logo.gif");// Image image = Image.getInstance(new URL(http://xxx.com/logo.jpg));
img.setAbsolutePosition(0, 0);
document.add(img);
五、参考文档:
- iText
http://www.lowagie.com/iText/- iText API:
http://itext.sourceforge.net/docs/
http://www.sentom.net/list.asp?id=42
上一篇: Win2003中apache2分别整合tomcat5和iis6的终极教程
下一篇: 使用python为Java项目增加一个预编译脚本
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




