Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

C#: Web browser control print line break

, 16 Jan 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
I was updating an old project for a client today. I needed to print a simple list and figured that the easiest way should be to format the data in HTML. Most developers out there has built webpages now days and we all know that it is a quick way to format the information and […]

I was updating an old project for a client today. I needed to print a simple list and figured that the easiest way should be to format the data in HTML. Most developers out there has built webpages now days and we all know that it is a quick way to format the information and print it. So after a quick creation of the HTML i found an article named "Displaying custom HTML in WebBrowser control" by Gunnar Peipman. So easy enough to print the custom HTML from the control, based on Gunnar's article I came up with this code:

WebBrowser webPrint = new WebBrowser();
webPrint.Navigate("about:blank");
if (webPrint.Document != null)
{
webPrint.Document.Write(string.Empty);
}
webPrint.DocumentText = printHTML.ToString();
webPrint.DocumentCompleted += webPrint_DocumentCompleted;
void webPrint_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser webPrint = (WebBrowser)sender;
webPrint.ShowPrintDialog();
}

If you don't use the "DocumentComplete" event handler your printout will be blank. In the print I used a basic HTML table to show the data with rows like this:

<table cellpadding="3" style="width:100%">
<tr style="color:White;background-color:Black;font-weight:bold;"><td>Box ID</td><td>Gång</td><td>Hylla</td><td>Nivå</td><td>Nåbar från golvet</td></tr>
<tr><td>10346</td><td>AB</td><td>3</td><td>F</td><td>True</td></tr>
<tr><td>10294</td><td>AB</td><td>3</td><td>D</td><td>True</td></tr>
<tr><td>10296</td><td>AB</td><td>3</td><td>C</td><td>True</td></tr>
<tr><td>10298</td><td>AB</td><td>3</td><td>C</td><td>True</td></tr>
<tr><td>10300</td><td>AB</td><td>3</td><td>B</td><td>True</td></tr>
<tr><td>10302</td><td>AB</td><td>3</td><td>B</td><td>True</td></tr>

When printing data that took up several pages I ended up with this:

pagebreak error

The data was cut of at the line breaks. I Googled a lot and found a few suggestions about css "page-break.." but they didn't have any effect on the web browser control print layout. Finally I found a simple solution, just add a bland column to the left of the table and the line break works. Like this:

<table cellpadding="3" style="width:100%">
<tr style="color:White;background-color:Black;font-weight:bold;"><td> </td><td>Box ID</td><td>Gång</td><td>Hylla</td><td>Nivå</td><td>Nåbar från golvet</td></tr>
<tr><td> </td><td>10346</td><td>AB</td><td>3</td><td>F</td><td>True</td></tr>
<tr><td> </td><td>10294</td><td>AB</td><td>3</td><td>D</td><td>True</td></tr>
<tr><td> </td><td>10296</td><td>AB</td><td>3</td><td>C</td><td>True</td></tr>
<tr><td> </td><td>10298</td><td>AB</td><td>3</td><td>C</td><td>True</td></tr>
<tr><td> </td><td>10300</td><td>AB</td><td>3</td><td>B</td><td>True</td></tr>
<tr><td> </td><td>10302</td><td>AB</td><td>3</td><td>B</td><td>True</td></tr>
<tr><td> </td><td>10382</td><td>AB</td><td>4</td><td>F</td><td>True</td></tr>

Then the printout looked like this:

pagebreak fix

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kristofer Kallsbo
CEO Kallsbo Consulting
Sweden Sweden
I develop in C# on .Net platforms like MVC. Like to use jQuery to build rich interfaces. I also blog about development and snags I got and the solutions I found for them.
 
I also a full time CIO at a Swedish energy company. When there is time I do some part time consulting on cloud issues.
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 16 Jan 2014
Article Copyright 2014 by Kristofer Kallsbo
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid