In the project, use TX Text Control to edit and display Word documents.
In many functional modules of document management, we often need to display and edit WORD stability. If Microsoft word control is used for processing, we need to install the WORD component, in addition, the interface is not easy to use. Therefore, if a third party does not need to install Office to display WORD and edit it, it is a good choice. TX Text Control is such a Control, this article is based on the use of this control to implement the application in the Document Management Project.
1. Introduction and use of TX Text Control
TX Text Control is a function similar to MS Word Text processing controls, including document creation, editing, printing, mail merge, format conversion, split merge, Import Export, batch generation and other functions. It is widely used in enterprise document management, website content publishing, creation of medical record templates in electronic medical records, writing of medical records, modification history, continuous printing, and archiving of medical records. The main function of this control is to use it as an editor for Word and other documents. The overall display effect is very close to that of native WORD. It is a very good control suite.
In my earlier article "use of the Text Processing Control TX Text Control", I briefly introduced the use of this Control, in addition, I have compiled and used all the English resources of this version. Basically, all the functions are displayed in normal Chinese, which is more suitable for our actual project.
We add the corresponding controls in the VS toolbar. You can see that the following control objects are available. In general, TextControl is used, then, you can create other RulerBar, ButtonBar, and StatusBar Based on it. If you need to merge data (frequently used), you need to add the MailMerge control object.
After adding a widget, you can directly use the widget's API to copy, paste, and cut controls.
The main purpose here is to integrate the use of this control in the WInform project, as described in the previous article.
The following figure shows the effect of dragging related controls to custom controls.
The class code is as follows:
/// <Summary> /// custom control encapsulation Based on TxTextControl /// </summary> public partial class TextWordControl: XtraUserControl
In addition, the function buttons can be customized to add or remove some buttons, as shown in the following list.
2. Code implementation and use of the custom Control of TX Text Control
To implement some key operations of the WORD editing control, such as selecting all (Ctrl + A), searching (Ctrl + F), and replacing (Ctrl + H, we need to implement the related button event processing in the control code, so that we can use these buttons to directly call the built-in Processing Form of the control. The specific code is as follows.
/// <Summary> /// custom control encapsulation Based on TxTextControl /// </summary> public partial class TextWordControl: XtraUserControl {public TextWordControl () {InitializeComponent (); initEvent () ;}/// <summary> // process the control event /// </summary> private void InitEvent () {Thread. currentThread. currentUICulture = new System. globalization. cultureInfo ("zh-CN"); Thread. currentThread. currentCulture = new System. globalization. cu LtureInfo ("zh-CN"); this. textControl1.KeyDown + = textControl1_KeyDown;} void textControl1_KeyDown (object sender, KeyEventArgs e) {HandleKeyDownEvent (e);} private void HandleKeyDownEvent (KeyEventArgs e) {switch (e. keyCode) {case Keys. insert: if (e. control | e. alt | e. shift) break; // Insert the button operation textControl1.InsertionMode = textControl1.InsertionMode = TXTextControl. insertionMode. insert? TXTextControl. InsertionMode. Overwrite: TXTextControl. InsertionMode. Insert; break; case Keys. A: if (! E. Control | e. Alt | e. Shift) break; // Ctrl-A: select all operations textControl1.SelectAll (); break; case Keys. F: if (! E. Control | e. Alt | e. Shift) break; // Ctrl-F: Query operation textControl1.Find (); break; case Keys. H: if (! E. Control | e. Alt | e. Shift) break; // Ctrl-H: replace textControl1.Replace (); break ;}}
The built-in dialog box for searching and replacing is shown in the following figure.
Because the word document data is stored in the database, binary storage is generally used. Therefore, you need to set the properties of a custom control to obtain the data of the control, the Code is as follows.
/// <Summary> /// document data /// </summary> [Browsable (false)] public byte [] DocData {get {byte [] docData = null; textControl1.Save (out docData, TXTextControl. binaryStreamType. MSWord); return docData;} set {if (value! = Null) {try {textControl1.Load (value, TXTextControl. binaryStreamType. MSWord);} catch (Exception ex) {LogHelper. error (ex); MessageDxUtil. showError (ex. message );}}}}
Then the interface function is provided to load the data of local Word documents. The specific code is as follows.
public void LoadData(string file) { if (!string.IsNullOrEmpty(file)) { try { this.textControl1.Load(file, TXTextControl.StreamType.MSWord); } catch (Exception ex) { LogHelper.Error(ex); MessageDxUtil.ShowError(ex.Message); } } }
Then, in the document management function module, load and save the document content, as shown in the following figure.
The interface for editing Word documents shows the following results. We can see that the Word format and content displayed in this control are not the same as those displayed in the wordsoftware, which is very good and fast, this is much better than the RichTextEdit control of DevExpress.
The above is a summary of how to edit and display a Word document using TX Text Control. We hope to provide you with inspiration and help.