Java iText 動態 生成 PDF 文檔表格中文問題

iText是一個開發源代碼的項目,你可以使用iText方便的實現PDF的輸出。
 
 
一、iText的下載:

    你可以在 http://www.lowagie.com/iText/ 查看關於iText的相關信息,包括源代碼,文檔..

    1. itext-src-1.4.zip(源代碼)
        
http://jaist.dl.sourceforge.net/sourceforge/itext/itext-src-1.4.zip

    2. itext-1.4.jar(可以直接導入的jar文件)

        http://jaist.dl.sourceforge.net/sourceforge/itext/itext-1.4.jar

    3. 亞洲語言包

        http://itextdocs.lowagie.com/downloads/iTextAsian.jar          (或者)

        http://itextdocs.lowagie.com/downloads/iTextAsianCmaps.jar

       http://prdownloads.sourceforge.net/itext/iTextAsian.jar

二、示例程序:

        首先把上面的這幾個jar包的完整路徑添加到環境變量 classpath 中,然後再下面的程序
        中導入相應的包

java 代碼
  1. /**  
  2.   * HelloWorld.java  
  3.  */  
  4.   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7.   
  8. import com.lowagie.text.*;   
  9. import com.lowagie.text.pdf.PdfWriter;   
  10.   
  11. public class HelloWorld {   
  12.   
  13.   public static void main(String[] args) {   
  14.   
  15.     System.out.println("Hello World");   
  16.   
  17.     // 創建一個Document對象   
  18.     Document document = new Document();   
  19.   
  20.     try  
  21.     {   
  22.   
  23.       // 生成名爲 HelloWorld.pdf 的文檔   
  24.       PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));   
  25.   
  26.       // 添加PDF文檔的一些信息   
  27.       document.addTitle("Hello World example");   
  28.       document.addAuthor("Bruno Lowagie");   
  29.       document.addSubject("This example explains how to add metadata.");   
  30.       document.addKeywords("iText, Hello World, step 3, metadata");   
  31.       document.addCreator("My program using iText");   
  32.   
  33.       // 打開文檔,將要寫入內容   
  34.       document.open();   
  35.   
  36.       // 插入一個段落   
  37.       document.add(new Paragraph("Hello World!"));   
  38.   
  39.     }    
  40.     catch (DocumentException de)   
  41.     {   
  42.       System.err.println(de.getMessage());   
  43.     }   
  44.     catch (IOException ioe)   
  45.     {   
  46.       System.err.println(ioe.getMessage());   
  47.     }   
  48.   
  49.     // 關閉打開的文檔   
  50.     document.close();   
  51.   }   
  52. }    
  53.   

 

編譯運行以後,我們可以在運行的目錄發現生成的HelloWorld.pdf,打開可以看到我們寫的文字:

Hello World



三、中文問題:

        由於iText不支持東亞語言,我們下載了iTextAsian.jar 以後,就可以在PDF裏面寫中文:

java 代碼
  1. /**  
  2.   * AsianTest.java  
  3.  */  
  4.   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7.   
  8. import com.lowagie.text.*;   
  9. import com.lowagie.text.pdf.PdfWriter;   
  10. import com.lowagie.text.pdf.BaseFont;   
  11. import com.lowagie.text.Font;   
  12. import java.awt.Color;   
  13.   
  14. public class AsianTest{   
  15.   
  16.   public static void main(String[] args) {   
  17.   
  18.     // 創建一個Document對象   
  19.     Document document = new Document();   
  20.   
  21.     try  
  22.     {   
  23.   
  24.       // 生成名爲 AsianTest.pdf 的文檔   
  25.       PdfWriter.getInstance(document, new FileOutputStream("AsianTest.pdf"));   
  26.   
  27.      /**  新建一個字體,iText的方法  
  28.        *  STSongStd-Light 是字體,在iTextAsian.jar 中以property爲後綴  
  29.        *  UniGB-UCS2-H   是編碼,在iTextAsian.jar 中以cmap爲後綴  
  30.        *  H 代表文字版式是 橫版, 相應的 V 代表豎版  
  31.       */  
  32.       BaseFont bfChinese = BaseFont.createFont("STSongStd-Light""UniGB-UCS2-H"false);   
  33.   
  34.         Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);   
  35.   
  36.       // 打開文檔,將要寫入內容   
  37.       document.open();   
  38.   
  39.       // 插入一個段落   
  40.       Paragraph par = new Paragraph("我們",fontChinese);   
  41.   
  42.       document.add(par);   
  43.   
  44.     }    
  45.     catch (DocumentException de)   
  46.     {   
  47.       System.err.println(de.getMessage());   
  48.     }   
  49.     catch (IOException ioe)   
  50.     {   
  51.       System.err.println(ioe.getMessage());   
  52.     }   
  53.   
  54.     // 關閉打開的文檔   
  55.     document.close();   
  56.   }   
  57. }   
  58.   

四、其他問題:(應導入相應的包)

java 代碼
  1.  1. 換頁:   
  2.   
  3. document.newPage();   
  4.   
  5.        2. 表格:   
  6.   
  7. // 設置 Table   
  8. Table aTable = new Table(3);   
  9. int width[] = {25,25,50};   
  10. aTable.setWidths(width);   
  11. aTable.setWidth(80); // 佔頁面寬度 80%   
  12.   
  13. aTable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT);   
  14. aTable.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);   
  15. aTable.setAutoFillEmptyCells(true); //自動填滿   
  16. aTable.setPadding(1);   
  17. aTable.setSpacing(1);   
  18. aTable.setDefaultCellBorder(0);   
  19. aTable.setBorder(0);   
  20.   
  21. Cell cell = new Cell(new Phrase("這是一個測試的 3*3 Table 數據", fontChinese ));   
  22. cell.setVerticalAlignment(Element.ALIGN_TOP);   
  23. cell.setRowspan(3);   
  24. aTable.addCell(cell);   
  25.   
  26. aTable.addCell(new Cell("#1"));   
  27. aTable.addCell(new Cell("#2"));   
  28. aTable.addCell(new Cell("#3"));   
  29.   
  30. aTable.addCell(new Cell("#4"));   
  31. aTable.addCell(new Cell("#5"));   
  32. aTable.addCell(new Cell("#6"));   
  33.   
  34. document.add(aTable);   
  35.   
  36.        3. 圖片:   
  37.   
  38. // 可以是絕對路徑,也可以是URL   
  39. Image img = Image.getInstance("logo.gif");   
  40.   
  41. // Image image = Image.getInstance(new URL(http://xxx.com/logo.jpg));   
  42.   
  43. img.setAbsolutePosition(00);   
  44.   
  45. document.add(img);   
  46.   

 

五、參考文檔:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章