您的位置:首页 > 理论基础

Itext的简单应用和学习

2015-08-02 12:09 267 查看
1.Itext使用需要使用itext.jar包。

2.Itext使用中文,主要有两种方式,下载itextasian.jar包,或者使用本地计算机字体。

   本地计算机字体:

<span style="font-size:12px;"> BaseFont bfHei = BaseFont.createFont("c:/Windows/fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfHei, 32);
String text = "这是黑体字测试!";
document.add(new Paragraph(text, font));</span>   使用iTextAsian.jar中的字体:
<span style="font-size:12px;"> BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);</span>3.Itext简单实例使用。
Document document = new Document(PageSize.A3);
document.addAuthor("Ryan");
document.addCreationDate();
try {
PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
//Chunk chunk = new Chunk("Holle  乐", new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED), 32));
document.add(new Paragraph("Holle world!"));
<span style="white-space:pre">	</span>Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 255));
table.setPadding(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setColspan(3);
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("cell test1");
cell = new Cell("big cell");
cell.setRowspan(2);
cell.setColspan(2);
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
//document.add(chunk);
BaseFont bfHei = BaseFont.createFont("c:/Windows/fonts/SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(bfHei, 32);
String text = "这是黑体字测试!";
document.add(new Paragraph(text, font));
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  计算机