What library to use to create a document editor?

vitaminchik 446 Reputation points
2023-01-14T19:32:02.7633333+00:00

Good afternoon. What library should I use to create a document editor on C# and WPF? Is there a library to create an editor similar to MS word? Where will the A4 sheets be located (I'm interested in splitting the table into several pages, I don't quite understand how this can be done).

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,671 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,249 questions
0 comments No comments
{count} votes

Accepted answer
  1. Hui Liu-MSFT 38,251 Reputation points Microsoft Vendor
    2023-01-16T09:57:15.0066667+00:00

    You could try the following code.

    <Window.CommandBindings>
            <CommandBinding Command="ApplicationCommands.Open" Executed="Open_Executed" />
            <CommandBinding Command="ApplicationCommands.Save" Executed="Save_Executed" />
        </Window.CommandBindings>
        <DockPanel>
            <ToolBar DockPanel.Dock="Top">
                <Button Command="ApplicationCommands.Open">
                    <Image Source="C:\...\WpfApp1\folder.png" Width="16" Height="16" />
                </Button>
                <Button Command="ApplicationCommands.Save">
                    <Image Source="C:\...\WpfApp1\save.png" Width="16" Height="16" />
                </Button>
                <Separator />
                <ToggleButton Command="EditingCommands.ToggleBold" Name="btnBold">
                    <Image Source="C:\...\WpfApp1\blod.png" Width="16" Height="16" />
                </ToggleButton>
                <ToggleButton Command="EditingCommands.ToggleItalic" Name="btnItalic">
                    <Image Source="C:\...\WpfApp1\italic.png" Width="16" Height="16" />
                </ToggleButton>
                <ToggleButton Command="EditingCommands.ToggleUnderline" Name="btnUnderline">
                    <Image Source="C:\...\WpfApp1\underline.png" Width="16" Height="16" />
                </ToggleButton>
                <Separator />
    <ToggleButton Click="btnPrint_Click"  Name="btnPrint">
                    <Image Source="C:\...\WpfApp1\printer.png" Width="24" Height="18" />
                </ToggleButton>
                <Separator />
                <ComboBox Name="cmbFontFamily" Width="150" SelectionChanged="cmbFontFamily_SelectionChanged" />
                <ComboBox Name="cmbFontSize" Width="50" IsEditable="True" TextBoxBase.TextChanged="cmbFontSize_TextChanged" />
            </ToolBar>
            <RichTextBox Name="rtbEditor" SelectionChanged="rtbEditor_SelectionChanged" />
        </DockPanel>
    
    
    using System;
    using System.Linq;
    using System.Collections.Generic;
    using System.IO;
    using System.Windows;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using Microsoft.Win32;
    using System.Windows.Controls;
    
    namespace WpfApp1
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                cmbFontFamily.ItemsSource = Fonts.SystemFontFamilies.OrderBy(f => f.Source);
                cmbFontSize.ItemsSource = new List<double>() { 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72 };
            }
    
            private void rtbEditor_SelectionChanged(object sender, RoutedEventArgs e)
            {
                object temp = rtbEditor.Selection.GetPropertyValue(Inline.FontWeightProperty);
                btnBold.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(FontWeights.Bold));
                temp = rtbEditor.Selection.GetPropertyValue(Inline.FontStyleProperty);
                btnItalic.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(FontStyles.Italic));
                temp = rtbEditor.Selection.GetPropertyValue(Inline.TextDecorationsProperty);
                btnUnderline.IsChecked = (temp != DependencyProperty.UnsetValue) && (temp.Equals(TextDecorations.Underline));
    
                temp = rtbEditor.Selection.GetPropertyValue(Inline.FontFamilyProperty);
                cmbFontFamily.SelectedItem = temp;
                temp = rtbEditor.Selection.GetPropertyValue(Inline.FontSizeProperty);
                cmbFontSize.Text = temp.ToString();
            }
    
            private void Open_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|All files (*.*)|*.*";
                if (dlg.ShowDialog() == true)
                {
                    FileStream fileStream = new FileStream(dlg.FileName, FileMode.Open);
                    TextRange range = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd);
                    range.Load(fileStream, DataFormats.Rtf);
                }
            }
    
            private void Save_Executed(object sender, ExecutedRoutedEventArgs e)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Filter = "Rich Text Format (*.rtf)|*.rtf|All files (*.*)|*.*";
                if (dlg.ShowDialog() == true)
                {
                    FileStream fileStream = new FileStream(dlg.FileName, FileMode.Create);
                    TextRange range = new TextRange(rtbEditor.Document.ContentStart, rtbEditor.Document.ContentEnd);
                    range.Save(fileStream, DataFormats.Rtf);
                }
            }
    
            private void cmbFontFamily_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (cmbFontFamily.SelectedItem != null)
                    rtbEditor.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, cmbFontFamily.SelectedItem);
            }
    
            private void cmbFontSize_TextChanged(object sender, TextChangedEventArgs e)
            {
                rtbEditor.Selection.ApplyPropertyValue(Inline.FontSizeProperty, cmbFontSize.Text);
            }
     private void btnPrint_Click(object sender, RoutedEventArgs e)
            {
               
                    PrintCommand();
                
    
            }
            private void PrintCommand()
            {
                PrintDialog pd = new PrintDialog();
                if ((pd.ShowDialog() == true))
                {
                    pd.PrintVisual(rtbEditor as Visual, "printing as visual");
                    pd.PrintDocument((((IDocumentPaginatorSource)rtbEditor.Document).DocumentPaginator), "printing as paginator");
                }
            }
        }
    }
    
    

    The result:

    User's image

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Lex Li (Microsoft) 4,742 Reputation points Microsoft Employee
    2023-01-15T06:09:58.81+00:00

    All major third party vendors sell their rich text editor controls with the features that you might want. You can play with their sample projects and talk to their presales team. Then you can decide which to purchase.

    For example, you can find one from Telerik.

    Search engines can give you more options.

    0 comments No comments