Using document as a template: heading numbering lost

The document body is hardcoded and then inserted into a template document with contains cover, summary, headers and styles. Heading styles are numbered 1, 1.1, 1.2, and so on. But to insert a heading just with ‘Heading [n]’ style does not work, numbering is lost. I think this happens because numbering is set through a multilevel list with headings attached.

Question: is it possible to use a document as a template without coding any formatting, or it is inevitable to deal with list styles in the code?

Using Aspose Words 16.1.0 for Java

@vvvilela

Yes, you can use a document as a template without any formatting. Please note that when you copy nodes from one document to another, this option specifies how formatting is resolved when both documents have a style with the same name, but different formatting.

The formatting is resolved as follows:

  • Built-in styles are matched using their locale independent style
    identifier. User defined styles are matched using case-sensitive
    style name.
  • If a matching style is not found in the destination document, the
    style (and all styles referenced by it) are copied into the
    destination document and the imported nodes are updated to reference
    the new style.
  • If a matching style already exists in the destination document, what
    happens depends on the importFormatMode parameter passed to
    Document.ImportNode as described below.

When using the UseDestinationStyles option, if a matching style already exists in the destination document, the style is not copied and the imported nodes are updated to reference the existing style.

So, in your case, I suggest you please use UseDestinationStyles option while inserting one document into another.

If you still face any issue, please ZIP and attach your input, output and expected output documents. We will then provide you more information about your query along with code example.

Thank you for the quick answer. In fact I don’t use two documents. At first I generate text within Java. Then I read a document through DocumentBuilder. The text in the internal format is then converted and inserted in the document through DocumentBuilder or directly.

To insert a heading, I use the locale-independent style identifier. What I don’t know how to do is to set a heading style of a new paragraph in a way that preserves heading numbering set in the template. An original heading in the document stays numbered, while an inserted heading with the same style has no numbering.

@vvvilela

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a sample Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.