Replace Text in Word Document with HTML with Option to Select Formatting inside HTML or of Document Builder C# .NET

Thanks again, yeah my username was funny/clever when I was younger and had just moved to the UK. Now being back in NZ its actually a little embarrassing but hey ho.

Thanks for being so patient with this issue. I do understand the complexity, I’ve written similar code myself in the past. Thinking back when we started with V5.5 I don’t think you used any of the HTML formatting at all, so things have definitely improved. And while the solution is a bit messy at least you’re sticking with us.

Now one (hopefully) final question, I think I understand how you are doing the compare to see whether the formatting was different to the default. I guess then that that leaves once small hole, where I might be intentionally setting the formatting of the inserted text to the same formatting as the default, and in such a case it would then look like the default and be over-written?

So for example Document default: Times New Roman
Inserted Test
Test
Merge field formatting: Arial

In this case the inserted formatting appears to be the default, will therefore change to Arial if I am not mistaken?

I’m sure I can work with this, just wanting to be clear.

*CoolKiwiBloke:
Thanks again for persisting with this. It does now work as expected, although there was one small error in your code below which should read NOT equals :slight_smile:

else if (prop.CanWrite)
{
// dest value != default dont copy
if (**!**prop.GetValue(dest, null).Equals(prop.GetValue(compare, null)))
{
// If we can write to this property then copy the value across.
prop.SetValue(dest, prop.GetValue(source, null), null);
}
}

Cheers,

Dale*

Ignore my code change - the compare was correct as it stood, it just needed the try/catch in case it failed.

*aske012:
Hi Dale,

Thanks for your inquiry

3) You can use Range.Replace to find and replace text. Please see the second code example under Find and Replace Overview in the documentation which uses a Regex to match text.

If you find you need to have more control over how the text is replaced then you may need to use the replace evaluator overload as described here.

If we can help with anything else, please feel free to ask.

Thanks,*

I’ve just had a shot at this, and its working fine, of course what I need to be able to do is insert HTML using the mechanisms we’re discussed. I have written some code that works, but I don’t think its very safe as it makes a few assumptions about the node provided being a run. However if its not a run I wouldn’t know how to handle things. Could you please run your eye over this code and suggest any changes to make it more robust and do things in the way you would normally do them.

DocumentBuilder builder = new DocumentBuilder(doc);
doc.Range.Replace(new Regex(@"\[.+?\]"), new MyReplaceEvaluator(builder), false);
public class MyTestReplaceEvaluator : IReplacingCallback
{
    private DocumentBuilder Builder { set; get; }

    public MyTestReplaceEvaluator(DocumentBuilder builder)
    {
        Builder = builder;
    }

    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        string MergeFieldData = "*Test*";
        Node currentNode = e.MatchNode;
        if (e.MatchOffset > 0)
        {
            SplitRun(e.MatchNode as Run, e.MatchOffset);
            currentNode = e.MatchNode.NextSibling;
        }
        Builder.MoveTo(currentNode); // Insert before merge field
        new DocumentBuilderHelper(Builder).InsertHtmlWithBuilderFormatting(MergeFieldData);
        (currentNode as Run).Text = (currentNode as Run).Text.Replace(e.Match.ToString(), "");
        return ReplaceAction.Skip; // We've handled it ourselves
    }

    private 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;
    }
}

Hi Dale,

Thanks for this additional information.

I believe you are correct with your assumption that the Arial formatting will override the default Times New Roman in that particular case. From memory, this may well have been how the InsertHtml behavior worked in Aspose.Words 8.X before the changes to the way the formatting is applied however I cannot be 100% sure on that. Hopefully this doesn’t cause too much of a problem.

Regarding your replace code, it looks pretty good. You should only ever expect Run nodes to be sent to the callback so it should work fine. However note that your matching text maybe made up of more than one run, which would mean you may need to use the more elaborate way of splitting runs as found in the example code. This depends on your expected input documents though.

Please let me know if I can help with anything else.

Thanks,

Thanks again, that should keep me busy for a while now :slight_smile:

The issues you have found earlier (filed as WORDSNET-6726) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(1)