Add list to existing template

c#

     var bld = string.Empty;
      if (briefDto.IsX) bld += $"- {Xtekst}" + Aspose.Words.ControlChar.LineBreak;
      if (briefDto.IsY) bld += $"- {YTekst}" + Aspose.Words.ControlChar.LineBreak;
      document.Range.Replace("[ToBeReplaced]", bld);

This works if Xtekst and Ytekst don’t run over to the next line, then the indentation is off even if the indentation are correct in the word template. Ideally I want to make a list with DocumentBuilder but I don’t know how to do the replace.

@AvanBrouwershaven,

Please compress the following resources into ZIP format and attach the .zip file here for testing:

  • A simplified source Word DOCX document
  • Your expected DOCX file showing the desired output. You can create this file manually by using MS Word.

We will then start further investigation into your particular scenario and provide you code to achieve the same expected output by using Aspose.Words.

Hello Awais,

(See attached file: Aspose.zip)

1E725139.png (469 Bytes)

Aspose.zip (145 KB)

@AvanBrouwershaven,

We are checking the scenario and will get back to you soon.

@AvanBrouwershaven You can easily achieve this using IReplacingCallback. For example you can use the technique like in the following code example:

Document doc = new Document(@"C:\Temp\Template.docx");
FindReplaceOptions opt = new FindReplaceOptions(new ReplaceWithListCallback());
doc.Range.Replace("[RedenAfwijzing]", "ListItem1|ListItem2|ListItem3", opt);
doc.Save(@"C:\Temp\out.docx");
private class ReplaceWithListCallback : IReplacingCallback
{
    public ReplaceAction Replacing(ReplacingArgs args)
    {
        DocumentBuilder builder = new DocumentBuilder((Document)args.MatchNode.Document);
        builder.MoveTo(args.MatchNode);

        string[] listItems = args.Replacement.Split('|');

        // Here you can insert any content including List, Images, tables.
        builder.ListFormat.ApplyBulletDefault();

        foreach (string item in listItems)
            builder.Writeln(item);

        builder.ListFormat.RemoveNumbers();

        args.Replacement = "";
        return ReplaceAction.Replace;
    }
}

But I think Find and Replace feature is not the best option to fill templates with data. If you have control over the template, I would suggest you to consider using Mail Merge or LINQ reporting Engine to fill your template with data.
For example for repeating items, like list items or table rows, you can use Mail Merge with Regions feature.

Thnx!, worked