【发布时间】:2011-04-14 18:43:22
【问题描述】:
我通过 iTextSharp 从 DataTable 成功创建了一个 PDF 文档,但我无法获得所需格式的布局。
虽然不太可能,但 DataTable 具有数十列的潜力。想象一下下面的 DataTable - 每个数字代表一个页面可以容纳的区域:
|------------|
| 1 : 2 : 3 |
|------------|
| 4 : 5 : 6 |
|------------|
这应该按照我对这些部分进行编号的顺序导出为 6 页的 PDF 文档。相反,它目前生成为一个两页文档,每页上有 40 多列,字体非常小,几乎没有细节。
解释我想要什么的最简单方法是:当生成的 PDF 打印时,我希望它像一个非常宽的 Excel 表一样打印,而不是将所有内容压缩在一起。
我的代码如下:
public static void ExportAsPDF(DataTable Table, IList<string> Columns, string filename)
{
int ColumnCount = Table.Columns.Count;
int RowCount = Table.Rows.Count;
iTextSharp.text.Table BodyTable = new iTextSharp.text.Table(ColumnCount, RowCount);
BodyTable.AutoFillEmptyCells = true;
foreach (string s in Columns)
{
BodyTable.AddCell(s);
}
foreach (object o in from DataRow row in Table.Rows from o in row.ItemArray select o)
{
BodyTable.AddCell(o.ToString());
}
Document doc = new Document();
PdfWriter.GetInstance(doc, HttpContext.Current.Response.OutputStream);
doc.Open();
doc.Add(BodyTable);
doc.Close();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.pdf", filename));
HttpContext.Current.Response.End();
}
非常感谢所有输入和帮助。谢谢
【问题讨论】:
标签: c# asp.net itextsharp