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();
}