Hi all,
If a document contains a section break then the next heading number is restarted with its value set to 1. This occcurs without doing any operation on the document, calling “UpdateFields” is probably enough to reproduce this effect.
See these screenshots:
I also provide two samples to reproduce this problem:
Thanks for reading (and handling)!
Best regards,
JL
Test Headling numbering.docx (79.3 KB)
Test Headling numbering.OUTPUT.docx (76.9 KB)
I forgot to mention the Aspose.Words versions tested: 22.3 and 24.5. So probably all versions till now are concerned.
@jlmorard Unfortunately, I cannot reproduce the problem on my side using the following simple code:
Document doc = new Document(@"C:\Temp\in.docx");
doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");
Here is the produced output: out.docx (75.9 KB)
Thanks Alexey for looking at this. I proof the code and tell you soon which other method we call.
1 Like
While finalizing the document, the document generation service calls these methods:
document.MailMerge.DeleteFields();
// Update the list labels, if any
document.UpdateListLabels();
// UpdateTableLayout is obsolete. Column widths re-calculation are performed automatically before saving the document
// document.UpdateTableLayout();
document.UpdateThumbnail();
document.UpdateFields();
document.UpdatePageLayout();
Does it help to reproduce the problem?
@jlmorard Thank you for additional information. unfortunately, the problem is still not reproducible on my side.
Thank you Alexey, hummmm strange… I will investigate the running code to localize which method causes this impact. You hear from me.
1 Like
Hi Alexey
I investigated the problem further. The problem with the heading numbering occurs as soon as you 1) insert a section break in the document (see the attachment), 2) call the MailMerge.ExecuteWithRegions
or MailMerge.Execute
method. The method document.UpdateFields()
updates the TOC so that the heading numbering becomes visible in the TOC.
Try this code to reproduce the problem (using the attached Docx file):
// Template.Data is of type byte[]
document = new Aspose.Words.Document(new MemoryStream(Template.Data));
DocumentBuilder builder = new DocumentBuilder(document);
// Call ExecuteWithRegions or Execute (same effect)
builder.Document.MailMerge.ExecuteWithRegions(Dataset.Tables[tableSpec]);
After the section break, the heading numbering is resetted to “1”. Note:
- in the previous section, the format is changed to “landscape”, I don’t know (yet) if this could be part of the problem.
MailMerge.ExecuteWithRegions
does “nothing” as threre is no merge field in the document
Thanks for investigating on your side, enjoy your day!
@jlmorard If possible, could you please create a simple console application that will allow us to reproduce the problem? Unfortunately, it is still not clear how to reproduce it on our side.
Hi Alexey
As you requested I created a small .NET console application that reproduces this problem. The number of the chapter 2 is resetted to the value 1 after a section break. See the attached files “TestHeadlingNumbering.docx” (input file) and “TestHeadlingNumbering.Output.docx” (output file after processing).
To run the application copy the input file into the C:\Temp directory.
The .NET code is attached to this reply (Program.zip). If you want to download the all .NET solution, use this link:
https://www.bk.admin.ch/apps/morard/download/AsposeConsoleApp.zip
TestHeadlingNumbering.docx (16.4 KB)
TestHeadlingNumbering.Output.docx (29.0 KB)
Program.zip (1.0 KB)
Thank you for investigating this problem!
Jean-Louis
@jlmorard You can change the code like this:
private static Document Transform(string fin, DataSet ds)
{
byte[] data = File.ReadAllBytes(fin);
Document document = new Document(new MemoryStream(data));
document.MailMerge.ExecuteWithRegions(ds);
foreach (Aspose.Words.Lists.List list in document.Lists)
{
list.IsRestartAtEachSection = false;
}
document.UpdateFields();
return document;
}
to avoid restarting list numbers at each section.
@alexey.noskov Thanks for this solution, it helps. From my point of view, this is a workaround to a problem: Aspose does not respect the Word behaviour. And if the document’s owner actually reset the numbering in a new section, we will have a side effect with your solution.
Will Aspose correct this in a future release of Aspose?
@jlmorard MS Word and AW apply restartNumberingAfterBreak attribute to each list in document during mail merge. In some cases, it is necessary to use backward compatibility during mail merge, which is what this workaround is about. I think that for your case the following solution would be most useful:
Dictionary<Aspose.Words.Lists.List, bool> lists = new Dictionary<Aspose.Words.Lists.List, bool>();
foreach (Aspose.Words.Lists.List list in document.Lists)
lists[list] = list.IsRestartAtEachSection;
document.MailMerge.ExecuteWithRegions(ds);
foreach (KeyValuePair<Aspose.Words.Lists.List, bool> pair in lists)
pair.Key.IsRestartAtEachSection = pair.Value;
@vyacheslav.deryushev @alexey.noskov Thank you for this second proposition. Indeed much better for my use case where I use Apose.Word in a generic service to generate full documents.
I think that we can close this ticket. Thanks again and enjoy the coming weekend!
1 Like