Wed Jul 07, 2021 10:51 am
您好,
感谢您的咨询。
我做了初步测试确实注意到了这个问题的存在。非常抱歉给您造成的不便。不过我们另一款产品Spire.PDF也可以实现类似的功能,我建议您使用Spire.PDF并参考以下代码将DataTable绘制到PDF中。
- Code: Select all
var font = new PdfGridCellStyle() { Font = new PdfTrueTypeFont(new Font("宋体", 10f, FontStyle.Regular), true) };
DataTable dataTable = new DataTable();
dataTable.Columns.Add("姓名", typeof(string));
dataTable.Columns.Add("年龄", typeof(string));
dataTable.Columns.Add("籍贯", typeof(string));
dataTable.TableName="Table";
dataTable.Rows.Add(new object[] { "张三", 18, "北京" });
dataTable.Rows.Add(new object[] { "李四", 99, "成都" });
dataTable.Rows.Add(new object[] { "周五",15, "江西" });
dataTable.Rows.Add(new object[] { "小明", 78, "新疆" });
dataTable.Rows.Add(new object[] { "小华", 32, "内蒙古" });
dataTable.Rows.Add(new object[] { "小刚", 45, "黑龙江" });
PdfDocument pdf = new PdfDocument();
PdfPageBase page=pdf.Pages.Add();
PdfGrid grid = new PdfGrid();
grid.Columns.Add(3);
PdfGridRow row=grid.Rows.Add();
for(int i = 0; i < dataTable.Columns.Count; i++)
{
row.Cells[i].StringFormat.Alignment = PdfTextAlignment.Center;
row.Cells[i].Value = dataTable.Columns[i].ColumnName;
row.Cells[i].Style = font;
}
for(int i = 0; i < dataTable.Rows.Count; i++)
{
row = grid.Rows.Add();
DataRow dataRow = dataTable.Rows[i];
for(int j = 0; j < dataRow.ItemArray.Length; j++)
{
row.Cells[j].StringFormat.Alignment = PdfTextAlignment.Center;
row.Cells[j].Value = dataRow.ItemArray[j];
row.Cells[j].Style = font;
}
}
grid.Draw(page, new PointF(20, 20));
pdf.SaveToFile("output.pdf",FileFormat.PDF);
Sincerely,
Andy
E-iceblue support team