New to Telerik UI for WinFormsStart a free 30-day trial

Print Silently Documents with RadRichTextEditor

Updated over 6 months ago

Environment

Product VersionProductAuthor
2021.1.223RadRichTextEditor for WinFormsDesislava 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

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

See Also