在Asp.Net中操作PDF – iTextSharp - 使用表格
轉自:http://www.cnblogs.com/CareySon/archive/2011/11/05/2237116.html
使用Asp.Net生成PDF最常用的元素應該是表格,表格可以幫助比如訂單或者發票類型的文檔更加格式化和美觀。本篇文章并不會深入探討表格,僅僅是提供一個使用iTextSharp生成表格的方法介紹
使用iTextSharp來操作表格是一件簡單的事,尤其是iTextSharp中表格元素的命名方式和HTML與CSS中非常類似。iTextSharp提供了多個類用于創建表格,為了不讓讀者產生混淆,這里我使用PdfPTable這個專門為在PDF中創建表格的類,下面代碼展示了如何創建一個表格并將其加入PDF中:
PdfPTable table = new PdfPTable(3);PdfPCell cell = new PdfPCell(new Phrase("Header spanning 3 columns"));
cell.Colspan = 3;
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);
table.AddCell("Col 1 Row 1");
table.AddCell("Col 2 Row 1");
table.AddCell("Col 3 Row 1");
table.AddCell("Col 1 Row 2");
table.AddCell("Col 2 Row 2");
table.AddCell("Col 3 Row 2");
doc.Add(table);</pre>
通過為pdfpTable的構造函數傳入整數3,pdfpTable被初始化為一個三列的表格.為pdfpTabled添加單元格有多種方式,第一個單元格是通過PdfPCell對象添加進去的,PdfPCell的構造函數接受一個Phrase對象作為參數,然后將Cell的colspan設置為3,這樣這個單元格占了整個一行.就像HTML中表格那樣,單元格的水平對齊方式使用了三個值中的一個(譯者:左對齊,居中,右對齊),這三個值我加在了注釋中。后面的單元格我都通過AddCell方法加入,最后文檔的效果如下:
下面代碼從數據庫抽取值,并將數據插入到iTextSharp生成的表格中,下面代碼還設置了一些表格的展現方式:
PdfPTable table = new PdfPTable(2);//actual width of table in points
table.TotalWidth = 216f;
//fix the absolute width of the table
table.LockedWidth = true;
//relative col widths in proportions - 1/3 and 2/3
float[] widths = new float[] { 1f, 2f };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
//leave a gap before and after the table
table.SpacingBefore = 20f;
table.SpacingAfter = 30f;
PdfPCell cell = new PdfPCell(new Phrase("Products"));
cell.Colspan = 2;
cell.Border = 0;
cell.HorizontalAlignment = 1;
table.AddCell(cell);
string connect = "Server=.\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;";
using (SqlConnection conn = new SqlConnection(connect))
{
string query = "SELECT ProductID, ProductName FROM Products";
SqlCommand cmd = new SqlCommand(query, conn);
try
{
conn.Open(); using (SqlDataReader rdr = cmd.ExecuteReader()) { while (rdr.Read()) { table.AddCell(rdr[0].ToString()); table.AddCell(rdr[1].ToString()); } }
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
doc.Add(table);
}</pre>
這個表格一開始被初始化為兩列的表格,然后設置了表格的固定寬度,然后對每一列設置相對寬度為別為整個表格的三分之一和三分之二。如果你想將寬度設置為5分之一和是5分之四,只需要將參數分別改為1f和4f.如果你想設置每列的絕對寬度,只需要將列寬度和表格的總寬度傳入,例如:float[] widths = new float[] { 100f, 116f };
通過設置表格的SpacingBefore和SpacingAfter屬性,可以分別設置表格頭部離上一個元素的距離以及表格結束離下一個元素的距離.在文檔中有幾個表格緊挨著時,這個功能尤其有效。如果不設置上述屬性,那表格之間的距離就像在word中一個回車的距離一樣,那會和針一樣細。接下來我們通過設置第一個單元格的邊框為0,colspan為列數,居中使其像表格的標題一樣。接下來就是我們用編程的方式將從SqlDataReader讀取到的數據動態的添加到單元格中最后加入表格:
接下來的代碼展示了格式化單元格的一些選項,正如你所見,iTextSharp的作者遵循CSS的命名規則來設置單元格的選項使格式化單元格更加容易(當然,我假設你了解CSS。。。):
PdfPTable table = new PdfPTable(3);table.AddCell("Cell 1");
PdfPCell cell = new PdfPCell(new Phrase("Cell 2", new Font(Font.HELVETICA, 8f, Font.NORMAL, Color.YELLOW)));
cell.BackgroundColor = new Color(0, 150, 0);
cell.BorderColor = new Color(255,242,0);
cell.Border = Rectangle.BOTTOM_BORDER | Rectangle.TOP_BORDER;
cell.BorderWidthBottom = 3f;
cell.BorderWidthTop = 3f;
cell.PaddingBottom = 10f;
cell.PaddingLeft = 20f;
cell.PaddingTop = 4f;
table.AddCell(cell);
table.AddCell("Cell 3");
doc.Add(table);</pre>
上面代碼中不難看出,通過設置colspan來讓一個單元格在水平上跨多行十分容易。那如果是在垂直上使單元格跨越多行呢?在HTML中,你可以使用Rowspan屬性,但是在iTextSharp中并沒有Rowspan屬性。所以達到這個目的的方法只有嵌套表格。下面代碼創建了一個四列的表格,右下的表格橫跨三列,豎跨三行。當然,這是表面看起來這樣,但實際上是通過在表格左下角的單元格中嵌套一個三行一列的子表格,我們將左下角嵌套子表格的單元格的padding全部設置為0使被嵌入的子表格占據了整個左下單元格:
PdfPTable table = new PdfPTable(4);table.TotalWidth = 400f;
table.LockedWidth = true;
PdfPCell header = new PdfPCell(new Phrase("Header"));
header.Colspan = 4;
table.AddCell(header);
table.AddCell("Cell 1");
table.AddCell("Cell 2");
table.AddCell("Cell 3");
table.AddCell("Cell 4");
PdfPTable nested = new PdfPTable(1);
nested.AddCell("Nested Row 1");
nested.AddCell("Nested Row 2");
nested.AddCell("Nested Row 3");
PdfPCell nesthousing = new PdfPCell(nested);
nesthousing.Padding = 0f;
table.AddCell(nesthousing);
PdfPCell bottom = new PdfPCell(new Phrase("bottom"));
bottom.Colspan = 3;
table.AddCell(bottom);
doc.Add(table);</pre>
最后,在這篇闡述使用表格的文章末尾,我們來看看如何將一個單元格中的文本進行旋轉:
PdfPTable table = new PdfPTable(3);table.TotalWidth = 144f;
table.LockedWidth = true;
table.HorizontalAlignment = 0;
PdfPCell left = new PdfPCell(new Paragraph("Rotated"));
left.Rotation = 90;
table.AddCell(left);
PdfPCell middle = new PdfPCell(new Paragraph("Rotated"));
middle.Rotation = -90;
table.AddCell(middle);
table.AddCell("Not Rotated");
doc.Add(table);</pre>
Rotation屬性必須設置成90的倍數,否則就會引發錯誤,middle單元格的Rotation在這里設置成-90和270效果一樣,這個度數默認是按逆時針算的:
實際上iTextSharp可以操作表格的功能非常強大,在未來的文章中我會更加詳細的闡述。于此同時,大家可以使用Visual Studio的智能感知和對象瀏覽器充分挖掘iTextSharp的潛力,并看看最終生成的結果如何.