我是iTextSharp的新手,並且想將我的DataGridView轉換爲PdfPtable。iTextSharp PdfPtable多頁
當我將表格寫入PDF時,它顯示得很好,除非列數量相對較大。然後通過使列寬變小來適應。
相反,我想從新頁面開始,顯示更多的表格和標題。
下面是我的表碼:
PdfPTable table = new PdfPTable(view.Columns.Count);
float[] widths = new float[view.Columns.Count];
for (int i = 0; i < view.Columns.Count; i++) {
widths[i] = view.Columns[i].Width;
}
table.SetWidths(widths);
table.HorizontalAlignment = 1; //Center
PdfPCell cell = null;
//Headers
foreach (DataGridViewColumn c in view.Columns) {
cell = new PdfPCell(new Phrase(new Chunk(c.HeaderText, _standardFont)));
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
}
//Rest of table
for (int i = 1; i < view.Rows.Count; i++) {
for (int j = 0; j < view.Columns.Count; j++) {
bool hasValue = view.Rows[i].Cells[j].Value == null ? false : true;
if (hasValue) {
cell = new PdfPCell(new Phrase(view.Rows[i].Cells[j].Value.ToString(), _standardFont));
} else {
cell = new PdfPCell(new Phrase("", _standardFont));
}
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
table.AddCell(cell);
}
}
一個解決辦法是莫名其妙地知道有多少列,可以在頁面上打印的全尺寸,然後添加一個新的頁面,更顯示網格那裏,但我不確定如何檢測新頁面。這裏的問題是,似乎沒有辦法改變PdfPtable的列數,所以當預先添加列時,我必須能夠計算寬度的效果。
編輯:我要拆分的頁面垂直
當行不符合頁面的可用高度時,iText會自動分割表格。你是說你想要垂直分割桌子嗎? –
是的,我應該更清楚在我的問題描述:) – User