pavanred
pavanred

Reputation: 13793

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)

I tried using the @page property in my CSS but I cannot use it because of browser compatibility issue. So that's ruled out.

I found an alternative using the PrintDocument class and setting the page to landscape mode

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. I need to print a gridview and am not able to figure out how to do that. Any ideas?

Moreover, I found yet another alternative to print the gridview using PrintDocument class but this is rather laborious. 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.

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? Is there a simpler and better way of doing this?

Upvotes: 1

Views: 2654

Answers (1)

SeanCocteau
SeanCocteau

Reputation: 1876

I think you're mixing scenarios here. If you want to Print from the client-side (i.e. the person visiting your web page) then you will have to render out the 'printable' area through the browser such as HTML. Of course, as you'll find out HTML printing is pretty limited and only certain browsers implement the correct CSS printing rules.

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. 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.

Finally, the PrintDocument object will only run server-side, i.e. 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.

Upvotes: 1

Related Questions