How to understand text style in c #? I need to determine how the text is written:

  • Italic
  • Fat
  • Underlined
  • Nadched
  • Crossed out

How to recognize the mark on c #, if the text is taken from a word file?

StreamReader readLorem = new StreamReader(@"lorem.docx"); StreamWriter writeEncrypt = new StreamWriter(@"encrypt.docx"); StreamWriter writeDecrypt = new StreamWriter(@"decrypt.docx"); public Bacon() { InitializeComponent(); } private void Bacon_Load(object sender, EventArgs e) { string lorem = readLorem.ReadToEnd(); } 

This code does not preserve the style.

  • The outline is the formatting of the text, it is "superimposed" on the text in a special format that is implemented by one or another program, for example Word. And StreamWriter writes the text as a simple sequence of characters, here the concept of style is simply not applicable. - Bulson
  • So how then to be? - kot_mapku3
  • If you definitely need * .docx, then just Word, well, or read the documentation on this file format and write your own classes, which will be written in this format. - Bulson
  • Read about tag-based XML, you can write your own text file format, kh-m with paly and girls :) - Bulson
  • 3
    @Bulson do not talk nonsense. There are libraries ... - Pavel Mayorov

1 answer 1

As previously noted, you can use the Microsoft OpenXML library, which can be downloaded here. So after installation in your project you need to connect the following assembly:

  • DocumentFormat.OpenXml
  • WindowsBase

To work with the text you need to connect the namespace:

 using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; 

I created for demonstration a small console application for an example, below is a function that parses the text formatting that is contained in the file along the path . This function displays the text from the document and the formatting options of each text area:

 static void ReadDocx(string path) { try { using (var doc = WordprocessingDocument.Open(path, false)) { foreach (var p in doc.MainDocumentPart.Document.Body.Elements<Paragraph>()) { foreach (var r in p.Elements<Run>()) { Console.WriteLine(r.InnerText); Console.WriteLine("Является:"); if (r.RunProperties.Bold != null) Console.WriteLine("Жирный"); if (r.RunProperties.Italic != null) Console.WriteLine("Курсив"); if (r.RunProperties.Underline != null) Console.WriteLine("Подчёркнутый"); if (r.RunProperties.Strike != null) Console.WriteLine("Перечеркнутый"); if (r.RunProperties.VerticalTextAlignment != null) { if (r.RunProperties.VerticalTextAlignment.Val == VerticalPositionValues.Subscript) Console.WriteLine("Подстрочный"); if (r.RunProperties.VerticalTextAlignment.Val == VerticalPositionValues.Superscript) Console.WriteLine("Надстрочный"); } } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } 
  • one
    You know I get a NullReferenceException trying to get RunProperties for Run . Examining the question, I will say that RunProperies not a required property . Please add handling of this case. - Vadim Ovchinnikov
  • one
    Just information for everyone. There are also unofficial Nuget packages for the Open XML SDK. Newer and older . Essentially, they just posted the same libraries that you download from the Microsoft website. - Vadim Ovchinnikov
  • Another note: can you omit exception handling for this case? Here it is trivial and simply "swallows" all exceptions. - Vadim Ovchinnikov