If you wish to apply specific formatting for printing the contents of the RichTextBox Edit pane, you can use the CSS2 @media rule which allows you to implement media specific styles. For a more detailed article on all the media types available and implementation syntax the following resource is most useful –
http://www.meyerweb.com/eric/articles/webrev/200001.html
This article will focus on the print and screen media types using the @media rule rather than Link or Import syntaxes as the @media rule is the most appropriate with the RichTextBox.
I’ve placed the CSS rules below in a file called MediaRules.css (located in the root of the Web application) and set the EditorWysiwygModeCss property to this file in the following code –
MediaRules.css -
@media screen
{
h2 {color:blue; font-family:tahoma; font-size=12pt}
body {font-size: 14px; font-family:arial}
}
@media print
{
h2 {color:black; text-transform:capitalize; font-
family:arial; font-style:italic; font-size=12pt}
body {font-size: 12pt; font-family:tahoma}
}
.aspx.cs -
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
string myHtml = RichTextBox1.TextXhtml;
}
else
{
RichTextBox1.EditorWysiwygModeCss = "MediaRules.css";
}
}
The result is that printing from the RichTextBox Print toolbar button will apply the @media print styles which differ from the screen view. To enhance the functionality I’d also add a toolbar button to the RichTextBox or button on my .aspx page that provides View Print Screen/Layout functionality. When this is clicked, simply set the EditorWysiwygModeCss property to a style sheet that applies a @media screen rule that is the same as the @media print rule and then switch it back when the user clicks to finish the View Print Layout.
An interesting addition to this would be the use of the Style Sheet Attributes page-break-after, page-break-before like the following –
Br.pageEnd {display:block; page-break-after: always}
Then to force a page break in the document use the tag –
<br class=”pageEnd”>
Such Page Breaks are not rendered in the visual browser so you may need to insert a visual reference if the user needs to be able to ‘see’ the Page Break locations.
We hope that this article will be helpful with your development of print functionality using the RichTextBox.