[itextsharp][004]建立基本的table(insert table到特定位置,設定欄位寬度)

[itextsharp][004]建立基本的table(insert table到特定位置,設定欄位寬度)

開始使用table之前,首先當然還是要宣告pdf文件的紙張大小:

Document doc = new Document(PageSize.A5);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("CreateBasicTable.pdf", FileMode.Create));
doc.Open();

宣告table變數的方式是利用PdfPTable類別,下例是示範最基本的table,每一列有兩個欄位,兩個欄位的大小都一樣大:

PdfPTable basicTable = new PdfPTable(2);

然後宣告PdfPCell物件作為table的儲存格,並直接加入到PdfPTable物件之中:

basicTable.AddCell(new PdfPCell(new Phrase("Create a table with all default settings")));
basicTable.AddCell(new PdfPCell(new Phrase("if the content is too long, cell will breakline by itself.")));
basicTable.AddCell(new PdfPCell(new Phrase("line2,cell1")));
basicTable.AddCell(new PdfPCell(new Phrase("line2,cell2")));

並且將此table加入到Paragraph物件中,以利於在pdf文件中排版:

Paragraph para = new Paragraph();
para.Add(basicTable);
para.Add("\n");

由於這個table每一列只有兩個儲存格,因此在上面加入四個儲存格的情況下,將會自動換行,產出的pdf如下:

如果要指定table每個欄位的大小的話,可以利用陣列的方式指定大小,如果table的每一列希望有三個儲存格,且第二個儲存格的寬度最寬的話,可以如下這樣做:

basicTable = new PdfPTable(new float[] { 1f, 2f, 1f });

然後一樣加入儲存格到此table之中,並且也一樣加入到Paragraph物件中以利在pdf文件中排版:

basicTable = new PdfPTable(new float[] { 1f, 2f, 1f });
basicTable.AddCell(new PdfPCell(new Phrase("Create table with unequal width columns ")));
basicTable.AddCell(new PdfPCell(new Phrase("line1,cell2: is the widest cell")));
basicTable.AddCell(new PdfPCell(new Phrase("line1,cell3")));
para.Add(basicTable);

執行結果可以發現第二個儲存格變寬了:

上列的範例是利用Paragraph物件幫忙把table在pdf文件中排版,就像是在word中加入表格一樣,也許加入幾個換行符號之後,再插入一個table。如果想要在pdf文件的某某個位置插入table的話,可以利用達成PdfPTable.WriteSelectedRows()達成,下面是簡單小範例:

PdfPTable tableAtBottom = new PdfPTable(1);
tableAtBottom.AddCell(new PdfPCell(new Phrase("insert this table at the bottom of the document")));
tableAtBottom.TotalWidth = 300f;
tableAtBottom.LockedWidth = true;
PdfContentByte cb = writer.DirectContent;
tableAtBottom.WriteSelectedRows(0, -1, 50, 50, cb);

上面程式碼將新增的table放在座標(x,y) = (50,50),要注意座標(0,0)是在文件的左下角喔!而在利用PdfPTable.WriteSelectedRows()指定table的絕對位置的時候,一定要先利用tableAtBottom.TotalWidth設定好table的寬度,而我習慣會再把table的寬度利用tableAtBottom.LockedWidth做鎖定,避免最後輸出時table的大小被文字撐大或是被文件的大小限制導致縮小了。

然後產出的pdf結果如下,table果然被insert到文件的底部了:

完整範例程式碼:

Document doc = new Document(PageSize.A5);
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("CreateBasicTable.pdf", FileMode.Create));
doc.Open();
			
PdfPTable basicTable = new PdfPTable(2);
basicTable.AddCell(new PdfPCell(new Phrase("Create a table with all default settings")));
basicTable.AddCell(new PdfPCell(new Phrase("if the content is too long, cell will breakline by itself.")));
basicTable.AddCell(new PdfPCell(new Phrase("line2,cell1")));
basicTable.AddCell(new PdfPCell(new Phrase("line2,cell2")));
Paragraph para = new Paragraph();
para.Add(basicTable);
para.Add("\n");

//Create table with unequal width columns 
basicTable = new PdfPTable(new float[] { 1f, 2f, 1f });
basicTable.AddCell(new PdfPCell(new Phrase("Create table with unequal width columns ")));
basicTable.AddCell(new PdfPCell(new Phrase("line1,cell2: is the widest cell")));
basicTable.AddCell(new PdfPCell(new Phrase("line1,cell3")));
para.Add(basicTable);

PdfPTable tableAtBottom = new PdfPTable(1);
tableAtBottom.AddCell(new PdfPCell(new Phrase("insert this table at the bottom of the document")));
tableAtBottom.TotalWidth = 300f;
tableAtBottom.LockedWidth = true;
PdfContentByte cb = writer.DirectContent;
tableAtBottom.WriteSelectedRows(0, -1, 50, 50, cb);

doc.Add(para);
doc.Close();

本文使用iTextSharp版本:5.5.8

iTextSharp 4.1.6測試結果:

實測結果,無須修改任何程式碼,可得到一樣的結果。

這篇大概是這樣。。。