Paragraph numbering and document builder

Hello,
I’m evaluating Aspose for .Net and it seems to suite fine for my needs except for one thing: section/paragraph numbering.
I need to take some arbitrary Word files and join them together, and build a special section with numbering too, and I want to have a consistent TOC.
For example, I could have to join Document1 that is like that:

  1. New York
    1.1 Manhattan

with Document2 that is like

  1. Rome
    1.1 Trastevere
    1.2 Campidoglio

with a Document3 that I generate at runtime and that has section numbering, for example:

  1. London
    1.1 East End

The output should be:

  1. New York

1.1 Manhattan

  1. Rome

2.1 Trastevere

2.2 Campidoglio
3.1. London

3.1 East End

I tried using something like mainDoc.AppendDocument(tDoc, ImportFormatMode.UseDestinationStyles) and it seems to work fine as long as the users use default names like “Heading 1”, but I have problems with the dynamically generated file.
That’s my code (after I got mainDoc by joining the other documents):

Section sectionToAdd = new Section(mainDoc);

mainDoc.Sections.Add(sectionToAdd);

tBuilder = new DocumentBuilder(mainDoc);

tBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

tBuilder.ListFormat.ApplyNumberDefault();

tBuilder.Writeln("London");

tBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;

tBuilder.ListFormat.ListLevelNumber = 2;

tBuilder.ListFormat.ListOutdent();

tBuilder.ListFormat.List = mainDoc.Lists.Add(Aspose.Words.Lists.ListTemplate.NumberArabicDot);

tBuilder.Writeln("East End");

The result is that London is numbered “1” (with a different formatting from the rest of the document) and East End is numbered “a” (and it’s indented).

Is there a way to achieve what I need?

Thank you,
Luca

Hi Luca,
Thanks for your request. Here are explanation for some of your question.
1. Why numbering is not preserved when insert content from one document into another
This probably occurs because you are using Document.ImportNode method instead of Nodeimporter class to copy nodes from one document into another. When you use Document.ImportNode method Aspose.Words create a separate context for each imported node. So if you would like to import several paragraph which are members of same list in the source document and you are using Document.ImportNode, as separate instance of list will be create for each paragraph in the destination document. That is why if you had numbering like 1, 2, 3… in the source document, you will get 1, 1, 1… in the destination document.
To prevent this, you have to use one instance of NodeImporter class to import all nodes from the source document to the destination one. In this case numbering will be retained after copying content.
2. How to continue numbering when concatenate or insert documents
When you copy content from one document to another and your source and destination documents contains lists, Aspose.Words copies list from the source document to the destination. That is why numbering of items will not continue.
If you need that numbering continues. You should apply numbering in source and destination documents using styles and use UseDestinationStyles option upon copying content from one document to another. In this case, if names of styles you applied to the paragraphs are the same, the style from the source document will not be copied into the destination document. The copied paragraphs will use styles from the destination document. And since you applied numbering using paragraph styles, the paragraphs from the destination document and the copied paragraphs will become members of the same list and numbering will continue.
3. Regarding the dynamically generated documents.
To continue numbering you should apply numbering via style, but in your code you applied numbering to a paragraph itself. You should modify your code like shown below:

// Apply numbering to styles.
List list = mainDoc.Lists.Add(ListTemplate.NumberArabicDot);
mainDoc.Styles[StyleIdentifier.Heading1].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading1].ListFormat.ListLevelNumber = 0;
mainDoc.Styles[StyleIdentifier.Heading2].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading2].ListFormat.ListLevelNumber = 1;
mainDoc.Styles[StyleIdentifier.Heading3].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading3].ListFormat.ListLevelNumber = 2;
Section sectionToAdd = new Section(mainDoc);
sectionToAdd.EnsureMinimum();
mainDoc.Sections.Add(sectionToAdd);
DocumentBuilder tBuilder = new DocumentBuilder(mainDoc);
tBuilder.MoveToDocumentEnd();
tBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
tBuilder.Writeln("London");
tBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
tBuilder.Writeln("East End");

Hope this helps.
Best regards,

Hello Alexey,
thank you for your quick response.

I had it working for the numbering in the first level (3. London), but the second level is always "a. East End. ", with a letter.
I tried, to better understand, changing the object list that you defined to ListTemplate.NumberLowercaseLetterParenthesis, and it’s applied only to first level. I tried also to create another List object and applying it to the style:

Aspose.Words.Lists.List list2 = mainDoc.Lists.Add(Aspose.Words.Lists.ListTemplate.NumberArabicDot);
mainDoc.Styles[StyleIdentifier.Heading2].ListFormat.List = list2;

but it’s doesn’t work too, I still get “a. East End”.
What am I missing?

Thank you,
Luca

Hi Luca,
Thanks for your request. You can use code like the following to configure each level of your list:

// Apply numbering to styles.
List list = mainDoc.Lists.Add(ListTemplate.NumberArabicDot);
// here you can configure each level as you need.
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;
mainDoc.Styles[StyleIdentifier.Heading1].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading1].ListFormat.ListLevelNumber = 0;
mainDoc.Styles[StyleIdentifier.Heading2].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading2].ListFormat.ListLevelNumber = 1;
mainDoc.Styles[StyleIdentifier.Heading3].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading3].ListFormat.ListLevelNumber = 2;
Section sectionToAdd = new Section(mainDoc);
sectionToAdd.EnsureMinimum();
mainDoc.Sections.Add(sectionToAdd);
DocumentBuilder tBuilder = new DocumentBuilder(mainDoc);
tBuilder.MoveToDocumentEnd();
tBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
tBuilder.Writeln("London");
tBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
tBuilder.Writeln("East End");

Hope this helps.
Best regards,

Hello Alexey,
I fear it doesn’t work : I got “East End” without any numbering, like this:
3. London
East End

I tried with some combinations of the formatting (by the way, I couldn’t find documentation about the syntax like “\x0001”), and I was able to get some data only putting “\x0001”, without dots, but it just gives one number:
*3. London

  1. East End*

PS: I’m using 2.0 .net version. Is there any difference between other versions?

Hi Luca,
Thank you for additional information. It would be great if you create a simple application that will allow me to reproduce the problem. I will check it and provide you more information.
Best regards,

Hello Alexey,
I’m sending you my test applications. There are other tests, but if you just click on the button you will get an empty documents with the problem I get (i.e. no numbers on second level headers).

Thank you,
Luca

Hi Luca,
Thank you for addition information. But unfortunately, I still cannot reproduce the problem on my side. I further simplified the code:

Document mainDoc = new Document();
DocumentBuilder builder = new DocumentBuilder(mainDoc);
Aspose.Words.Lists.List list = mainDoc.Lists.Add(Aspose.Words.Lists.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;
mainDoc.Styles[StyleIdentifier.Heading1].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading1].ListFormat.ListLevelNumber = 0;
mainDoc.Styles[StyleIdentifier.Heading2].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading2].ListFormat.ListLevelNumber = 1;
mainDoc.Styles[StyleIdentifier.Heading3].ListFormat.List = list;
mainDoc.Styles[StyleIdentifier.Heading3].ListFormat.ListLevelNumber = 2;
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;
builder.Writeln("Functional Details");
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2;
builder.Writeln("East End");
mainDoc.Save(Response, "Aspose.Words.Demos.doc", ContentDisposition.Attachment, SaveOptions.CreateSaveOptions(SaveFormat.Doc));

Could you please try using this code on your side and send the output document back to me.
Best regards,

Hello Alexey,
I include the output generated with your simplified code in a new project and a screenshot of how I see it (in case with different version of Word we see different things: I tested it with Word 2003).

Regards,
Luca

Hi Luca,
Thank you for additional information. It is really odd. The same code produces the expected output on my side. Please see the attached document.
Do you use the latest version of Aspose.Words for testing?
Best regards,

Hi Alexey,
It’s very odd. Even the file you sent me seems to be wrong, this is a screenshot:

I tried opening it with Word 2007 but it’s the same.
I found out that I did not have the latest version, I now have installed 10.2.0 but nothing changed.

Others thing that I tried is to use 2.0 or 3.5 assembly, or to save in rtf or docx, and I get no changes.
Is there anything else I can try?

Hi
Thank you for additional information. I attached a screenshot of the document produced on my side. As you can see it looks as expected on my side. I am not sure why this does not work on your side.
You can try to clear paragraph and list formats before applying the style. Maybe this helps. Also, you can try play with Tab, Text and Number positions of list levels. Maybe this is the case.
Best regards,

Hello Alexey,
I still haven’t managed to find out where the problem is, playing with lists and paragraphs got me nothing.
I have tried sending the generated file around and we get different results. With Word 2003 or Word 2007 we always see “East End”, while with Word2010, Word 2011 for Mac or opening it online with Google Docs I can see “1.1 East End”.
So it seems that it depends on Word version, and it does not fit with our needs. Maybe it has something to do with the syntax of NumberFormat? What does that hex number mean?
I have found out that if I format level 1 with “\x0001.” I get “1. East End”, but as soon as I put “\x0000” or, just to try, “\x0002” the format is lost.

What version have you tried on? Do you have the possibility to try on with Word 2003 and maybe find out what’s wrong with it?

Hi
Thanks for your request. I tried to open the generated document even in MS Word 2000 and in Open Office. Still the output looks correct on my side.
Have you tried to render the document (convert to PDF. XPS or Image)? Does the document looks incorrect when you convert it to PDF?
Best regards,