Use openxml to add text to Word documents. Each module has its own attributes and content. To set a style, declare the attribute object and append the style to the attribute, append the attribute to the module, and the content in the module will have the style. This method appends content to the end of the file by default.
Code:
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. threading. tasks; using documentformat. openxml; using documentformat. openxml. packaging; using documentformat. openxml. wordprocessing; namespace addstringtoword {public class program {public static void main (string [] ARGs) {addstring ("test.docx", "");} public static void addstring (string filepath, string Str) {using (wordprocessingdocument Doc = wordprocessingdocument. open (filepath, true) {paragraph = new paragraph (); Run = new run (); runproperties = new runproperties (); // attribute runfonts fonts = new runfonts () {Eastasia = "dfkai-Sb"}; // set fontsize = new fontsize () {val = "52 "}; // set the font size color = new color () {val = "red"}; // set the font style // Add the style to runproperties in the property. append (color); runproperties. append (size); runproperties. append (fonts); run. append (runproperties); run. append (new text (STR); paragraph. append (run); Doc. maindocumentpart. document. body. append (paragraph); Doc. maindocumentpart. document. save ();}}}}
As follows:
Openxml entry-add text to Word documents