简体   繁体   English

在横向模式下打印gridview

[英]print a gridview in landscape mode

I need to print a gridview in landscape mode and also need pagination (the header row to be printed on each page) 我需要以横向模式打印gridview,也需要分页(要在每页上打印标题行)

I tried using the @page property in my CSS but I cannot use it because of browser compatibility issue. 我尝试在CSS中使用@page属性,但由于浏览器兼容性问题,无法使用它。 So that's ruled out. 所以这是排除在外的。

I found an alternative using the PrintDocument class and setting the page to landscape mode 我找到了使用PrintDocument类并将页面设置为横向模式的替代方法

PrintDocument pd1 = new PrintDocument();
pd1.DefaultPageSettings.Landscape = true; //this works

Though, this worked and I am able to print the document in landscape mode. 虽然如此,但我可以在横向模式下打印文档。 PrintDocument class can print a stream but not rendered HTML markup. PrintDocument类可以打印流,但不能显示HTML标记。 I need to print a gridview and am not able to figure out how to do that. 我需要打印一个gridview,但无法弄清楚该怎么做。 Any ideas? 有任何想法吗?

Moreover, I found yet another alternative to print the gridview using PrintDocument class but this is rather laborious. 此外,我发现了使用PrintDocument类打印gridview的另一种方法,但这很费力。 I loop through the gridview data and use the RectangleF class of System.Drawing and manually draw rectangles and text for each cell in the gridview and manage the rectangle dimensions and x / y offsets to draw the entire table. 我遍历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));
}

This works but obviously, this is laborious. 这行得通,但是显然,这很麻烦。 But this also gives me problems as I need to paginate data manually and redraw the header row on each page. 但这也给我带来了麻烦,因为我需要手动对数据进行分页并在每页上重新绘制标题行。 And, for pagination I will need the printer page size and printer resolution which I obviously cannot obtain. 而且,对于分页,我将需要我显然无法获得的打印机页面大小和打印机分辨率。

Any ideas? 有任何想法吗? How can I print a gridview in landscape and with pagination with header row repeated in each page? 如何在横向和分页中每页重复标题行的情况下打印gridview? Is there a simpler and better way of doing this? 有没有更简单更好的方法?

I think you're mixing scenarios here. 我认为您在这里混合场景。 If you want to Print from the client-side (ie the person visiting your web page) then you will have to render out the 'printable' area through the browser such as HTML. 如果要从客户端(即访问您网页的人)进行打印,则必须通过诸如HTML之类的浏览器呈现“可打印”区域。 Of course, as you'll find out HTML printing is pretty limited and only certain browsers implement the correct CSS printing rules. 当然,您会发现HTML打印非常有限,只有某些浏览器才能实现正确的CSS打印规则。

Most developers get around this issue by dynamically creating and redirecting the user to a PDF document that, as a plugin, is pretty ubitious nowadays. 大多数开发人员通过动态创建用户并将其重定向到作为插件的PDF文档来解决这个问题,而该PDF文档如今已相当庞大。 One word of warning, you mention the 'laborious' drawing / rendering, well you should expect pretty much the same coding experience. 一句话警告,您提到了“费力的”绘图/渲染,那么您应该期待几乎相同的编码体验。

Check out this article for a open-source PDF creation toolkit , however, you might find a commercial one easier to use. 查看本文,了解开放源代码PDF创建工具包 ,但是,您可能会发现更易于使用的商业版本。

Finally, the PrintDocument object will only run server-side, ie in your ASP.NET code and will only print on the machine / server that is executing that code - it will not print on the user's printer. 最后,PrintDocument对象将仅在服务器端运行,即在您的ASP.NET代码中运行,并且只会在执行该代码的机器/服务器上打印-不会在用户的打印机上打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM