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.

Mon Jun 06, 2022 5:25 pm

In a document sentence, I replace a placeholder with another value, in my case with an empty string. If in the phrase where the replacement is made there are words with formatting, for example, in bold, this attribute is lost. I have attached a word document for testing. This example replaces the placeholders <if-ifok> and </if-ifok> with empty strings. I also attach the resulting document.
I am using Spire.Doc version 10.5.10 with java on windows 11.

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Tue Jun 07, 2022 9:39 am

Hi,

Thank you for your inquiry.
I did an initial test but did not reproduce your problem. Below is my test code, is it the same as yours? Here I uploaded my executable jar for your reference, you could execute it on your side and tell us the result.

Code: Select all
        Document doc=new Document();
        doc.loadFromFile("PruebaWord.docx");
        doc.replace("<if-ifok>","a",true,true);
        doc.replace("</if-ifok>","b",true,true);
        doc.saveToFile(output);

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Tue Jun 07, 2022 6:30 pm

The code you have put works, but in my case I have some more instructions. If you incorporate what I put below into your example code, you will see that it does not work. Basically, if I comment out the getAsOneRange() line in my code it also works, so I think the call to that method does something that causes the end result to be wrong.

My code:

Document document = new Document();
document.loadFromFile("PruebaWord.docx");

Pattern regex=Pattern.compile("<if-(\\w+)>([\\s\\S]*?)((<else-\\1>)([\\s\\S]*?))?(<\\/if-\\1>)",Pattern.CASE_INSENSITIVE);
TextSelection CadenasEncontradas[];
CadenasEncontradas = document.findAllPattern(regex, true);
TextRange Range = CadenasEncontradas[0].getAsOneRange();

document.replace("<if-ifok>", "a", false, true); // Quitamos la instrucion de inicio del if
document.replace("</if-ifok>", "b", false, true);
document.saveToFile("PruebaWord-procesado.docx", FileFormat.Docx);

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Wed Jun 08, 2022 8:07 am

Hi,

Thank you for your feedback.
Please note the found TextSelection may have multiple textranges, which may have different formats. The getAsOneRange() method will merge multiple textranges as one, this leads to the issue that formats of textranges were lost. To get rid of this issue, please use the TextRange Range = CadenasEncontradas[0].getRanges()[0]; rather than the getAsOneRange().
If there is other question, please feel free to contact us.

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Thu Jun 09, 2022 11:04 am

I use getAsOneRange to determine if the found text is a whole paragraph, multiple paragraphs, or just a part of it. I need to know this because if I were to remove everything found, I also want to remove the paragraph breaks. As I really only need to know if there are complete paragraphs in case I have to remove all the content, I can do this management at that moment and not before and so the formatting stays if the text does not have to be removed.

So I have it fixed now. Only one thing, the documentation would have to be improved because there is very little information about each method and therefore these problems arise, which are more due to ignorance of use.

Thanks.

jmparada
 
Posts: 47
Joined: Wed May 25, 2022 7:50 am

Fri Jun 10, 2022 1:36 am

Hi,

Thank you for your suggestion, we will improve it in the future.
Wish you all the best :D

Sincerely,
Kylie
E-iceblue support team
User avatar

kylie.tian
 
Posts: 412
Joined: Mon Mar 07, 2022 2:30 am

Return to Spire.Doc

cron