New to Telerik UI for WinForms? Download free 30-day trial

Print Silently Documents with RadRichTextEditor

Environment

Product Version Product Author
2021.1.223 RadRichTextEditor for WinForms Desislava Yordanova

Description

This tutorial demonstrates how to print silently different document types, e.g. .docx, .html, etc.

The required Assembly References are:

  • System.Windows.Forms (for Console applications)
  • Telerik.WinControls
  • Telerik.WinControls.RichTextEditor
  • Telerik.WinControls.UI
  • TelerikCommon

Solution

RadRichTextEditor supports importing the content of different document types like DOCX, XAML, HTML, RTF, Plain text. To import a document you have to use a specific class that implements the IDocumentFormatProvider. The supported document providers in RadRichTextEditor are listed here. Once a document is imported in RadRichTextEditor, you can use its printing functionality.

Silent Printing of Word and HTML documents

namespace ConsoleApplicationSilentPrintRTE
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintWordDocument(@"..\..\Sample.docx");
            PrintHtmlDocument(@"..\..\Sample.html");
        }

        private static void PrintHtmlDocument(string fileName)
        {
            Telerik.WinControls.UI.RadRichTextEditor radRichTextEditor1 = new RadRichTextEditor();
            Telerik.WinForms.Documents.FormatProviders.Html.HtmlFormatProvider provider = new HtmlFormatProvider();
            using (System.IO.FileStream inputStream = File.OpenRead(fileName))
            {
                radRichTextEditor1.Document = provider.Import(inputStream);
            }
            radRichTextEditor1.LoadElementTree();
            radRichTextEditor1.Print();
        }

        private static void PrintWordDocument(string fileName)
        {
            Telerik.WinControls.UI.RadRichTextEditor radRichTextEditor1 = new RadRichTextEditor();
            Telerik.WinForms.Documents.FormatProviders.OpenXml.Docx.DocxFormatProvider provider = new DocxFormatProvider();
            using (System.IO.FileStream inputStream = File.OpenRead(fileName))
            {
                radRichTextEditor1.Document = provider.Import(inputStream);
            }
            radRichTextEditor1.LoadElementTree();
            radRichTextEditor1.Print();
        }
    }
}

The above approach can be followed with the rest of the document providers that RadRichTextEditor offers.