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.
Thu Feb 28, 2019 1:11 pm
Hello,
I use
- Code: Select all
document.Replace(matchString, newString, ....)
to replace a text.
Now additionally to replace a text I also need to set the color of the new text (newString)
So basically I would need a function like this:
- Code: Select all
document.Replace(matchString, newString, newStringColor, ....)
Can I somehow do this ?
-
Heinz4711
-
- Posts: 20
- Joined: Thu Feb 07, 2019 5:22 pm
Fri Mar 01, 2019 6:10 am
Hello,
Thanks for your inquiry.
Sorry that there is no a direct method like
document.Replace(matchString, newString, newStringColor, ....). But you could define a method by yourself to achieve it. Below is the sample code for your kind reference. If there is any question, please provide your testing document as well as the string information of matchString and newString to help us further investigate it. You could send them to us via email (
support@e-iceblue.com).
- Code: Select all
{
Document document = new Document();
document.LoadFromFile(@"sample.docx");
ReplaceTextAndColor(document, "matchString", "newString", Color.Red);
document.SaveToFile("Result.docx", Spire.Doc.FileFormat.Docx2013);
}
private static void ReplaceTextAndColor(Document doc, string matchString, string newString, Color color)
{
//Search matchString
TextSelection[] text = doc.FindAllString(matchString, false, true);
foreach (TextSelection seletion in text)
{
//Replace with newString
doc.Replace(matchString, newString, false, true);
}
//Search newString
TextSelection[] text2 = doc.FindAllString(newString, false, true);
foreach (TextSelection seletion in text2)
{
//set color for newString
seletion.GetAsOneRange().CharacterFormat.TextColor = color;
}
}
Sincerely,
Lisa
E-iceblue support team
-
Lisa.Li
-
- Posts: 1261
- Joined: Wed Apr 25, 2018 3:20 am
Fri Mar 01, 2019 6:38 am
the code that you suggested does not only set the color the search text (in my example "hPa" - only this should be red)- but it sets the color for whole line:
Login to view the files attached to this post.
-
Heinz4711
-
- Posts: 20
- Joined: Thu Feb 07, 2019 5:22 pm
Fri Mar 01, 2019 8:36 am
Hello,
Thanks for your quick responce.
Please provide the string information of matchString and newString, your testing document as well as well as your desired effect of output for our reference, then we will look into it and guide you accordingly. You could send them to us via email (
support@e-iceblue.com). Thanks in advance.
Sincerely,
Lisa
E-iceblue support team
-
Lisa.Li
-
- Posts: 1261
- Joined: Wed Apr 25, 2018 3:20 am
Wed Mar 06, 2019 9:03 am
Hello,
Sorry to bother you.
Could you please let us know how is your issue going? Thanks in advance.
Sincerely,
Lisa
E-iceblue support team
-
Lisa.Li
-
- Posts: 1261
- Joined: Wed Apr 25, 2018 3:20 am