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 Jun 03, 2020 9:56 pm

Hello!
I'm trying to replace a word inside a Paragraph.

Explaining the context, i'm inserting Texts (that have HTML inside the Text Variable) into a Paragraph, from my Database.

Code: Select all
TextSelection position = document.FindString("textPositionIntoWordDocument", true, true);
TextRange range = position.GetAsOneRange();
Paragraph paragraph = range .OwnerParagraph;

paragraph .Text = String.Empty;
paragraph .AppendHTML(stringWithHTML);


And I want to replace a word inside that Text that was inserted before, called {customerName}

I'm trying this, but this will replace all my HtmlText inserted.

Code: Select all
 TextSelection[] positions = document.FindAllString("{customerName}", true, true);
            foreach (var actualPosition in positions)
            {
                TextRange range= actualPosition .GetAsOneRange();
                Paragraph paragraph = range.OwnerParagraph;

                paragraph .Text = Customer.Name;
            }


And the worst, if my inserted text have a HTML attribute, like a Header Text (<h1>), when I "replace", I lose that, and I dont want it!

Example:
String stringHtml = "<h1>Hello {customerName}</h1>";

When I replace:
Kevinka999

What I want
Hello Kevinka999


If it’s not asking too much, simplify max u can the code so I can understand better how it works :mrgreen:

Thanks a lot!

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Thu Jun 04, 2020 3:16 am

Hello,

Thanks for your inquiry.
For your requirement, you can directly use the method "Document.Replace" to replace the text, please refer to the following code. If you have further questions, just feel free to write back.
Code: Select all
    //Replace(string matchString, string newValue, bool caseSensitive, bool wholeWord)
    document.Replace("{customerName}", "Kevinka999", false, true);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Fri Jun 05, 2020 12:43 pm

OMG! Perfect dude.

That solved my problem!!!!
I really enjoyed this, thanksss fella!

kevinka999
 
Posts: 16
Joined: Wed Apr 08, 2020 7:09 pm

Mon Jun 08, 2020 1:18 am

Hello,

Glad to hear that!
If you encounter any issues related to our product in the future, just feel free to contact us.
Have a nice day!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc

cron