一、iText簡介
iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉化為PDF文件。
使用iText非常方便,引入jar包,程序中就可以使用iText類庫了。iText.jar包下載地址:http://www.itextpdf.com/download.php
如果生成的PDF文件中需要出現中文、日文、韓文字符,則同樣的地址,下載extrajars-2.3.zip擴展包,里面包括itext-asian.jar等擴展工具包。
二、功能介紹
在企業的信息系統中,報表處理一直占比較重要的作用,iText組件通過在服務器端使用Jsp 或JavaBean生成PDF報表,客戶端采用超級連接顯示或下載得到生成的報表,這樣就很好的解決了B/S系統的報表處理問題。
適合使用IText的需求:
Typically, iText is used in projects that have one ofthe following requirements:
The content isn't available in advance: it'scalculated based on user input or real-time database information.
The PDF files can't be produced manually due to the massivevolume of content: a large number of pages or documents.
Documents need to be created in unattended mode, in abatch process.
The content needs to be customized or personalized;for instance, the name of the end user has to be stamped on a number of pages.
Often you'll encounter these requirements in webapplications, where content needs to be served dynamically to a browser.Normally, you'd serve this information in the form of HTML, but for somedocuments, PDF is preferred over HTML for better printing quality, foridentical presentation on a variety of platforms, for security reasons, or toreduce the file size.
通常,iText用於具有下列條件之一的項目:
內容不固定,它是基於用戶輸入或實時數據庫信息計算。
由於頁數多或者文件較大而造成的內容過多而使得PDF文件不能手動生成,。
文件需要在無人值守模式下創建的,使用批處理過程。
內容需要自定義或個性化;例如,最終用戶的名字需要被印在某一頁中。
通常你會在Web應用程序中遇到的這些要求,其中的內容對於瀏覽者來說必須是動態。通常,你會以HTML的形式提供這些信息,但對於一些文檔,PDF格式在印刷質量上是優於HTML的,同樣,在各種平台上,出於安全原因,或減少文件大小的考慮,PDF都優於HTML。
三、demo演示,一個最簡單的使用IText轉化為PDF的例子
用iText生成PDF文檔需要5個步驟:
①建立com.itextpdf.text.Document對象的實例。
Document document= new Document();
②建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中。
PDFWriter.getInstance(document,new FileOutputStream("ITextTest.pdf"));
③打開文檔。
document.open();
④向文檔中添加內容。
document.add(newParagraph("IText Test"));
⑤關閉文檔。
document.close();
通過上面的5個步驟,就能產生一個ITextTest.PDF的文件,文件內容為"ITextTest"。
具體代碼如下:
1 package com.wh; 2 3 importjava.io.FileOutputStream; 4 importcom.itextpdf.text.BaseColor; 5 importcom.itextpdf.text.Document; 6 importcom.itextpdf.text.Element; 7 importcom.itextpdf.text.Font; 8 importcom.itextpdf.text.Paragraph; 9 importcom.itextpdf.text.Rectangle; 10 importcom.itextpdf.text.pdf.BaseFont; 11 importcom.itextpdf.text.pdf.PdfPTable; 12 importcom.itextpdf.text.pdf.PdfWriter; 13 14 public class ToPDF{ 15 // 表頭 16 public static final String[] tableHeader= { "姓名", "性別", "年齡", 17 "學院", "專業", "年級"}; 18 19 // 數據表字段數 20 private static final int colNumber = 6; 21 22 // 表格的設置 23 private static final int spacing = 2; 24 25 // 表格的設置 26 private static final int padding = 2; 27 28 // 導出Pdf文擋 29 public static void exportPdfDocument() { 30 // 創建文Pdf文擋50, 50, 50,50左右上下距離 31 Document document = newDocument(new Rectangle(1500, 2000), 50, 50, 50, 32 50); 33 try { 34 //使用PDFWriter進行寫文件操作 35 PdfWriter.getInstance(document,new FileOutputStream( 36 "d:\\學生信息.pdf")); 37 document.open(); 38 // 中文字體 39 BaseFont bfChinese =BaseFont.createFont("STSong-Light", 40 "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 41 Font fontChinese = newFont(bfChinese, 12, Font.NORMAL); 42 // 創建有colNumber(6)列的表格 43 PdfPTable datatable = newPdfPTable(colNumber); 44 //定義表格的寬度 45 int[] cellsWidth = { 8, 2,2, 8, 5, 3 }; 46 datatable.setWidths(cellsWidth); 47 // 表格的寬度百分比 48 datatable.setWidthPercentage(100); 49 datatable.getDefaultCell().setPadding(padding); 50 datatable.getDefaultCell().setBorderWidth(spacing); 51 //設置表格的底色 52 datatable.getDefaultCell().setBackgroundColor(BaseColor.GREEN); 53 datatable.getDefaultCell().setHorizontalAlignment( 54 Element.ALIGN_CENTER); 55 // 添加表頭元素 56 for (int i = 0; i <colNumber; i++) { 57 datatable.addCell(newParagraph(tableHeader[i], fontChinese)); 58 } 59 // 添加子元素 60 for (int i = 0; i <colNumber; i++) { 61 datatable.addCell(newParagraph(tableHeader[i], fontChinese)); 62 } 63 document.add(datatable); 64 } catch (Exception e) { 65 e.printStackTrace(); 66 } 67 document.close(); 68 } 69 70 public static void main(String[] args)throws Exception { 71 exportPdfDocument(); 72 } 73 74 }
下載示例代碼及jar包請點擊: 下載