【发布时间】:2012-02-22 11:57:46
【问题描述】:
我需要在横向模式下打印一个gridview,还需要分页(每页上要打印的标题行)
我尝试在我的 CSS 中使用 @page 属性,但由于浏览器兼容性问题,我无法使用它。所以排除了。
我找到了使用PrintDocument 类并将页面设置为横向模式的替代方法
PrintDocument pd1 = new PrintDocument();
pd1.DefaultPageSettings.Landscape = true; //this works
不过,这行得通,我能够以横向模式打印文档。 PrintDocument 类可以打印流但不能渲染 HTML 标记。我需要打印一个gridview,但无法弄清楚如何做到这一点。有什么想法吗?
此外,我找到了另一种使用PrintDocument 类打印gridview 的替代方法,但这相当费力。我遍历 gridview 数据并使用 System.Drawing 的 RectangleF 类并为 gridview 中的每个单元格手动绘制矩形和文本,并管理矩形尺寸和 x / y 偏移量以绘制整个表格。
PrintDocument pd1 = new PrintDocument();
pd1.DefaultPageSettings.Landscape = true;
pd1.PrintPage += printer_PrintPage;
pd1.Print();
//Draw a rectangle and fill text in it.
//I Change values of xoffset, yoffset accordingly to draw a row one cell after other
protected void printer_PrintPage(object sender, PrintPageEventArgs e)
{
var rectF1 = new RectangleF(xoffset, yoffset, cellWidth, cellHeight);
e.Graphics.DrawString(propertyinfo.GetValue(obj, null).ToString(), font1, Brushes.Black, rectF1);
e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(rectF1));
}
这行得通,但显然,这很费力。但这也给我带来了问题,因为我需要手动对数据进行分页并重绘每页上的标题行。而且,对于分页,我将需要我显然无法获得的打印机页面大小和打印机分辨率。
有什么想法吗?如何在横向打印gridview并在每页中重复标题行的分页?有没有更简单更好的方法来做到这一点?
【问题讨论】:
标签: c# asp.net gridview printing