Free Support Forum - aspose.com

List Numbers not showing up

I’m trying to create a numbered list, but the numbers are not showing up. What am I missing? First, I setup the doc and doc builder:

Aspose.Words.Document doc = new Aspose.Words.Document(Path.Combine(templateFilePath, ioFileName));
var builder = new Aspose.Words.DocumentBuilder(doc);
var bookmark = doc.Range.Bookmarks[“recs”];
		Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">List</span> list <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">null</span>;

		<span style="color:#93c763;">if</span> (<span style="color:#678cb1;">Constants</span><span style="color:#e8e2b7;">.</span>UseCaseTypeRecContinuousNumbering)
		{
			list <span style="color:#e8e2b7;">=</span> doc<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span>Add(Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">ListTemplate</span><span style="color:#e8e2b7;">.</span>NumberArabicDot);
			list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>NumberStyle <span style="color:#e8e2b7;">=</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">NumberStyle</span><span style="color:#e8e2b7;">.</span>Arabic;
			list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>NumberPosition <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">0</span>;
			list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>TextPosition <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">20</span>;
			list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>TabPosition <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">20</span>;

		}
		<span style="color:#93c763;">else</span>
		{
			list <span style="color:#e8e2b7;">=</span> doc<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span>Add(Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">ListTemplate</span><span style="color:#e8e2b7;">.</span>BulletDefault);
		}</pre></div><div><br></div><div><br></div><div>Then I iterate over some data points and add a new list item for each one (there's some omitted code here that does the iterating and also decides if a grouping header needs to also be inserted)</div><div><br></div><div><pre style="font-family:Calibri;font-size:16;color:#f1f2f3;background:#22282a;">Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Paragraph</span> recPara <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">new</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Paragraph</span>(doc);
			recPara<span style="color:#e8e2b7;">.</span>ParagraphFormat<span style="color:#e8e2b7;">.</span>StyleIdentifier <span style="color:#e8e2b7;">=</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">StyleIdentifier</span><span style="color:#e8e2b7;">.</span>Normal;				
			bookmark<span style="color:#e8e2b7;">.</span>BookmarkStart<span style="color:#e8e2b7;">.</span>ParentNode<span style="color:#e8e2b7;">.</span>ParentNode<span style="color:#e8e2b7;">.</span>InsertBefore(recPara, bookmark<span style="color:#e8e2b7;">.</span>BookmarkStart<span style="color:#e8e2b7;">.</span>ParentNode);

			Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Section</span> thisSection <span style="color:#e8e2b7;">=</span> GetParentSection(recPara);
			<span style="color:#93c763;">var</span> sectionParas <span style="color:#e8e2b7;">=</span> thisSection<span style="color:#e8e2b7;">.</span>GetChildNodes(Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">NodeType</span><span style="color:#e8e2b7;">.</span>Paragraph, <span style="color:#93c763;">true</span>);
			builder<span style="color:#e8e2b7;">.</span>MoveToParagraph(sectionParas<span style="color:#e8e2b7;">.</span>IndexOf(recPara), <span style="color:#ffcd22;">0</span>);
			builder<span style="color:#e8e2b7;">.</span>ListFormat<span style="color:#e8e2b7;">.</span>List <span style="color:#e8e2b7;">=</span> list;
			builder<span style="color:#e8e2b7;">.</span>InsertHtml(rec<span style="color:#e8e2b7;">.</span>RecText);
			recPara<span style="color:#e8e2b7;">.</span>ParagraphFormat<span style="color:#e8e2b7;">.</span>LeftIndent <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">70</span>;</pre></div><div><br></div><div>The list seems to be getting made, as it's indented the way that I want, but there is neither a bullet nor a number for each list item being added</div><div><br></div><div><br></div><div>Thanks</div><div><br></div>

Hello

Thanks for your inquiry. Could you please create simple application, which will demonstrate the problem on my side? I will check it and provide you more information.

Best regards,

ok, attached a project with all the pertinent stuff included. Thanks for looking into it

Hi Jason,

Thanks for your inquiry.

When you insert content from HTML the formatting is taken explicty from the HTML and not from the DocumentBuilder. Therefore currently you need to use

    and
  • tags in your HTML snippets to make the inserted content have bullet points.

    In your case since you want to apply formatting from the DocumentBuilder you should just use the Writeln method with your plain text instead. Please see the modified code below.

    string[] listItems = new string[]{<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

    "First Item",

    "Second Item",

    "Last Item"

    };

    builder.MoveTo(bookmark.BookmarkStart);

    foreach (var rec in listItems)

    {

    builder.ListFormat.List = list;

    builder.CurrentParagraph.ParagraphFormat.LeftIndent = 70;

    builder.Writeln(rec);

    }

    bookmark.BookmarkStart.ParentNode.Remove();

    doc.Save("output.docx");

    Thanks,

Adam,

Thanks for your help, but that answer doesn’t really get me to my goal. I did stumble on a happy workaround though. If you append any character to your HTML, the InsertHTML() function preserves the Numbered List formatting (found this out when I attempted to write out the numbering manually; this resulted in the numbers being duplicated as in “1. 1. stuff”).

That coupled with a little regex to kill the paragraph tags (which were causing the indentation to get out of whack) ended up solving my problem. I’ll post the code below, which allows us to build a continuously numbered list, based on HTML user inputs (ckeditor) and grouped by region headers:

Aspose.Words.Document doc = new Aspose.Words.Document(“template.docx”);
var builder = new Aspose.Words.DocumentBuilder(doc);
var bookmark = doc.Range.Bookmarks[“recs”];
		Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">List</span> list <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">null</span>;

		list <span style="color:#e8e2b7;">=</span> doc<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span>Add(Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span>Lists<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">ListTemplate</span><span style="color:#e8e2b7;">.</span>NumberArabicDot);
		list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>NumberStyle <span style="color:#e8e2b7;">=</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">NumberStyle</span><span style="color:#e8e2b7;">.</span>Arabic;
		list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>NumberPosition <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">0</span>;
		list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>TextPosition <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">20</span>;
		list<span style="color:#e8e2b7;">.</span>ListLevels[<span style="color:#ffcd22;">0</span>]<span style="color:#e8e2b7;">.</span>TabPosition <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">20</span>;
		

		<span style="color:#93c763;">string</span>[] listItems <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">new</span> <span style="color:#93c763;">string</span>[]{
			<span style="color:#ec7600;">"<p>First Item </p> <p>With Some more stuff</p>"</span>,
			<span style="color:#ec7600;">"<p>Second Item </p>"</span>,
			<span style="color:#ec7600;">"<p>Last Item </p>"</span>
		};

		
		<span style="color:#678cb1;">Regex</span> pTag <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">new</span> <span style="color:#678cb1;">Regex</span>(<span style="color:#ec7600;">"<p>"</span>, <span style="color:#678cb1;">RegexOptions</span><span style="color:#e8e2b7;">.</span>IgnoreCase);
		<span style="color:#678cb1;">Regex</span> pEndTag <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">new</span> <span style="color:#678cb1;">Regex</span>(<span style="color:#ec7600;">"</p>"</span>, <span style="color:#678cb1;">RegexOptions</span><span style="color:#e8e2b7;">.</span>IgnoreCase);
					
		<span style="color:#93c763;">foreach</span> (<span style="color:#93c763;">var</span> rawRec <span style="color:#93c763;">in</span> listItems)
		{
			<span style="color:lime;">//replace the P tags - this fixes the indentation getting reset within the list</span>
			<span style="color:#93c763;">string</span> rec <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">string</span><span style="color:#e8e2b7;">.</span>Empty;
			rec <span style="color:#e8e2b7;">=</span> pTag<span style="color:#e8e2b7;">.</span>Replace(rawRec, <span style="color:#ec7600;">""</span>);
			rec <span style="color:#e8e2b7;">=</span> pEndTag<span style="color:#e8e2b7;">.</span>Replace(rec, <span style="color:#ec7600;">"\x0b"</span>); <span style="color:lime;">//The MS Word character for new line - \r\n doesn't always work</span>

			<span style="color:lime;">//add in a group header</span>
			Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Paragraph</span> headerPara <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">new</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Paragraph</span>(doc);
			Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Run</span> headerRun <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">new</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Run</span>(doc);
			headerRun<span style="color:#e8e2b7;">.</span>Text <span style="color:#e8e2b7;">=</span> <span style="color:#ec7600;">"The Form Name Here"</span>;
			headerPara<span style="color:#e8e2b7;">.</span>AppendChild(headerRun);
			bookmark<span style="color:#e8e2b7;">.</span>BookmarkStart<span style="color:#e8e2b7;">.</span>ParentNode<span style="color:#e8e2b7;">.</span>ParentNode<span style="color:#e8e2b7;">.</span>InsertBefore(headerPara, bookmark<span style="color:#e8e2b7;">.</span>BookmarkStart<span style="color:#e8e2b7;">.</span>ParentNode);
			
			<span style="color:lime;">//prep the paragraph that the rec item will go</span>
			Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Paragraph</span> recPara <span style="color:#e8e2b7;">=</span> <span style="color:#93c763;">new</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Paragraph</span>(doc);
			recPara<span style="color:#e8e2b7;">.</span>ParagraphFormat<span style="color:#e8e2b7;">.</span>StyleIdentifier <span style="color:#e8e2b7;">=</span> Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">StyleIdentifier</span><span style="color:#e8e2b7;">.</span>Normal;
			bookmark<span style="color:#e8e2b7;">.</span>BookmarkStart<span style="color:#e8e2b7;">.</span>ParentNode<span style="color:#e8e2b7;">.</span>ParentNode<span style="color:#e8e2b7;">.</span>InsertBefore(recPara, bookmark<span style="color:#e8e2b7;">.</span>BookmarkStart<span style="color:#e8e2b7;">.</span>ParentNode);

			Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">Section</span> thisSection <span style="color:#e8e2b7;">=</span> GetParentSection(recPara);
			<span style="color:#93c763;">var</span> sectionParas <span style="color:#e8e2b7;">=</span> thisSection<span style="color:#e8e2b7;">.</span>GetChildNodes(Aspose<span style="color:#e8e2b7;">.</span>Words<span style="color:#e8e2b7;">.</span><span style="color:#678cb1;">NodeType</span><span style="color:#e8e2b7;">.</span>Paragraph, <span style="color:#93c763;">true</span>);
			builder<span style="color:#e8e2b7;">.</span>MoveToParagraph(sectionParas<span style="color:#e8e2b7;">.</span>IndexOf(recPara), <span style="color:#ffcd22;">0</span>);
			builder<span style="color:#e8e2b7;">.</span>ListFormat<span style="color:#e8e2b7;">.</span>List <span style="color:#e8e2b7;">=</span> list;	
			
			<span style="color:lime;">//append an empty string to the rec- this magically forces the numbered list to work. Any character will do, and this one is invisible</span>
			builder<span style="color:#e8e2b7;">.</span>InsertHtml(<span style="color:#ec7600;">"&nbsp;"</span> <span style="color:#e8e2b7;">+</span> rec);
			recPara<span style="color:#e8e2b7;">.</span>ParagraphFormat<span style="color:#e8e2b7;">.</span>LeftIndent <span style="color:#e8e2b7;">=</span> <span style="color:#ffcd22;">70</span>;</pre></div><div><br></div><div>Here's what my output looks like:</div><div><p class="MsoNormal">Stuff</p>

Your Recs:

The Form Name

1. First Item
With Some more stuff

The Form Name

2. Second Item

The Form Name

3. Last Item

Recs

Other stuff



Thanks
<!–[if gte mso 10]> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Malgun Gothic"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;}

<![endif]–>

Hello Jason,

Thank you for additional information. Your HTML contains

tags. The text between these tags is represented as separate paragraphs after inserting HTML. So if would like to insert HTML into the list item (list item is a paragraph in Word document) you should avoid using tags which are represented as paragraphs after inserting HTML. This is not a bug this is expected behavior.

Best regards,