iText5報表__表格處理

來源:互聯網
上載者:User
1.概述
       對於比較簡單的表格處理可以用Table,但是如果要處理複雜的表格,這就需要PDFPTable進行處理。
  建立表格之後,可以設定表格的屬性,如:邊框寬度、邊框顏色、襯距(padding space 即儲存格之間的間距)大小等屬性。下面通過一個簡單的例子說明如何使用表格,代碼如下: 

2.表格的操作
2.1 表格的初始化
       你可以用3種不同的方法建立PdfTable:
              PdfPTable(float[] relativeWidths);
              PdfPTable(int numColumns);
              PdfPTable(PdfPTable table);
       舉例:
              // 建立一個有3列的表格  
              PdfPTable table = new PdfPTable(3);  

2.2 表格的寬度和高度
       設定表格的寬度有兩種方法,分別如下
               table.setTotalWidth(float totalWidth); //設定表格的總寬度
               table.setTotalWidth(float[] columnWidth); //設定表格的各列寬度

       使用以上兩個函數,必須使用以下函數,將寬度鎖定。
               table.setLockedWidth(true);

       設定行的高度
               cell.setMinimumHeight(60);

代碼舉例
PdfPTable table = new PdfPTable(3);
table.setTotalWidth(300);
table.setLockedWidth(true);
table.setTotalWidth(new float[]{ 144, 72, 72 });
table.setLockedWidth(true);
2.3 添加儲存格
       把下面這9項順次的加入到表格中,當一行充滿時候自動折行到下一行  
            PdfPTable table = new PdfPTable(3);  
            table.addCell("1.1");  
            table.addCell("1.2");  
            table.addCell("1.3");  
            table.addCell("2.1");  
            table.addCell("2.2");  
            table.addCell("2.3");
以上程式運行結果將顯示三行二列的表格。

       添加儲存格的內容還可以是以下幾種形式。
            public void addCell(PdfPCell cell);
            public void addCell(PdfPTable table);
            public void addCell(Phrase phrase);
            public void addCell(String text);

2.3 合併儲存格
       iText合併儲存格的過程如下,首先建立一個cell,設定這個儲存格的跨度,
       如果是橫向合并,則通過
               cell.setColspan(n);  //n代表從目前的儲存格的位置開始,合并的儲存格數
       如果是縱向合并,
              cell.setRowspan(n);//n代表從目前的儲存格的位置開始,合并的儲存格數
代碼舉例
            //建立一個有3列的表格 
            PdfPTable table = new PdfPTable(3); 
            // 定義一個表格單元 
            PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3")); 
            // 定義一個表格單元的跨度 
            cell.setColspan(3); 
            // 把單元加到表格中 
            table.addCell(cell);
以上代碼建立一個表格,具有一行,一行本來有3列,結果經過合并,只有1列。

代碼舉例
import java.io.FileOutputStream;
import java.io.IOException;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class TableDemo2 {

    public static final String RESULT = "c:\\TableDemo2.pdf";
 
    public static void createPdf(String filename)
        throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        // step 3
        document.open();
        // step 4
        PdfPTable table = createTable1();
        document.add(table);
        table = createTable2();
        table.setSpacingBefore(5);
        table.setSpacingAfter(5);
        document.add(table);
        table = createTable3();
        document.add(table);
        table = createTable4();
        table.setSpacingBefore(5);
        table.setSpacingAfter(5);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.