Formatting lost in range replace

I am using example from Range.replace question But I still loose all the original doc formatting…
My code is like:

Document efDoc = new Document(filename);
ReplaceEvaluator RE = new ReplaceEvaluator(ReplaceEvaluator);
efDoc.Range.Replace(new Regex(@"[PATIENTFIRST]"), RE, false);
…
dr.Document = efDoc;
dr.PrinterSettings.PrinterName = printerName;
dr.Print();
private ReplaceAction ReplaceEvaluator(object sender, ReplaceEvaluatorArgs args)
{

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

    builder.MoveTo(args.MatchNode);
    DataRowView row2 = (DataRowView)PatientbindingSource.Current;
    String RepValue = "";
    // MessageBox.Show(args.Match.Value.ToString());
    switch (args.Match.Value.ToString())
    {
        case "[PATIENTFIRST]":
            RepValue = row2["FirstName"].ToString();
            break;
            // ...
    }
    SplitRun(builder, args.MatchOffset, args.Match.Value.Length, RepValue);

    return ReplaceAction.Skip;
}

private void SplitRun(DocumentBuilder builder, int offset, int length, string replacement)
{
    Run firstRun = (Run)builder.CurrentNode;
    Run prevRun = new Run(firstRun.Document, firstRun.Text.Substring(0, offset));
    int total;
    ArrayList runs = GetRunsToRemove(firstRun, length, out total);
    Run lastRun = (Run)runs[runs.Count - 1];
    int lastRunLength = lastRun.Text.Length;
    int lastRunOffset = (offset + length) - (total - lastRunLength);
    Run nextRun = new Run(lastRun.Document, lastRun.Text.Substring(lastRunOffset, lastRunLength - lastRunOffset));
    builder.InsertNode(prevRun);
    // builder.Font.HighlightColor = Color.Red;
    builder.Write(replacement);
    builder.MoveTo(lastRun);
    builder.InsertNode(nextRun);
    foreach (Run run in runs)
        run.Remove();
}

private ArrayList GetRunsToRemove(Run firstRun, int matchLength, out int total)
{
    total = 0;
    Run run = firstRun;
    ArrayList runs = new ArrayList();
    while (total < matchLength)
    {
        total += run.Text.Length;
        runs.Add(run);
        run = (Run)run.NextSibling;
    }
    return runs;
}

attached is word doc. Most important it looses the blank lines?

Hi

Thanks for your request. In your case, you can use code like the following to fill your template with data:

// Open document.
Document doc = new Document(@"Test001\in.doc");
// Find and replace placeholders
doc.Range.Replace(new Regex(@"\[(?\S+)\]"), new ReplaceEvaluator(ReplaceEvaluator), false);
// Save output document
doc.Save(@"Test001\out.doc");

private ReplaceAction ReplaceEvaluator(object sender, ReplaceEvaluatorArgs e)
{
    switch (e.Match.Groups["name"].Value)
    {
        case "PATIENTFIRST":
            e.Replacement = "Den";
            break;
        case "PATIENTLAST":
            e.Replacement = "Martin";
            break;
        case "MAILINGADDRESS":
            e.Replacement = "Main str, 28";
            break;
        case "MAILINGCITY":
            e.Replacement = "Auckland";
            break;
        case "MAILINGSTATECODE":
            e.Replacement = "6545665";
            break;
        case "MAILINGPOSTALCODE":
            e.Replacement = "11111";
            break;
        case "PROVIDERLASTNAME":
            e.Replacement = "Smith";
            break;
    }
    return ReplaceAction.Replace;
}

In additional, I think, it is better to use simple mail merge in your case. Please follow the link to learn more about mail merge:
https://docs.aspose.com/words/net/mail-merge-and-reporting/

Hope this helps.

Best regards.

sorry to report, that does not fix the problem…I have no issue getting the replace to work, When printed it looses all formatting IE: there is a blank line between the address and Dear, also one before the paragraph. all the blank lines disapear

Hi

Thanks for your request. Which version of Aspose.Words do you use? And could you please show me code you use to print your document? I will check the issue and provide you more information.

Best regards.

dll says version 5.3.0.0 where else would I look?
dr.Document = efDoc;
dr.PrinterSettings.PrinterName = printerName; //I get this from print dlg
dr.Print();

We are happy to tell you that the new Rendering Engine has replaced the “old Viewer Beta”. The Rendering Engine can print, save as images or draw onto .NET Graphics object any document page.

Please see Aspose.Words documentation to learn more about new features.

Saving to image

In additional, new Rendering engine allows you to convert Word document to PDF directly (without using Aspose.Pdf).

Saving to Pdf

The latest version of Aspose.Words is available for download from downloads section or from NuGet

That did the trick…thanks