List items with different indent

Hello,

I’m trying to create a document similar to the attached, however when I change the NumberPosition via:
para->ListFormat->ListLevel->NumberPosition = 20;

It changes the NumberPosition for the entire list. Is there a way to recreate the document attached (with the numbers at different indents but still in the same list?)

Hi Simon,

Thanks for your inquiry. Please use ParagraphFormat.LeftIndent property to get or set the value (in points) that represents the left indent for paragraph. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
List list = doc.Lists.Add(ListTemplate.NumberArabicDot);
builder.ListFormat.List = list;
builder.ParagraphFormat.LeftIndent = 36;
builder.Writeln("Some text");
builder.ParagraphFormat.LeftIndent = 50;
builder.Writeln("Some text");
builder.ParagraphFormat.LeftIndent = 70;
builder.Writeln("Some text");
// This is a way to stop list formatting. 
builder.ListFormat.List = null;
doc.Save(MyDir + "Out.docx");

Ok so this code (its C++CLI)

Words::Document^ doc = gcnew Words::Document();
Words::DocumentBuilder^ builder = gcnew Words::DocumentBuilder(doc);
Words::Lists::List^ list = doc->Lists->Add(Words::Lists::ListTemplate::NumberArabicDot);
list->ListLevels[0]->NumberPosition = 40;
builder->ListFormat->List = list;
builder->ParagraphFormat->LeftIndent = 50;
builder->Writeln("1. Some text");
builder->ParagraphFormat->LeftIndent = 70;
builder->Writeln("2. Some text");
builder->ParagraphFormat->LeftIndent = 36;
builder->Writeln("3. Some text");
doc->Save(R"(C:\Out.docx)");

Does exactly what I want. However I try to modify paragraphs that are already there, I get some pretty weird results when opened in Word (certainly not the same as when I create the content with the DocumentBuilder). Unfortunately the problem I’m solving involves changing existing paragraphs, rather than building new ones with the DocumentBuilder.

Words::Document^ doc = gcnew Words::Document();
Words::DocumentBuilder^ builder = gcnew Words::DocumentBuilder(doc);
Words::Lists::List^ list = doc->Lists->Add(Words::Lists::ListTemplate::NumberArabicDot);
builder->ListFormat->List = list;
builder->Writeln("1. Some text");
builder->Writeln("2. Some text");
builder->Writeln("3. Some text");
// This is a way to stop list formatting.
builder->ListFormat->List = nullptr;

auto paras = doc->GetChildNodes(Words::NodeType::Paragraph, true);
// Note para0 is some random paragraph
auto para1 = static_castWords::Paragraph^(paras[1]);
auto para2 = static_castWords::Paragraph^(paras[2]);
auto para3 = static_castWords::Paragraph^(paras[3]);
para1->ListFormat->ListLevel->NumberPosition = 40;
para1->ParagraphFormat->LeftIndent = 50;
para2->ParagraphFormat->LeftIndent = 70;
para3->ParagraphFormat->LeftIndent = 36;
doc->Save(R"(C:\Out.docx)");

Can you please inform me how I can change the code which modifies the existing document so that it looks like the one generated by the DocumentBuilder version?

Hi Simon,

Thanks for your inquiry. Could you please share your input and expected output documents here for our reference? Please manually create your expected Word document using Microsoft Word. We will then provide you code example according to your requirement. If “Hello World.docx” is your input document, please only share expected output document.

Attached is my In.docx and Out.docx. I’m trying to modify each item to get it to change the indent of that particular list item. More generally I’m trying to work out what the relationship between the following are:

ListLevel.NumberPosition
ListLevel.TabPosition
ParagraphFormat.LeftIndent

I think I’ve actually worked it out:

int para1desired = 50;
int para2desired = 70;
int para3desired = 36;
para1->ParagraphFormat->LeftIndent = para1desired - para1->ListFormat->ListLevel->NumberPosition;
para2->ParagraphFormat->LeftIndent = para2desired - para2->ListFormat->ListLevel->NumberPosition;
para3->ParagraphFormat->LeftIndent = para3desired - para3->ListFormat->ListLevel->NumberPosition;

Seems to give me the right values.

So if I set the NumberPosition to 70, then the numbers will be at 70, but I can actually modify the position of each number using the paragraph’s LeftIndent. Word essentially goes “Position this at NumberPosition + LeftIndent + FirstLineIndent” or something like that. Is that correct?

Hi Simon,

Thanks for your inquiry.

The ListLevel.NumberPosition property gets or sets the position (in points) of the number or bullet for the list level. NumberPosition corresponds to LeftIndent plus FirstLineIndent of the paragraph.

The ListLevel.TextPosition property gets or sets the position (in points) for the second line of wrapping text for the list level. TextPosition corresponds to LeftIndent of the paragraph.

You can change left indentation of list items using ParagraphFormat.LeftIndent as shown below.

Document doc1 = new Document(MyDir + "in.docx");
Paragraph para2 = (Paragraph)doc1.GetChild(NodeType.Paragraph, 1, true);
para2.ParagraphFormat.LeftIndent += 80.5;
Paragraph para3 = (Paragraph)doc1.GetChild(NodeType.Paragraph, 2, true);
para3.ParagraphFormat.LeftIndent -= 13.5;
para3.ParagraphFormat.FirstLineIndent = -9;
doc1.Save(MyDir + "17.4.docx");