Use Existing Custom List Template Styles in DOCM Word Document and Write new List Paragraph Items using C#


We are facing couple of issues.

  1. trying to create a word (.docm) document using a predefined template via Aspose words c#. But in output file there is lot of extra while spaces after the generated numbers as shown in attached image.

  2. Also is there is way to use predefined custom list format. Because there is custom list formats defined in template as shown in attached image and i want to use it via code to create list. Please provide us c# code example to this custom list format.



I am afraid, we do not see any attachments (images) in this thread. Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document(s)
  • Aspose.Words 20.3 generated output DOCX files showing the undesired behaviors
  • Your expected DOCX files showing the desired outputs. You can create these documents by using MS Word.
  • Image files
  • Please also create a standalone simple console application (source code without compilation errors) that helps us to reproduce your current problems on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

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

aspose (923.6 KB)

We are currently using Aspose.Words.16.1.0 in our code. I have added everything you asked for in attached zip file.
I am trying to find a way to use custom list formating (as shown in screenshot with their names) present in our template file to create list via C# code.
I have also added the original query with its screenshot that I have shared with you earlier.



Please check the following code that demonstrates how to write new text in Word document by using existing List:

Document doc = new Document("E:\\Temp\\aspose ticket\\template.Docm");

Aspose.Words.Lists.List targetList = null;
foreach (Aspose.Words.Lists.List list in doc.Lists)
    if (list.Style != null && list.Style.Name == "Red Flag Details Outline")
        targetList = list;

if (targetList != null)
    DocumentBuilder builder = new DocumentBuilder(doc);

    builder.ParagraphFormat.Style.Name = targetList.Style.Name;
    builder.ListFormat.List = targetList;

    builder.ListFormat.ListLevel.StartAt = 1;
    builder.Writeln("first level");

    builder.Writeln("second level");

    builder.Writeln("third level");
    builder.Writeln("another entry on this level");

    builder.Writeln("fourth level");

    builder.ListFormat.List = null;

doc.Save("E:\\Temp\\aspose ticket\\20.3.docm");

Hope, this helps in achieving what you are looking for.

Thank you for your code sample, it been really helpful. I am still facing an technical challenge. When I am trying to create an heading 2 for table of content in the list. It is automatically applyig some custom heading style (which is present in template) to the generated file but I want to use a different custom heading style( present in template.). Please provide me sample code to use custom heading via code. I have attached the screenshot showing the custom heading style.

new aspose (750.0 KB)



We are working on your query and will get back to you soon.

Any update team or you need more time?

Thanks and Regards


Please spare us some time. We are checking this scenario and will get back to you with our findings soon.


You can fix “generated File.Docm” document by using the following code:

Document doc = new Document("E:\\Temp\\new aspose Ticket\\generated File.Docm");

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
    if (para.ToString(SaveFormat.Text).StartsWith("third level") ||
        para.ToString(SaveFormat.Text).StartsWith("another third level"))
        Aspose.Words.Lists.List list = para.ListFormat.List;
        int listLevelNumber = para.ListFormat.ListLevelNumber;

        para.ListFormat.List = null;

        para.ParagraphFormat.Style.Name = "3. Red Flags Summary (hyperlink)";
        para.ListFormat.List = list;

        for (int i = 1; i <= listLevelNumber; i++)

        foreach (Run run in para.Runs)
            run.Font.Size = 10;
            run.Font.Bold = true;

doc.Save("E:\\Temp\\new aspose Ticket\\20.3.docm");