1.簡介
一種基于Java的服務器端報表技術,用于制作PDF格式的報表。
iText7以前的版本是免費的,7及之后的版本收費。
2.快速入門
Maven坐標
<!--iText報表-->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.1</version>
</dependency>
<!--中文支持組件-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
- iText包5.5.9及之后的版本由于廠家易主,groupId變為com.itextpdf
- 注意:如果使用的文字Itext不支持,那么Itext在生成PDF的時候會直接跳過這些文字,不會報錯
使用步驟
-
創建Document文檔對象
空參構造器:
Document document = new Document();
有參構造器:
Document document = new Document(PageSize.A4,20,20,20,20);
參數一為紙張大小,后四個參數為邊距。
-
設置輸出位置
PdfWriter.getInstance(document,new FileOutputStream(new File(path)));
-
打開文檔
document.open();
-
寫入內容
document.add(new Paragraph("hello world"));
-
關閉文檔
document.close();
中文輸出支持問題
中文的輸出問題是Java本身的問題。
為了讓IText支持中文輸出,必須要導入Itext-Asian.jar亞洲語言包。而后在代碼中為Paragraph對象設置字體:
//定義中文基礎字體 參數:1.字體 2.橫豎排版樣式 3.是否內嵌
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//用中文基礎字體實例化一個字體類 參數:1.中文基礎字體 2.字號 3.是否斜體、加粗 4.顏色
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL,BaseColor.PINK);
//將字體用到一個段落中
Paragraph par = new Paragraph("薇薇的小龍蝦",FontChinese);
輸出表格
-
定義表格
PdfPTable table = new PdfPTable(3);
- 其中構造器的參數為表格的列數。
-
定義單元格
PdfPCell cell = new PdfPCell(new Phrase("月份", titleFont));
-
添加單元格到表格
table.addCell(cell);
- iText中的表格與POI不同,并不需要設置行。只是按照添加順序依次排列單元格,達到列數就自動換行。
3.范例:
public class ITextTest02 {
public static void main(String[] args) throws Exception{
//準備數
List<String[]> list = new ArrayList<String[]>();
list.add(new String[]{"七月","1000","1100"});
list.add(new String[]{"八月","950","1000"});
list.add(new String[]{"九月","1000","1200"});
//創建文檔對象
/**
* 第一個參數:PageSize:設置紙張的大小:A1,A2,A3,A4(默認值),A5
* 第2--5個參數:左右上下:紙張的邊距
*/
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
//設置輸出位置
PdfWriter.getInstance(document, new FileOutputStream(new File("d://a.pdf")));
//打開文檔
document.open();
//寫入內容
//創建亞洲中文字體
/**
* 第一個參數:當前的字體:宋體、楷體...
* 第二個參數:編碼
* 第三個參數:是否以內嵌的樣式顯示,值是boolean類型
* true:以內嵌的方式顯示,比較占用資源
* false:不適用內嵌的方式顯示,(更常用)
*/
BaseFont baseFont = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont,
AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
/**********大標題的輸出***********/
//設置字體樣式
/**
* 第一個參數:字體
* 第二個參數:字體大小
* 第三個參數:加粗、傾斜..
* 第四個參數:顏色
*/
Font bigTitleFont = new Font(baseFont, 30f, Font.BOLD, BaseColor.PINK);
//設置內容
/**
* 第一個參數:內容
* 第二個參數:字體格式
*/
Paragraph bigTitleParagraph = new Paragraph("銷量表", bigTitleFont);
//設置對其方式
bigTitleParagraph.setAlignment(Paragraph.ALIGN_CENTER);
//添加至文檔中
document.add(bigTitleParagraph);
/**********表格作者***********/
//設置字體樣式
Font authorFont = new Font(baseFont, 20f, Font.NORMAL, BaseColor.BLACK);
//設置內容
Paragraph authorParagraph = new Paragraph("Ivon", authorFont);
//設置對其方式
authorParagraph.setAlignment(Paragraph.ALIGN_RIGHT);
//添加至文檔中
document.add(authorParagraph);
/**********表格的創建***********/
PdfPTable table = new PdfPTable(3);
//設置當前面table的上邊距
table.setSpacingBefore(20f);
/**********表格標題***********/
//設置字體樣式
Font titleFont = new Font(baseFont, 15f, Font.BOLD, BaseColor.GREEN);
//設置內容 參數:1.內容 2.字體樣式
table.addCell(new PdfPCell(new Phrase("月份", titleFont)));
table.addCell(new PdfPCell(new Phrase("去年銷量", titleFont)));
table.addCell(new PdfPCell(new Phrase("今年銷量", titleFont)));
/**********表格內容***********/
//設置字體樣式
Font contentFont = new Font(baseFont, 15f, Font.NORMAL, BaseColor.GREEN);
//設置內容
for(String[] values:list){
table.addCell(new PdfPCell(new Phrase(values[0], contentFont)));
table.addCell(new PdfPCell(new Phrase(values[1], contentFont)));
table.addCell(new PdfPCell(new Phrase(values[2], contentFont)));
}
//將表格添加至document中
document.add(table);
//關閉文檔
document.close();
System.out.println("輸出完成");
}
}