How to set font to numbering lists using aspose.words

Hi Team,

We are using Aspose.words to set the listlevels and that’s including numbering formats.

We use {#L1#}, {#L2#} and so on as placeholders for assigning the paragraph formats and below is the code.

Aspose.Words.Document doc = new Aspose.Words.Document(filepath);
DocumentBuilder db = new DocumentBuilder(doc);
Aspose.Words.Lists.List list = doc.Lists.Add(Aspose.Words.Lists.ListTemplate.NumberLowercaseLetterDot);

list.ListLevels[0].NumberStyle = NumberStyle.DecimalFullWidth;
list.ListLevels[1].NumberStyle = NumberStyle.DecimalFullWidth;
list.ListLevels[2].NumberStyle = NumberStyle.DecimalFullWidth;
list.ListLevels[3].NumberStyle = NumberStyle.DecimalFullWidth;

list.ListLevels[0].NumberFormat = "\x0000";
list.ListLevels[1].NumberFormat = "\x0000.\x0001";
list.ListLevels[2].NumberFormat = "\x0000.\x0001.\x0002";
list.ListLevels[3].NumberFormat = "\x0000.\x0001.\x0002.\x0003";

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
bool nolevelreset = false;

int currpara = 0;

foreach (Aspose.Words.Paragraph p in paragraphs)
{
    if (nolevelreset)
    {
        // This is used for reseting the numbering formats
        Aspose.Words.Lists.List list1 = doc.Lists.AddCopy(list);
        list = list1;
        nolevelreset = false;
    }

    if (p.GetText().IndexOf("#L1#}") >= 0)
    {
        if (xdoc.GetElementsByTagName("Level").Count > 0)
        {
            p.Range.Replace("#L1#}", "", false, false);
            p.ListFormat.List = list;
            p.ListFormat.ListLevelNumber = 0;
            currpara = 0;
        }
        else
        {
            p.Range.Replace("#L1#}", "", false, false);
        }

    }

    else if (p.GetText().IndexOf("#L2#}") >= 0)
    {
        if (xdoc.GetElementsByTagName("Level").Count > 1)
        {
            p.Range.Replace("#L2#}", "", false, false);
            p.ListFormat.List = list;
            p.ListFormat.ListLevelNumber = 1;
            currpara = 1;
        }
        else
        {
            p.Range.Replace("#L2#}", "", false, false);
        }

    }

    else if (p.GetText().IndexOf("#L3#}") >= 0)
    {
        if (xdoc.GetElementsByTagName("Level").Count > 2)
        {
            p.Range.Replace("#L3#}", "", false, false);
            p.ListFormat.List = list;
            p.ListFormat.ListLevelNumber = 2;
            currpara = 2;
        }
        else
        {
            p.Range.Replace("#L3#}", "", false, false);
        }
    }

    else if (p.GetText().IndexOf("#L4#}") >= 0)
    {
        if (xdoc.GetElementsByTagName("Level").Count > 3)
        {
            p.Range.Replace("#L4#}", "", false, false);
            p.ListFormat.List = list;
            p.ListFormat.ListLevelNumber = 3;
            currpara = 3;
        }
        else
        {
            p.Range.Replace("#L4#}", "", false, false);
        }
    }

    else if (p.GetText().IndexOf("#NL#}") >= 0)
    {
        p.Range.Replace("#NL#}", "", false, false);
        p.ListFormat.ListLevelNumber = 1;
        currpara = 0;
    }
    else if (p.GetText().IndexOf("#NLR#}") >= 0)
    {
        p.Range.Replace("#NLR#}", "", false, false);
        nolevelreset = true;
    }
    else
    {
        if (p.IsListItem)
        {
            p.ParagraphFormat.LeftIndent = list.ListLevels[currpara].TextPosition + 18;//added correction for good looking alignment
        }

    }
}
doc.GetText().Replace("{#L1#}", "").Replace("{#L2#}", "").Replace("{#L3#}", "").Replace("{#L4#}", "").Replace("{#NL#}", "").Replace("{#NLR#}", "");
doc.Save(filepath, SaveFormat.Doc);

So, now my question is in the final document what ever numbers that are replaced for our custom tags ({#L1#}…) , they were shown with a different font (always seems to be timesnewromman) instead of the font on each paragraph text.

So what’s the best methodology for maintaining the same font style as of the paragraph that its going to be assigned to…?

Thanks,
Yaswanth

Hi Yaswanth,

Thanks for your inquiry. Please use the latest version of Aspose.Words for .NET and try following code. Hope this helps you.

Document doc = new Document(myDir + @"output.doc");
doc.Lists[0].ListLevels[0].Font.Name = "Arial";
doc.Save(myDir + @"16.12.0.docx"); 

For more information, kindly visit List level collection. If you still face any problem, please let us know we will try to help you as much we can. Thanks.