We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Run's problem

Hello there! :slight_smile:


I have a problem in applying the styles to my word document. Let me explain in an example:
I have the text: “sometext… sometext replaced text”. Then I replace some old text with new, and finally apply new formatting to new text.

The problem is that new formatting applies not properly. It affects a bit more piece than I expected.
Result: “sometext… sometext replaced text”, but I need "“sometext… sometext replaced text”"

There is my Replacer:
ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
{
Node currentNode = e.MatchNode;
string value = ((Run)currentNode).Text;

currentNode.Range.Replace(oldValue, newValue, true, true);
if (value.Contains(oldValue))
{
var run = ((Run) currentNode);
run.Text = value.Replace(oldValue, newValue);
run.Font.HighlightColor = richText.HighlightColor;
}

return ReplaceAction.Skip;
}

How can I solve my issue.

Thank you.

Hello,


could you give me some advice.

Thank you

Hi Антон,


Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  1. Your input Word document you’re getting this problem with.
  2. Aspose.Words generated output Word document which shows the undesired behavior.
  3. Your expected document which shows the correct output. you can create this document using Microsoft Word.
  4. Please create a standalone runnable simple console application that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

Best regards,

Hi,


thanks for answer! I just attached necessary files - input document, result document, desired document and zipped sample console application.

Thanks a lot.

Hi Антон,


Thanks for the additional information. Please see the following changes in your code to achieve this:

var regex = new Regex(@"[[Hello]]", RegexOptions.IgnoreCase);

public class RichTextReplacer : IReplacingCallback

{<o:p></o:p>

private readonly string newValue;

private readonly string oldValue;

public RichTextReplacer(string newValue, string oldValue)

{

this.newValue = newValue;

this.oldValue = oldValue;

}

ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)

{

// This is a Run node that contains either the beginning or the complete match.

Node currentNode = e.MatchNode;

// The first (and may be the only) run can contain text before the match,

// in this case it is necessary to split the run.

if (e.MatchOffset > 0)

currentNode = SplitRun((Run)currentNode, e.MatchOffset);

// This array is used to store all nodes of the match for further highlighting.

ArrayList runs = new ArrayList();

// Find all runs that contain parts of the match string.

int remainingLength = e.Match.Value.Length;

while (

(remainingLength > 0) &&

(currentNode != null) &&

(currentNode.GetText().Length <= remainingLength))

{

runs.Add(currentNode);

remainingLength = remainingLength - currentNode.GetText().Length;

// Select the next Run node.

// Have to loop because there could be other nodes such as BookmarkStart etc.

do

{

currentNode = currentNode.NextSibling;

}

while ((currentNode != null) && (currentNode.NodeType != NodeType.Run));

}

// Split the last run that contains the match if there is any text left.

if ((currentNode != null) && (remainingLength > 0))

{

SplitRun((Run)currentNode, remainingLength);

runs.Add(currentNode);

}

DocumentBuilder builder = new DocumentBuilder((Document)e.MatchNode.Document);

builder.MoveTo(currentNode);

builder.Font.HighlightColor = Color.BlueViolet;

builder.Write(newValue);

foreach (Run run in runs)

run.Remove();

return ReplaceAction.Skip;

}

}

private static Run SplitRun(Run run, int position)

{

Run afterRun = (Run)run.Clone(true);

afterRun.Text = run.Text.Substring(position);

run.Text = run.Text.Substring(0, position);

run.ParentNode.InsertAfter(afterRun, run);

return afterRun;

}


I hope, this helps.

Best regards,

Hello, this is what I have using you changes - http://screencast.com/t/FkX050r5


Have you managed reproduce “desired” document on your back?

Thanks.

Hi Антон,


Thanks for your inquiry. Yes. I used the following code for testing. Also, please see attached the output document i.e. produced on my side using Aspose.Words for .NET 14.3.0.

Document doc = new
Document(MyDir + @“InputFile.doc”);

var regex = new Regex(@"\[\[Hello\]\]", RegexOptions.IgnoreCase);

doc.Range.Replace(regex, new RichTextReplacer("New Text Value ..", ""), false);

doc.Save(MyDir + @“out.doc”);


Please let me know if I can be of any further assistance.

Best regards,