Hi everyone.
First I would like to apologize if I didn't post this correctly, so please don't take it hard on me if I did. Here is my problem: I have to print out a system filled form, including text styles. For example (I have already solved the styling issue) Name: John Surname: Doe... I have tried saving this to a .rtf document, but was never able to print out the results. Even when I tried to preview the page it showed blank. So my question is how can I print this stuff out or if there is another more simple way of doing it, please advise me. I have searched tons of web pages in google, but I keep getting the same result: Blank page

Thanks for your help

Dani AI

Generated

Nice catch, . For anyone landing here later: a blank PrintPreview usually means the PrintPage event is firing but nothing is being rendered (e.Graphics never receives content), or the page loop keeps resetting state so no characters are laid out.

If you only need a quick way to print an existing .rtf with formatting and do not need custom margins/pagination, let the registered RTF handler (e.g., WordPad) do the work:

using System.Diagnostics;

var psi = new ProcessStartInfo(rtfPath) {
    Verb = "print",
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden
};
Process.Start(psi);

For full control and to keep RTF styles when printing from a WinForms RichTextBox, render via the RichEdit control itself using EM_FORMATRANGE (not DrawString, which loses formatting). Outline:

  • Wire PrintDocument.BeginPrint/PrintPage/EndPrint. Keep a running firstCharIndex across pages; do not reset it in PrintPage.
  • In PrintPage, send EM_FORMATRANGE with wParam=1 and a FORMATRANGE that points to the printer HDC (from e.Graphics.GetHdc()).
  • Convert bounds to twips (1440 per inch). MarginBounds from .NET are in hundredths of an inch, so multiply by 14.4.
  • EM_FORMATRANGE returns the index of the last formatted character. Set e.HasMorePages = lastIndex < richTextBox.TextLength.
  • After finishing (EndPrint), send EM_FORMATRANGE with wParam=0/lParam=IntPtr.Zero to free cached info and release the HDC.

Common causes of a blank page: PrintPage not subscribed, forgetting GetHdc/ReleaseHdc, not converting to twips, not persisting the character index between pages, or always setting HasMorePages = false.

Note for ASP.NET readers: browsers do not natively render RTF. In web apps, generate HTML (with CSS for bold) and use window.print(), or produce a PDF and let the client print that. Avoid automating Office on the server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.