ITextSharp Table使用

來源:互聯網
上載者:User

標籤:pad   布局   phrase   oat   多行   htm   alter   enter   使用   

原文:http://www.cnblogs.com/LifelongLearning/archive/2011/05/16/2048116.html

表格是我們在製作文檔時,經常使用的一個元素。對布局的控制非常精確。在ITextSharp中表格對象是下面兩個元素:

PdfTable,PdfCell

下面從ITextSharp In Action截取一段代碼:

 

從代碼中,可以看出,PdfTable的建構函式,傳入一個列數為參數,表示這個表格有多少列,往表格中加入PdfCell,如果加入的儲存格超過一行,自動會進行換行。儲存格中有一個setColspan函數(註:C#版本中,是屬性Colspan),用於設定一個儲存格跨多列。同樣,如果要跨越多行,也有一個屬性(C#)RolSpan,上面的示範代碼執行結果如下:

 

PdfTable對象有一個設定表格地區和每列寬度的函數,如下:

public void SetTotalWidth(float[] columnWidth);            public void SetWidthPercentage(float[] columnWidth, Rectangle pageSize);

屬性:HorizontalAlignment 設定表格的對齊

HeaderRows 表示第幾行作為表格頭

FooterRows  表示第幾行作為表格尾

SplitLate      表示儲存格是否跨頁顯示

SplitRows     表示行是否跨頁顯示

 

範例程式碼如下:

   1: public class PdfPTableDemo : TestBase 
   2:     { 
   3:         protected override void Opening(Document document, PdfWriter writer) 
   4:         { 
   5:             document.SetPageSize(PageSize.A4.Rotate()); 
   6:             base.Opening(document, writer); 
   7:         } 
   8:         protected override void WriteDocument(Document document, PdfWriter writer) 
   9:         { 
  10:             PdfPTable table = CreateTable(); 
  11:             //table.WidthPercentage = 80;//設定表格占的寬度,百分比 
  12:             //table.TotalWidth = 200;//設定表格占的寬度,單位點數 
  13:             //table.SetTotalWidth(); 
  14:             //table.SetWidthPercentage(); 
  15:             table.HorizontalAlignment = Element.ALIGN_LEFT; 
  16:             document.Add(table);
  17:  
  18:             table = CreateTable(); 
  19:             table.HorizontalAlignment = Element.ALIGN_RIGHT; 
  20:             table.SpacingBefore = (5); 
  21:             table.SpacingAfter = (5); 
  22:             Rectangle rect = new Rectangle(523, 770); 
  23:             table.SetWidthPercentage( 
  24:               new float[] { 50, 25, 25 }, rect); 
  25:             document.Add(table); 
  26:             table = CreateTable(); 
  27:             table.HorizontalAlignment = Element.ALIGN_CENTER; 
  28:             table.SetTotalWidth(new float[] { 144, 72, 72 }); 
  29:             table.LockedWidth = (true); 
  30:             document.Add(table); 
  31:             table = CreateTable(); 
  32:             table.SpacingBefore = (15); 
  33:             table.SpacingAfter = (15); 
  34:             document.Add(table);
  35:  
  36:             table = new PdfPTable(3); 
  37:             PdfPCell cell 
  38:   = new PdfPCell(new Phrase("表頭測試", Normal)); 
  39:             cell.BackgroundColor = (BaseColor.YELLOW); 
  40:             cell.HorizontalAlignment = (Element.ALIGN_CENTER); 
  41:             cell.Colspan = (7); 
  42:             table.AddCell(cell);
  43:  
  44:             cell 
  45:   = new PdfPCell(new Phrase("表尾測試", Normal)); 
  46:             cell.BackgroundColor = (BaseColor.YELLOW); 
  47:             cell.HorizontalAlignment = (Element.ALIGN_CENTER); 
  48:             cell.Colspan = (7); 
  49:             table.AddCell(cell);
  50:  
  51:             table.DefaultCell.BackgroundColor = (BaseColor.LIGHT_GRAY); 
  52:             for (int i = 0; i < 100; i++) 
  53:             { 
  54:                 table.AddCell("Location"); 
  55:                 table.AddCell("Time"); 
  56:                 table.AddCell("Run Length"); 
  57:                 table.AddCell("Title"); 
  58:                 table.AddCell("Year"); 
  59:                 table.AddCell("Directors"); 
  60:                 table.AddCell("Countries"); 
  61:             } 
  62:             table.DefaultCell.BackgroundColor = (null); 
  63:             table.HeaderRows = (2); 
  64:             table.FooterRows = (1); 
  65:             document.Add(table);
  66:  
  67:         }
  68:  
  69:         private PdfPTable CreateTable() 
  70:         { 
  71:             PdfPTable table = new PdfPTable(3); 
  72:             table.TableEvent = new AlternatingBackground(); 
  73:             //table.WidthPercentage = 80;//設定表格占的寬度,百分比 
  74:             //table.TotalWidth = 200;//設定表格占的寬度,單位點數 
  75:             //table.SetTotalWidth(); 
  76:             //table.SetWidthPercentage(); 
  77:             PdfPCell cell; 
  78:             cell = new PdfPCell(new Phrase("Cell with colspan 3")); 
  79:             cell.Colspan = (3); 
  80:             table.AddCell(cell); 
  81:             cell = new PdfPCell(new Phrase("Cell with rowspan 2")); 
  82:             cell.Rowspan = (2); 
  83:             table.AddCell(cell); 
  84:             table.AddCell("row 1; cell 1"); 
  85:             table.AddCell("row 1; cell 2"); 
  86:             table.AddCell("row 2; cell 1"); 
  87:             table.AddCell("row 2; cell 2");
  88:  
  89:             return table; 
  90:         } 
  91:     }
  92:  

 

補充一點:在設定儲存格的對齊時,應該選設定對齊,再來新增內容。

   1: cell = new PdfPCell(new Chunk("中國人民",Normal));
   2:                 cell.UseAscender = (true);
   3:                 cell.UseDescender = (true);
   4:                 cell.VerticalAlignment = Element.ALIGN_MIDDLE;
   5:                 cell.HorizontalAlignment = Element.ALIGN_CENTER;
   7:                 table.AddCell(cell);

 

ITextSharp Table使用 (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.