Headings Numbering after splitting documents is starting by 1

Hi,

we use aspose word to extract some content within a word document and insert this content into a new empty document. In this association the headings numbering when inserting the extacted content into the new empty document is starting with number 1 although the base document has got other heading numbers.

I have attached the Code of the class “FindandSplitDocument.java”. You only have to set the input word file (=> modify variable “docToSplit”) and where to save the new documents which contains the extacted content (this class extracts the content and creates new documents with the extracted content). We make usage of the “IReplacingCallback” interface for finding the search string. If a special keystring is founded then everything will be extracted till to the next occurence of the search string. This works without any problem.

But if headings are inside the extracted content then the new document where the content will be inserted does not take the heading number of the base document but starts with one.

For better understanding I have added an input file called “Unterlagen_Westernacher_04_CLH_Report_Formatvorlage_201005_mit_trennzeichen.docx”. This contains some headings. Each heading has got its one heading number. When you run the class you get the result of the new splitted documents with the extacted content (see public static void main method where the splitted docs are saved. Please modify the path to the directory where to store the new docs).

If you take a look to the source file of whom we extract the content (=>“Unterlagen_Westernacher_04_CLH_Report_Formatvorlage_201005_mit_trennzeichen.docx”) you see on site 6 that the chapter 2 is starting. After running the class this chapter is saved inside the new document called “Out_3.docx”. But the chapter number is starting with one and not two. The same problem for all other chapters in the new documents where the extracted content is inserted/copied. They always start with one. How can we take the same chapter number as it was in the base document?

When the chapter numbers are inside the new documents correct then all the splitted documents will be merged back into one new document and therefore the chapter numbers must be correct and not be “1” for all chapters. The merge code is also inside the main method at the last part.

What is the best way to have the same chapter numbers inside the new documents?

Thank you very much for your help.

Best regards,
Amin

Hi Amin,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you extract the contents (with list numbers) by using MS Word and copy these contents into new document, you will get the same output.

Regarding list number issue after joining the documents, I suggest you please
read following documentation link. The example shared at following
link describes how to append a document using destination styles and
preventing any list numbering from continuing on.
https://docs.aspose.com/words/java/working-with-lists/

Hope this helps you. Please let us know if you have any more queries.

Hi Tahir,

thanks for your response. It is not working for us. We use chapters. Maybe it works with lists, but for us it is not working with chapters.

Is there a possibility to call the word method for continue chapter numbers after the documents are merged?

Hi Amin,

Thanks for your inquiry.

When you copy content from one document to another and your source and destination documents contain 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.

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.

Hi Tahir,

thank you very much for your answer. Could you tell me, how to use styles to tell word to continue chapter numbers ?

Thank you,
Amin

Hi Amin,

Thanks for your inquiry. You can do the continues numbering by setting the same com.aspose.words.List to all paragraphs in a document which are the items of bulleted or numbered list. For example, following code example shows how to set same list object to all list items (paragraphs). Please check this code example with your Out_2.docx and Out_3.docx documents.

com.aspose.words.List list = null;
Document doc = new Document(MyDir + "doc1.docx");
Document doc2 = new Document(MyDir + "doc2.docx");
doc2.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
doc.appendDocument(doc2, ImportFormatMode.KEEP_SOURCE_FORMATTING);
Boolean blnFirstList = true;
for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if (para.isListItem() && blnFirstList == true)
    {
        list = para.getListFormat().getList();
        blnFirstList = false;
    }
    else if (para.isListItem() && blnFirstList == false)
    {
        para.getListFormat().setList(list);
    }
}
doc.save(MyDir + "Out.docx");

Hi Tahir,

perfect, great Job, is working now! I thank you very much for your great help!

Best regards,
Amin

Hi Amin,

Thanks for your feedback.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

Hi Tahir,
Do you know : how could I apply this solution for .NET ?
Best regards

@peter.peterson,
Thanks for your inquiry. Please check the following .NET code example. Hope this helps you.

List list = null;
Document doc = new Document(MyDir + "doc1.docx");
Document doc2 = new Document(MyDir + "doc2.docx");
doc2.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
doc.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);
Boolean blnFirstList = true;

foreach (Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.IsListItem && blnFirstList == true)
    {
        list = para.ListFormat.List;
        blnFirstList = false;
    }
    else if (para.IsListItem && blnFirstList == false)
    {
        para.ListFormat.List = list;
    }
}

doc.Save(MyDir + "18.4.docx");

Thank @tahir.manzoor
I tried to apply the solution to my numbering problem but it didn’t work.
Could you please have a look at this topic : Heading numbering isn’t copied after styles copying
Thanks

@peter.peterson,
Thanks for your inquiry. We have answered your query here in this post. Please follow that thread for further proceedings.

1 Like