Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReplaceText incorrect text changing. #666

Open
Kazbek opened this issue Oct 7, 2021 · 5 comments
Open

ReplaceText incorrect text changing. #666

Kazbek opened this issue Oct 7, 2021 · 5 comments

Comments

@Kazbek
Copy link

Kazbek commented Oct 7, 2021

This code

foreach (var p in doc.Paragraphs)
{
    if (p.Text == null)
        continue;
    foreach (var pair in replacers)
        if (p.Text.Contains(pair.Key))
            p.ReplaceText(pair.Key, pair.Value);
}`

When I using two text surrounded by dollars make incorrect replacement.
Examples on screenshot:
NpoiPneStepReplace
As you see p.Text after one step changing requested part and also removing "$" around last word (what shouldn't happen).
Minimal example of project and docx file which I used as template you can find here: https://github.com/Kazbek/NpoiTextReplaceDemo
If you need any additional details tell me.

@tonyqus
Copy link
Member

tonyqus commented Oct 15, 2021

I'm debugging your case and I figure out the Run list in your word file for one replacement.
image

As you can see that the second $ and third $ is the segments of the last Run. Since internally XWPFRun.ReplaceText will replace all texts that matchs '$', the third $ is also set to empty string as the second one.

@tonyqus
Copy link
Member

tonyqus commented Oct 15, 2021

To work around this, I suggest you change the placeholder texts as below

$FINALQUALIFYINGWORK_QUESTION_1_ASKING_SHORT$: #FINALQUALIFYINGWORK_QUESTION_1_QUESTION#
$FINALQUALIFYINGWORK_QUESTION_2_ASKING_SHORT$: #FINALQUALIFYINGWORK_QUESTION_2_QUESTION#

I've tested on my box and it works perfectly.

@Kazbek
Copy link
Author

Kazbek commented Oct 15, 2021

I cant change placeholder syntax. I workaround by using another lib. But want to use npoi, will it be fixed at some point or not?

@tonyqus tonyqus modified the milestones: NPOI 2.5.5, NPOI 2.5.6 Oct 15, 2021
@tonyqus tonyqus modified the milestones: NPOI 2.5.6, NPOI 2.5.7 Feb 4, 2022
@tonyqus
Copy link
Member

tonyqus commented Feb 14, 2022

Do you still wanna use NPOI or you have switched to another library?

@Kazbek
Copy link
Author

Kazbek commented Feb 14, 2022

Still want.

@tonyqus tonyqus added this to the NPOI 2.7.1 milestone Dec 14, 2023
@tonyqus tonyqus modified the milestones: NPOI 2.7.1, NPOI 2.7.2 May 4, 2024
@tonyqus tonyqus modified the milestones: NPOI 2.7.2, NPOI 2.7.3 Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants