Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

CSharpWriter

0.00/5 (No votes)
17 Dec 2020 1  
CSharpWriter is a RTF style Text writer control written by C#. Independent RichTextBox control and can save to XML file.
CSharpWriter is a RTF style Text writer control written by C#

Image 1

Background

Many people use standard RichTextBox control to handle RTF style text, but RichTextBox control has lots limited. For example, it cannot be used in NO-GUI application, RTF file format is hard to parse, hard to change party of document content by programming.

What is CSharpWriter

CSharpWriter (short name CSWriter) is a new RTF style document process engine without RichTextBox control. it is more powerful than RichTextBox. We hope it can replace OpenOffice in .NET World. It supports:

  1. RTF style document content like text color, back color, font.
  2. DOM, you can create document in memory without creating GUI control. So CSWriter can be used in console /ASP.NET application.
  3. Independent. CSWriter is independent to other components. It draws all content and GUI using system.drawing.graphics, handles mouse and keyboard events by itself. You can use it without technology limited.
  4. Easy to use. You can place a control on your Winform. It provides thousands OOP APIs.
  5. Lite weight control. CSWriter is implemented from System.Windows.Forms.UserControl. It runs in application UI thread, use less memory. You can catch any unhandled exception.
  6. Support XML format. The result file is more easy to parse than RTF format. Use DOM, you can create your owner file format.
  7. CSWriter is developing. If you have any idea about CSWriter, you can visit the site [https://github.com/dcsoft-yyf/CSharpWriter].

How to Use CSharpWriter in WinForm.NET

Just like other winform control, you can use CSWriter easily. In VS.NET IDE, open winform designer, at toolbox left side, choose "choose items..." . and select file DCSoft.CSharpWriter.dll, then you can see CSWriterControl on toolbox, just like the following:

Image 2

You can drag a CSWriterControl and a CSWriterCommandControler to the form, change its name as 'myEditControl' and 'myCommandControler'. Next, you can add some button/toolstripbutton or menu item on the form, select a button, at PropertyGrid, you can see a property item name "at myCommandControler's CommandName", just like the following:

Image 3

Click button at the end, then show a dialog like this:

Image 4

And you can select a command with binding to the button. Then you design a form like this:

Image 5

Open the source editor, at the form_load event source code, add the following code:

private void frmTextUseCommand_Load(object sender, EventArgs e)
{
    myEditControl.CommandControler = myCommandControler;
    myCommandControler.Start();
}

After do this, you can run application, click button or menu item to use CSWriter. Look, it is so easy.
CSWriter supports a lot of commands. You can open a file by code myEditControl.ExecuteCommand("FileOpen", true, null), save a file by code myEditControl.ExecuteCommand("FileSaveAs", true,null), insert a string at current position by code myEditControl.ExecuteCommand("InsertString",false,"Some text content") .

Those commands defined in WriterCommandModuleBrowse.cs, WriterCommandModuleEdit.cs, WriterCommandModuleFile.cs, WriterCommandModuleFormat.cs, WriterCommandModuleInsert.cs, WriterCommandModuleSecurity.cs, WriterCommandModuleTools.cs in directory "CSharpWriter\Commands". You can add your owner command in those files. For example, this code defines command "InsertString":

[WriterCommandDescription("InsertString")]
protected void InsertString(object sender, WriterCommandEventArgs args)
{
    if (args.Mode == WriterCommandEventMode.QueryState)
    {
        args.Enabled = args.DocumentControler != null
            && args.DocumentControler.CanInsertElementAtCurrentPosition(
            typeof(DomCharElement));
    }
    else if (args.Mode == WriterCommandEventMode.Invoke)
    {
        args.Result = 0;
        InsertStringCommandParameter parameter = null;
        if (args.Parameter is InsertStringCommandParameter)
        {
            parameter = (InsertStringCommandParameter)args.Parameter;
        }
        else
        {
            parameter = new InsertStringCommandParameter();
            if (args.Parameter != null)
            {
                parameter.Text = Convert.ToString(args.Parameter);
            }
        }
        if (args.ShowUI)
        {
            using (dlgInputString dlg = new dlgInputString())
            {
                dlg.InputText = parameter.Text;
                if (dlg.ShowDialog(args.EditorControl) == DialogResult.OK)
                {
                    parameter.Text = dlg.InputText;
                }
                else
                {
                    return;
                }
            }
        }
        if (string.IsNullOrEmpty(parameter.Text) == false)
        {
            args.Result = args.DocumentControler.InsertString(parameter.Text);
        }
    }
}

In this code, the "QueryState" party detects whether the command is Enable currently. System will call this party to set Button/MenuItem.Enable =true or false. The "Invoke" party is defined in the body of command.

File Format

Currently, CSWriter save load or save XML file format. In the future, it will support RTF, HTML(MHT), PDF, BMP, ODF(Open document format) file format and you can add your owner file format.

Deploy

CSWriter is a lite GUI Control component. All parts are included in single file "DCSoft.CSharpWriter.dll". Independent any third part component. It runs on MS.NET framework 2.0 SP2 or later. Notice, you must add SP2 because CSWriterControl use property "CanEnableIme" which is defined in SP2 (not defined in .NET2.0 without SP2).
When you need update CSWriter, overwrite DLL file , and re-compile your source code and finish.

Support

CSWriter provides technology support. If you have some question or idea, you can leave a message below or visit site[https://github.com/dcsoft-yyf/CSharpWriter].

Compatibility Warning

In this version, the width of whitespace is too small. In the future, the width of whitespace maybe increase, this will change document layout.

Route Map

This is the route map:

Image 6

  • For programmer, currently CSWriter supports WinForm.NET, Console, Windows Service. CSWriter providers a type CSWriterControl base System.Windows.Forms.UserControl, Paint document content by itself, handles mouse and keyboard event by itself, without any third part component.
  • COM-Interop, CSWriter will support COM-Interop using System.Runtime.InteropServices, it will support COM-base developing, such as VB6, VC6, PowerBuidler, Delphi7(XE) or host in IE as ActiveControl.
  • For Web programmer, in the future, CSWriter will support WEB developing, such as ASP.NET WebForm/MVC and ASP.NET Core. and also can work with JAVA/PHP/Node.js application. It will support web browser compatibility, etc. IE/Edge/Chrome/Safari/Firefox. One document in different web browsers, has the same view, same content layout and same print result.
  • For GUI Operator, CSWriter just like a MS Word style. You can type any char, support English, Arabic, Tibetan.You can change text font, color, backcolor, paragraph format. In the future, it will support table.
  • Performance. We are working hard to performance optimization. In the future, you can load document with 1000 pages completed no more than 10 seconds. Handle table with 1000 rows smooth.

Inside CSharpWriter

DOM

The core of CSWriter. Dom description any details of document. Every part of document has map to a class instance. Programmer can new or delete Dom elements, modify element's property, those can update document view, just like JavaScript update HTML view by modify HTML DOM. DOM also is expendable, in the future, it will add new type element class, support new behavior. Create an unlimited cswriter.
This is element implement tree in DOM:

Image 7

DOM defines a set of classes, just like HTML DOM, each type of class maps to a type content in document. For example, DomDocument class maps to the total document. It is the primary enterpoint for programming; DomImageElement maps to a image in document, DomCharElement maps to a character in document.
This is DOM element reference tree:

Image 8

CSWriter builds a DOM tree in memory, every document element instance is placed in the tree.[Document] is the single enterpoint to access DOM tree.You can insert/delete element in the tree, and the system can update view quickly, just like JavaScript access HTMLDom.

License

CSWriter uses LGPL-2.1 License. If you want to change License type and use it in business unlimited, you can contact me by leaving a comment below.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here