1.簡介
一種基于Java的服務器端報表技術(shù)诗箍,用于制作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變?yōu)閏om.itextpdf
- 注意:如果使用的文字Itext不支持,那么Itext在生成PDF的時候會直接跳過這些文字匠童,不會報錯
使用步驟
-
創(chuàng)建Document文檔對象
空參構(gòu)造器:
Document document = new Document();
有參構(gòu)造器:
Document document = new Document(PageSize.A4,20,20,20,20);
參數(shù)一為紙張大小埂材,后四個參數(shù)為邊距。
-
設置輸出位置
PdfWriter.getInstance(document,new FileOutputStream(new File(path)));
-
打開文檔
document.open();
-
寫入內(nèi)容
document.add(new Paragraph("hello world"));
-
關(guān)閉文檔
document.close();
中文輸出支持問題
中文的輸出問題是Java本身的問題俏让。
為了讓IText支持中文輸出楞遏,必須要導入Itext-Asian.jar亞洲語言包茬暇。而后在代碼中為Paragraph對象設置字體:
//定義中文基礎(chǔ)字體 參數(shù):1.字體 2.橫豎排版樣式 3.是否內(nèi)嵌
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//用中文基礎(chǔ)字體實例化一個字體類 參數(shù):1.中文基礎(chǔ)字體 2.字號 3.是否斜體首昔、加粗 4.顏色
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL,BaseColor.PINK);
//將字體用到一個段落中
Paragraph par = new Paragraph("薇薇的小龍蝦",FontChinese);
輸出表格
-
定義表格
PdfPTable table = new PdfPTable(3);
- 其中構(gòu)造器的參數(shù)為表格的列數(shù)糙俗。
-
定義單元格
PdfPCell cell = new PdfPCell(new Phrase("月份", titleFont));
-
添加單元格到表格
table.addCell(cell);
- iText中的表格與POI不同勒奇,并不需要設置行。只是按照添加順序依次排列單元格巧骚,達到列數(shù)就自動換行赊颠。
3.范例:
public class ITextTest02 {
public static void main(String[] args) throws Exception{
//準備數(shù)
List<String[]> list = new ArrayList<String[]>();
list.add(new String[]{"七月","1000","1100"});
list.add(new String[]{"八月","950","1000"});
list.add(new String[]{"九月","1000","1200"});
//創(chuàng)建文檔對象
/**
* 第一個參數(shù):PageSize:設置紙張的大小:A1劈彪,A2竣蹦,A3,A4(默認值),A5
* 第2--5個參數(shù):左右上下:紙張的邊距
*/
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
//設置輸出位置
PdfWriter.getInstance(document, new FileOutputStream(new File("d://a.pdf")));
//打開文檔
document.open();
//寫入內(nèi)容
//創(chuàng)建亞洲中文字體
/**
* 第一個參數(shù):當前的字體:宋體沧奴、楷體...
* 第二個參數(shù):編碼
* 第三個參數(shù):是否以內(nèi)嵌的樣式顯示痘括,值是boolean類型
* true:以內(nèi)嵌的方式顯示,比較占用資源
* false:不適用內(nèi)嵌的方式顯示滔吠,(更常用)
*/
BaseFont baseFont = BaseFont.createFont(AsianFontMapper.ChineseSimplifiedFont,
AsianFontMapper.ChineseSimplifiedEncoding_H, BaseFont.NOT_EMBEDDED);
/**********大標題的輸出***********/
//設置字體樣式
/**
* 第一個參數(shù):字體
* 第二個參數(shù):字體大小
* 第三個參數(shù):加粗纲菌、傾斜..
* 第四個參數(shù):顏色
*/
Font bigTitleFont = new Font(baseFont, 30f, Font.BOLD, BaseColor.PINK);
//設置內(nèi)容
/**
* 第一個參數(shù):內(nèi)容
* 第二個參數(shù):字體格式
*/
Paragraph bigTitleParagraph = new Paragraph("銷量表", bigTitleFont);
//設置對其方式
bigTitleParagraph.setAlignment(Paragraph.ALIGN_CENTER);
//添加至文檔中
document.add(bigTitleParagraph);
/**********表格作者***********/
//設置字體樣式
Font authorFont = new Font(baseFont, 20f, Font.NORMAL, BaseColor.BLACK);
//設置內(nèi)容
Paragraph authorParagraph = new Paragraph("Ivon", authorFont);
//設置對其方式
authorParagraph.setAlignment(Paragraph.ALIGN_RIGHT);
//添加至文檔中
document.add(authorParagraph);
/**********表格的創(chuàng)建***********/
PdfPTable table = new PdfPTable(3);
//設置當前面table的上邊距
table.setSpacingBefore(20f);
/**********表格標題***********/
//設置字體樣式
Font titleFont = new Font(baseFont, 15f, Font.BOLD, BaseColor.GREEN);
//設置內(nèi)容 參數(shù):1.內(nèi)容 2.字體樣式
table.addCell(new PdfPCell(new Phrase("月份", titleFont)));
table.addCell(new PdfPCell(new Phrase("去年銷量", titleFont)));
table.addCell(new PdfPCell(new Phrase("今年銷量", titleFont)));
/**********表格內(nèi)容***********/
//設置字體樣式
Font contentFont = new Font(baseFont, 15f, Font.NORMAL, BaseColor.GREEN);
//設置內(nèi)容
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);
//關(guān)閉文檔
document.close();
System.out.println("輸出完成");
}
}