是的,這是可能的。我甚至寫了一本關於它的書。
您可能想要使用PdfPTable
類。這個課程在chapter 4中解釋。例如,請參閱HeaderFooter1示例,其中我遍歷包含許多電影標題和這些電影放映的數據庫。結果是一個PDF格式的表格,其行數與行數對應的行數一樣多:header_footer_1.pdf
我看到您正在使用iTextSharp,因此您可能需要查看C# port of the examples。
創建PdfPTable
非常簡單。您只需創建該類的一個實例,然後向其添加PdfPCell
實例。這是還介紹了行跨度和列跨度的例子:
// a table with three columns
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.Colspan = 3;
table.AddCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.Rowspan = 2;
table.AddCell(cell);
// we add the four remaining cells with addCell()
table.AddCell("row 1; cell 1");
table.AddCell("row 1; cell 2");
table.AddCell("row 2; cell 1");
table.AddCell("row 2; cell 2");
在你的情況下,它足以創造一個PdfPTable
有3列,在短的代碼段完成,然後在所有的記錄中創建一個循環存儲過程的結果集。將每個字段的內容放在PdfPCell
中,並將該單元格添加到PdfPTable
。一旦循環完成,將PdfPTable
添加到Document
實例。
警告:有些人可能會認爲你的問題是一個「家庭作業」的問題。你的問題沒有顯示你的嘗試。通常人們希望你在發佈問題之前做一些努力。不要拍攝使者。我只是給你一個提醒,這樣如果你的問題被降低或關閉,你不會感到驚訝。