Reset paragraph formatting without loosing information about list type and level

Hi,

we are creating list programmatically and dynamically (based on html input - but we cannot use convert() method to convert from html to word as it does not suit our needs)
i am testing on following list:
<ul style=“color: red;”>
<li>Coffee
<li style=“color: blue;border: 1pt solid red”>Tea
<li>Milk
</ul>

we are building it like this:

        Document doc = new Document();

        DocumentBuilder builder = new DocumentBuilder(doc);

        builder.ListFormat.ApplyNumberDefault();

        builder.Font.Color = Color.Red;
        builder.Write("jeden");
        builder.InsertParagraph();

        builder.ParagraphFormat.ClearFormatting();
        builder.Font.ClearFormatting();

        builder.Font.Color = Color.Blue;
        builder.ParagraphFormat.Borders.Color = Color.Red;
        builder.ParagraphFormat.Borders.LineStyle = LineStyle.Single;
        builder.ParagraphFormat.Borders.LineWidth = 1;
        builder.Write("dwa");
        builder.InsertParagraph();

        builder.ParagraphFormat.ClearFormatting();
        builder.Font.ClearFormatting();

        builder.Font.Color = Color.Red;
        builder.Write("trzy");
        builder.InsertParagraph();

        builder.ListFormat.RemoveNumbers();


        doc.Save(@"OutputFiles\testText.docx");

Unfortunatelly after applying paragraphFormat.ClearFormatting() we are loosing all information about list level and type.
if we don’t clear formatting, in cases as above we keep the border formatting which is not intended.
Please note that the above is just an example of many possible combinations we could get and we can’t solve each case separately. instead we need some other way of either keeping info about list when performing paragrafFormat clean OR a way to re-apply list info to continue adding list items with correct numbering/on correct level

Thanks
Aga

@acturisaspose,

Thanks for your inquiry. Please use the following code example to reset the same list to another paragraph. Hope this helps you.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.ApplyNumberDefault();
//Get the current list
List list = builder.ListFormat.List;

builder.Font.Color = Color.Red;
builder.Write("jeden");
builder.InsertParagraph();

builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();

builder.Font.Color = Color.Blue;
builder.ParagraphFormat.Borders.Color = Color.Red;
builder.ParagraphFormat.Borders.LineStyle = LineStyle.Single;
builder.ParagraphFormat.Borders.LineWidth = 1;
builder.Write("dwa");
builder.InsertParagraph();

builder.ParagraphFormat.ClearFormatting();
builder.Font.ClearFormatting();

builder.Font.Color = Color.Red;
//Set the list
builder.ListFormat.List = list;
builder.Write("trzy");
builder.InsertParagraph();

builder.ListFormat.RemoveNumbers();

doc.Save(MyDir + "18.6.docx");

Hi,

thanks for the reply - it works for me.

thanks
Aga

AsposeList.zip (19.8 KB)

Hello,

i have another question about the way lists works in Aspose. I am adding a list as in attached example (please note that this is simplified code - in reality i am adding lists dynamically based on some input) and as you can see in the Output file i am getting additional break line at the end of the file. Is there any chance to avoid this break line (as in ExpectedOutput)?

Thanks
Aga

@acturisaspose,

Thanks for your inquiry. Please use following code example to get the desired output.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.ApplyNumberDefault();

builder.Writeln("test");
builder.Writeln("test");
builder.Writeln("test");

builder.ListFormat.RemoveNumbers();

if (doc.LastSection.Body.LastParagraph.ToString(SaveFormat.Text).Trim() == "")
    doc.LastSection.Body.LastParagraph.Remove();

doc.Save(MyDir + "output.docx");

Hi Tahir,

as i have mentioned above my example is just a simplified code. We are creating our documents dynamically based on some unknown input and the part of code responsible for list creation cannot tell whether the list will be the last object in the section/document

This solution is not really fixing the issue - it seems to only be fixing symptoms.
Also it won’t be suitable for our case - there are cases where our input documents would actually need to have empty paragraphs at the end of the section and we can’t just blindly remove them all.

Is there any way of actually fixing Lists so the issue is not occurring?

Thanks
Aga

@acturisaspose,

Thanks for your inquiry. You are facing expected behavior of Aspose.Words. The DocumentBuilder.Writeln inserts the string and paragraph break into document. Please use DocumentBuilder.Write method for last list number and do not use ListFormat.RemoveNumbers method.

You can use one of the following code example to get the desired output. Hope this helps you.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.ApplyNumberDefault();

builder.Writeln("test");
builder.Writeln("test");
builder.Write("test");
                  
doc.Save(MyDir + "18.6.docx");

Document doc = new Document();
List list = doc.Lists.Add(ListTemplate.NumberDefault);

Paragraph para = doc.FirstSection.Body.FirstParagraph;
para.ListFormat.List = list;
para.AppendChild(new Run(doc, "test1"));

para = new Paragraph(doc);
para.ListFormat.List = list;
para.AppendChild(new Run(doc, "test2"));

doc.FirstSection.Body.AppendChild(para);

para = new Paragraph(doc);
para.ListFormat.List = list;
para.AppendChild(new Run(doc, "test3"));

doc.FirstSection.Body.AppendChild(para);
 
doc.Save(MyDir + "18.6.docx");

hi,

one more question regarding lists:

if i create a nested bullet list like that:

        Document doc = new Document();

        DocumentBuilder builder = new DocumentBuilder(doc);

        builder.ListFormat.ApplyNumberDefault();

        builder.Writeln("test1");

        builder.Writeln("test2");

        builder.ListFormat.ListLevelNumber += 1;

        builder.ListFormat.List = builder.ListFormat.List.Document.Lists.Add(ListTemplate.NumberDefault);
        builder.ListFormat.ListLevel.NumberStyle = NumberStyle.Arabic;

        builder.Writeln("lvl2test1");

        builder.Writeln("lvl2test2");
        builder.Writeln("lvl2test3");
        builder.ListFormat.ListLevelNumber -= 1;

        builder.Writeln("test3");

        builder.Writeln("test4");

        builder.ListFormat.RemoveNumbers();


        doc.Save(@"OutputFiles\testText.docx");

why list numbering is output like that:

1 test1
2 test2
1 lvl2test1
2 lvl2test2
3 lvl2test3
2 test3
3 test4

(ie. test3i item is labeled as 2 not 3)?

thanks
aga

@acturisaspose,

Thanks for your inquiry. Please use the following modified code example to get the desired output.

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

builder.ListFormat.ApplyNumberDefault();
List list = builder.ListFormat.List;
builder.Writeln("test1");

builder.Writeln("test2");

builder.ListFormat.ListLevelNumber += 1;

builder.ListFormat.List = builder.ListFormat.List.Document.Lists.Add(ListTemplate.NumberDefault);
builder.ListFormat.ListLevel.NumberStyle = NumberStyle.Arabic;

builder.Writeln("lvl2test1");

builder.Writeln("lvl2test2");
builder.Writeln("lvl2test3");
builder.ListFormat.ListLevelNumber -= 1;

builder.ListFormat.List = list;
builder.Writeln("test3");

builder.Writeln("test4");

builder.ListFormat.RemoveNumbers();
                  
doc.Save(MyDir + "18.6.docx");