We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Numbered sections

Can you help?

Is there an example of creating a word document with sections (ie numbered heading styles) and table of contents?

Alternatively could you outline how to create numbered sections in a word document.

I would like to create a document something like:-

TOC

  1. Section One

Text

1.1 Section OneOne

text

1.2 Section OneTwo

text

  1. Section Two

Text

2.1 Section OneOne

text

2.2 Section OneTwo

text

Hi,

Thank you for considering Aspose.

You probably mean use of outline numbered headings which is not possible at the moment. However, you can easily use regular headings by implementing something like this:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("TOC"); 
builder.InsertTableOfContents("\o "1-3" \h \z \u");
builder.Writeln(String.Empty);
builder.ParagraphFormat.StyleName = "Heading 1";
builder.Writeln("1. Section One");
builder.ParagraphFormat.StyleName = "Normal";
builder.ParagraphFormat.LeftIndent = 20;
builder.Writeln("Text");
builder.ParagraphFormat.StyleName = "Heading 2";
builder.Writeln("1.1 Section OneOne");
builder.ParagraphFormat.StyleName = "Normal";
builder.Writeln("Text");
builder.ParagraphFormat.StyleName = "Heading 2";
builder.Writeln("1.2 Section OneTwo");
builder.ParagraphFormat.StyleName = "Normal";
builder.Writeln("Text");
builder.ParagraphFormat.LeftIndent = 0;
builder.ParagraphFormat.StyleName = "Heading 1";
builder.Writeln("2. Section Two");
builder.ParagraphFormat.StyleName = "Normal";
builder.ParagraphFormat.LeftIndent = 20;
builder.Writeln("Text");
builder.ParagraphFormat.StyleName = "Heading 2";
builder.Writeln("2.1 Section TwoOne");
builder.ParagraphFormat.StyleName = "Normal";
builder.Writeln("Text");
builder.ParagraphFormat.StyleName = "Heading 2";
builder.Writeln("2.2 Section TwoTwo");
builder.ParagraphFormat.StyleName = "Normal";
builder.Writeln("Text");
doc.Save(@"D:\SandBox\Result.doc");

Thanks.

Are outline numbered headings going to supported in the near future?

I tried the example but got an exception:-

Style ‘Heading 1’ cannot be found in the stylesheet.

on the following line:-

builder.ParagraphFormat.StyleName = "Heading 1";

do I need to setup anything up to use the headings?
When I create a new document from word, “heading 1” appears.

We will add support for outline numbered lists in the future, but I’m not sure that this will happen anytime soon.

Try to use style identifier instead of style name:

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

If it doesn’t help, please attach the problematic document.

Are numbered sections (outline numbered headings) in a word document supported yet?

Yes, it is probably possible now. We have added a rich list/numbering set of classes to our API since your last request. See code examples related to list-related classes, methods and properties in Aspose.Words documentation.

For example:
https://apireference.aspose.com/words/net/aspose.words.lists/listtemplate

If these examples will be insufficient to create required output then please compose a sample document in MS Word and attach it here. I will then try to write a code example that will create a similar output using Aspose.Words.

Best regards,

Is this how Word implements this?

I need to create a document which can be edited, and allow users to add their own sections which Word will renumber as required.

Sorry, your last post is a bit unclear. Please elaborate.

I would like to create a document something like:-

TOC

  1. Section One

Text

1.1 Section OneOne

text

1.2 Section OneTwo

text

  1. Section Two

Text

2.1 Section OneOne

text

2.2 Section OneTwo

text

When a user inserts a new section 2.1, the other two section should renumber to 2.2 and 2.3.

Would this happen with a document created using the approach you have suggested?

Yes, if you will create the content with proper list numbering, following the guidelines in our list API the TOC will automatically reflect the changes and the chapters will automatically be renumbered when the user will edit the document in MS Word. The only requirement is that the user should assign proper styles to the inserted chapters. Also note that TOC field need to be updated explicitly in MS Word (by pressing F9 or using context menu) to reflect the changes.

The list API is somewhat complex so please let me know if you will have any difficulties on the way. I will try to help you out.

Best regards,

That works well, but a couple of formatting questions.

  1. Using the example, the list numbers have the same format as the section text. When I use the code in the web site, the number appear to have no formatting, but the header text looks fine. When I click on the number style is correct.

  2. I set the numbers to be aligned on the left (using the following code), but the text is still indented, how can I get this to follow directly after the number?

List list = doc.Lists.Add(ListTemplate.OutlineLegal);

Builder.ListFormat.List.ListLevels(Level - 1).NumberPosition = 0

Hi

Please attach the document with problem created using Aspose.Words, and the document without problem created manually in MS Word.

Best regards.

The issues you have found earlier (filed as 565) have been fixed in this update.