[itextsharp][007]table操作進階(colspan,rowspan, cell align left or right or center, padding, border)
在看完這篇[itextsharp][004]建立基本的table(insert table到特定位置,設定欄位寬度)瞭解基本的TABLE操作之後,在這篇要瞭解進階一點的TABLE操作,就是ROWSPAN, COLSPAN, ALIGN LEFT OR CENTER OR RIGHT,padding,border。
首先利用PdfPCell.Colspan屬性可以達成TABLE的COLUMN SPAN的效果,同理應用在Rowspan上面:
PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Paragraph("colspan span of this cell is 2 "));
cell1.Colspan = 2;
table.AddCell(cell1);
然後可利用PdfPCell.HorizontalAlignment屬性以及PdfPCell.VerticalAlignment屬性來設定CELL的align left or center or right,注意很重要的一點就是,如果使用了rowspan or colspan屬性的話,一定要在table最後的操作加上PdfPTable.CompleteRow(),來表示已完成此table:
PdfPCell cell2 = new PdfPCell(new Paragraph("text align center by HorizontalAlignment "));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("text align bottom by VerticalAlignment"));
cell3.VerticalAlignment = Element.ALIGN_BOTTOM;
table.AddCell(cell3);
//這行最重要,如果有rowspan, colspan的情況下,一定要加上這個表示完成table
table.CompleteRow();
執行結果:
然後繼續介紹cell padding 以及 cell border。
利用PdfPCell.PaddingLeft屬性即可設定該cell的左邊的padding,同理可應用在類似的其他屬性:.Padding, .PaddingBottom, PaddingRight, PaddingTop
PdfPTable paddingTable = new PdfPTable(1);
PdfPCell paddingCell = new PdfPCell(new Paragraph("test cell padding = 40 on left side"));
paddingCell.PaddingLeft = 40;
paddingTable.AddCell(paddingCell);
然後介紹border:
要設定table是否有框線並非使用PdfPTable的屬性,而是使用PdfPCell.Border屬性,此屬性可以設定cell有沒有框線、框線在上or下or左or右;也可以利用屬性PdfPCell.BorderWidth設定框線寬度。
PdfPTable borderTable = new PdfPTable(1);
PdfPCell borderCell1 = new PdfPCell(new Paragraph("this is a no border cell"));
borderCell1.Border = PdfPCell.NO_BORDER;
borderTable.AddCell(borderCell1);
PdfPCell borderCell2 = new PdfPCell(new Paragraph("this cell has only border on its bottom"));
borderCell2.Border = PdfPCell.BOTTOM_BORDER;
borderTable.AddCell(borderCell2);
PdfPCell borderCell3 = new PdfPCell(new Paragraph("set border width on its right"));
borderCell3.Border = PdfPCell.RIGHT_BORDER;
borderCell3.BorderWidth = 5;
borderTable.AddCell(borderCell3);
完整程式碼:
Document doc = new Document(PageSize.A5);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("tableRowSpanAlign.pdf", FileMode.Create));
doc.Open();
Paragraph para = new Paragraph();
PdfPTable table = new PdfPTable(2);
PdfPCell cell1 = new PdfPCell(new Paragraph("colspan span of this cell is 2 "));
cell1.Colspan = 2;
table.AddCell(cell1);
PdfPCell cell2 = new PdfPCell(new Paragraph("text align center by HorizontalAlignment "));
cell2.HorizontalAlignment = Element.ALIGN_CENTER;
table.AddCell(cell2);
PdfPCell cell3 = new PdfPCell(new Paragraph("text align bottom by VerticalAlignment"));
cell3.VerticalAlignment = Element.ALIGN_BOTTOM;
table.AddCell(cell3);
//這行最重要,如果有rowspan, colspan的情況下,一定要加上這個表示完成table
table.CompleteRow();
PdfPTable paddingTable = new PdfPTable(1);
PdfPCell paddingCell = new PdfPCell(new Paragraph("test cell padding = 40 on left side"));
paddingCell.PaddingLeft = 40;
paddingTable.AddCell(paddingCell);
PdfPTable borderTable = new PdfPTable(1);
PdfPCell borderCell1 = new PdfPCell(new Paragraph("this is a no border cell"));
borderCell1.Border = PdfPCell.NO_BORDER;
borderTable.AddCell(borderCell1);
PdfPCell borderCell2 = new PdfPCell(new Paragraph("this cell has only border on its bottom"));
borderCell2.Border = PdfPCell.BOTTOM_BORDER;
borderTable.AddCell(borderCell2);
PdfPCell borderCell3 = new PdfPCell(new Paragraph("set border width on its right"));
borderCell3.Border = PdfPCell.RIGHT_BORDER;
borderCell3.BorderWidth = 5;
borderTable.AddCell(borderCell3);
para.Add("Set Colspan:");
para.Add(table);
para.Add("Set cell Padding:\n");
para.Add(paddingTable);
para.Add("set cell border:\n");
para.Add(borderTable);
doc.Add(para);
doc.Close();
本文使用iTextSharp版本:5.5.8
iTextSharp 4.1.6測試結果:
實測結果,無須修改任何程式碼,可得到一樣的結果。
這篇大概是這樣。。。