Hi,
Using Aspose.Net.Words, I want to create a numbered list as below without using document builder object.
A nested list:
1. New York
1.1 Manhattan
2. Rome
2.1 Trastevere
2.2 Campidoglio
Below is my code for the same but it does not seem working. Please help.
Document doc = new Document();
doc.RemoveAllChildren();
Section section = new Section(doc);
doc.AppendChild(section);
// Lets set some properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;
Body body = new Body(doc);
section.AppendChild(body);
Paragraph para = new Paragraph(doc);
body.AppendChild(para);
Run run = new Run(doc);
run.Text = “A nested list :”;
para.AppendChild(run);
// Apply numbering to styles.
List list = doc.Lists.Add(ListTemplate.NumberArabicDot);
list.ListLevels[0].NumberFormat = “\x0000.”;
list.ListLevels[0].NumberStyle = NumberStyle.Arabic;
list.ListLevels[1].NumberFormat = “\x0000.\x0001.”;
list.ListLevels[1].NumberStyle = NumberStyle.Arabic;
list.ListLevels[2].NumberFormat = “\x0000.\x0001.\x0002.”;
list.ListLevels[2].NumberStyle = NumberStyle.Arabic;
// para.ListFormat.ApplyNumberDefault();
para.ListFormat.List = list;
para.ListFormat.ListLevelNumber = 0;
run = new Run(doc);
run.Text = ControlChar.LineBreak + “New York”;
para.AppendChild(run);
para.ListFormat.ListLevelNumber = 1;
para.ListFormat.ListIndent();
run = new Run(doc);
run.Text = ControlChar.LineBreak + “Manhattan”;
para.AppendChild(run);
para.ListFormat.ListLevelNumber = 0;
para.ListFormat.ListOutdent();
run = new Run(doc);
run.Text = ControlChar.LineBreak + “Rome”;
para.AppendChild(run);
para.ListFormat.ListLevelNumber = 1;
para.ListFormat.ListIndent();
run = new Run(doc);
run.Text = ControlChar.LineBreak + “Trastevere”;
para.AppendChild(run);
run = new Run(doc);
run.Text = ControlChar.LineBreak + “Campidoglio”;
para.AppendChild(run);
// Save the document.
doc.Save(DataDir + “Section.CreateFromScratch Out.doc”);
Thanks,
Pradeep
Hi Pradeep,
Thanks for your inquiry. A paragraph in a Microsoft Word document can be bulleted or numbered. When a paragraph is bulleted or numbered, it is said that list formatting is applied to the paragraph.
Please use the following code example to achieve your requirements.
Document
doc = new Document();
doc.RemoveAllChildren();
Section section = new Section(doc);
doc.AppendChild(section);
// Lets set some properties for the section.
section.PageSetup.SectionStart = SectionStart.NewPage;
section.PageSetup.PaperSize = PaperSize.Letter;
Body body = new Body(doc);
section.AppendChild(body);
Paragraph para = new Paragraph(doc);
body.AppendChild(para);
Run run = new Run(doc);
run.Text = "A nested list :";
para.AppendChild(run);
// Apply numbering to styles.
List list = doc.Lists.Add(ListTemplate.NumberArabicDot);
list.ListLevels[0].NumberFormat = "\x0000";
list.ListLevels[0].NumberStyle = NumberStyle.Arabic;
list.ListLevels[1].NumberFormat = "\x0000.\x0001";
list.ListLevels[1].NumberStyle = NumberStyle.Arabic;
list.ListLevels[2].NumberFormat = "\x0000.\x0001.\x0002";
list.ListLevels[2].NumberStyle = NumberStyle.Arabic;
// para.ListFormat.ApplyNumberDefault();
para.ListFormat.List = list;
//para.ListFormat.ListLevelNumber = 0;
para = new Paragraph(doc);
body.AppendChild(para);
para.ListFormat.List = list;
run = new Run(doc);
run.Text = "New York";
para.AppendChild(run);
//para.ListFormat.ListLevelNumber = 1;
para.ListFormat.ListIndent();
para = new Paragraph(doc);
body.AppendChild(para);
para.ListFormat.List = list;
run = new Run(doc);
run.Text = "Manhattan";
para.AppendChild(run);
//para.ListFormat.ListLevelNumber = 0;
para.ListFormat.ListOutdent();
para = new Paragraph(doc);
body.AppendChild(para);
para.ListFormat.List = list;
run = new Run(doc);
run.Text = "Rome";
para.AppendChild(run);
//para.ListFormat.ListLevelNumber = 1;
para.ListFormat.ListIndent();
para = new Paragraph(doc);
body.AppendChild(para);
para.ListFormat.List = list;
run = new Run(doc);
run.Text = "Trastevere";
para.AppendChild(run);
para = new Paragraph(doc);
body.AppendChild(para);
para.ListFormat.List = list;
run = new Run(doc);
run.Text = "Campidoglio";
para.AppendChild(run);
// Save the document.
doc.Save(MyDir + "Section.CreateFromScratch Out.doc");
Hope this helps you. Please let us know if you have any more queries.