Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Wed Aug 11, 2021 2:17 pm

Hello,

using Spire.doc, how can I replace an exiting text in a DOCX document with chemical formular with subscipted numbers ?

So for example I have a „MatchText“ in DOCX and need this match text to be replaced with „result text“ in my DOCX

Something like this:

Replace_chemical_formular (doc,“O2“);

Chem.jpg

Heinz4711
 
Posts: 20
Joined: Thu Feb 07, 2019 5:22 pm

Thu Aug 12, 2021 6:57 am

Hello,

Thanks for your inquiry.
Please refer to the following code to meet your needs.
Code: Select all
            Document document = new Document();
            document.LoadFromFile("test.docx");

            Document newDocument = new Document();
            Paragraph chemical = newDocument.AddSection().AddParagraph();
            TextRange subscript = new TextRange(newDocument);


            subscript.Text = "2";
            subscript.CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            chemical.AppendText("O");
            chemical.ChildObjects.Add(subscript.Clone());
            TextSelection textSelections1 = newDocument.FindString(chemical.Text, true, true);
            document.Replace(chemical.Text, textSelections1,true,true);
            chemical = newDocument.AddSection().AddParagraph();

            subscript.Text = "2";
            subscript.CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            chemical.AppendText("N");
            chemical.ChildObjects.Add(subscript.Clone());
            textSelections1 = newDocument.FindString(chemical.Text, true, true);
            document.Replace(chemical.Text, textSelections1, true, true);
            chemical.ChildObjects.Clear();

            subscript.Text = "2";
            subscript.CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            chemical.AppendText("H");
            chemical.ChildObjects.Add(subscript.Clone());
            chemical.AppendText("O");
            textSelections1 = newDocument.FindString(chemical.Text, true, true);
            document.Replace(chemical.Text, textSelections1, true, true);
            chemical.ChildObjects.Clear();

            subscript.Text = "4";
            subscript.CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            chemical.AppendText("CH");
            chemical.ChildObjects.Add(subscript);
            textSelections1 = newDocument.FindString(chemical.Text, true, true);
            document.Replace(chemical.Text, textSelections1, true, true);
            chemical.ChildObjects.Clear();

            subscript.Text = "2";
            subscript.CharacterFormat.SubSuperScript = SubSuperScript.SubScript;
            chemical.AppendText("H");
            chemical.ChildObjects.Add(subscript.Clone());
            chemical.AppendText("S");
            subscript.Text = "4";
            chemical.ChildObjects.Add(subscript.Clone());
            chemical.AppendText("C");
            textSelections1 = newDocument.FindString(chemical.Text, true, true);
            document.Replace(chemical.Text, textSelections1, true, true);

            document.SaveToFile("result.docx",FileFormat.Docx);

If there are any other issues related to our products, just feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Tue Aug 31, 2021 10:43 am

Hello,

Greetings from E-iceblue!
Did the code we provided work for you? Any feedback will be greatly appreciated.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1271
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc