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

Free Support Forum - aspose.com

How to rest numbering between lists when merging documents with Aspose?

I have a JAVA program that is used Aspose.

Something similar to the following code:

Document doc1 = new Document(@"Test001\doc1.doc");
Document doc2 = new Document(@"Test001\doc2.doc");
doc1.AppendDocument(doc2, ImportFormatMode.UseDestinationStyles);
doc1.Save(@"Test001\out.doc");

Despite what was explained here, I want to reset numbering as soon as I attach the second document.

How can I achieve this?

Please note that I want to generate the destination file by merging some documents.

@mjz,

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

  • Your simplified input documents
  • Aspose.Words 18.12 generated output document showing the undesired behavior
  • Your expected document showing the correct output. You can create expected document by using MS Word. Please list complete Steps that you used in MS Word to create expected document.

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

Aspose.zip (415.7 KB)
Hi there.

Please check the attached file.

The code that attach the files is here:

protected void importDocuments(String directory) throws IOException {
    AtomicReference<Boolean> isFirstSection = new AtomicReference<>(true);

    Files.walk(Paths.get(directory))
            .filter(foundPath -> foundPath.toString().endsWith(".docx"))
            .sorted()
            .forEach((Path path) -> {

                System.out.println("INFO: Input file: " + path.toString());
                try {
                    Document chapterDocument = new Document(path.toString());

                    if (isFirstSection.get()) {
                        chapterDocument.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
                        isFirstSection.set(false);
                    }

                    for(com.aspose.words.Section section : chapterDocument.getSections()) {
                        section.getHeadersFooters().clear();
                    }

                    document.appendDocument(chapterDocument, ImportFormatMode.USE_DESTINATION_STYLES);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
}

Thanks.

@mjz,

There are ‘OUT.docx’ and ‘Expected.docx’ documents that you have shared. What is the difference between them? It would be great if you please also provide a comparison screenshot highlighting the problematic areas in Aspose.Words generated ‘OUT.docx’ file with respect to your ‘Expected.docx’ and attach it here for our reference. Thanks for your cooperation.

Capture.PNG (118.0 KB)

Please look at the attached picture.
The right hand side is expected and the left hand side output of the Aspose.
We expect by adding each document, the numbering is reset.

So I need some kind of command like the following that I call before some appendDocument function and reset the numbering.

document.resetNumbering(); // for example

@mjz,

After appending documents, please use the following code:

C#:

foreach (List list in doc.Lists)
    list.IsRestartAtEachSection = true;

Java:

for (com.aspose.words.List list : (Iterable<com.aspose.words.List>) doc.getLists())
    list.isRestartAtEachSection(true);

Hope, this helps.

Capture.PNG (20.2 KB)

@awais.hafeez

Sorry, but this is not the answer, I want to reset the numbering on a specific positions. While you set for the list type to reset the number in every new section. Just look at the picture.
I expect the numbering continues under I, II, III, …
While at the beginning of each of these sections the interior list reset.

@mjz,

Please see attached documents: sample documents.zip (19.9 KB)

The attached List.docx document contains 10 list items of the same list and if you need to reset numbering from 5th element for example, then please use the following code to achieve this:

Document doc = new Document("E:\\temp\\list.docx");

Console.WriteLine(doc.Lists.Count);

// The document contains 10 paragraphs (list items)
// Get first paragraph (first list item)
Paragraph firstItem = doc.FirstSection.Body.FirstParagraph;

// Create copy of list
List listCopy = doc.Lists.AddCopy(firstItem.ListFormat.List);

// Get 5th element
Paragraph item = doc.FirstSection.Body.Paragraphs[4];
// Make the remaining list items part of new List
while (item.IsListItem)
{
    item.ListFormat.List = listCopy;
    item = item.NextSibling as Paragraph;
    if (item == null)
        break;
}

Console.WriteLine(doc.Lists.Count);

doc.Save("E:\\temp\\18.12.docx");

Hi @awais.hafeez

Unfortunately it was not my answer as it does not take care of the nested paragraphs, but thanks for guidance.

I reflect the fully functional answer here:

protected void importDocuments(String directory) throws IOException {
    AtomicReference<Boolean> isFirstSection = new AtomicReference<>(true);

    Files.walk(Paths.get(directory))
            .filter(foundPath -> foundPath.toString().endsWith(".docx"))
            .sorted()
            .forEach((Path path) -> {

                System.out.println("INFO: Input file: " + path.toString());
                try {
                    Document chapterDocument = new Document(path.toString());

                    if (isFirstSection.get()) {
                        chapterDocument.getFirstSection().getPageSetup().setSectionStart(SectionStart.CONTINUOUS);
                        isFirstSection.set(false);
                    }

                    for(com.aspose.words.Section section : chapterDocument.getSections()) {
                        section.getHeadersFooters().clear();
                    }
                    document.appendDocument(chapterDocument, ImportFormatMode.USE_DESTINATION_STYLES);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
    NodeCollection paragraphs = document.getChildNodes(NodeType.PARAGRAPH,true);
    ArrayList<Paragraph> firstParagraphs = new ArrayList<Paragraph>();
    ArrayList<Paragraph> sibilingParagraphs = new ArrayList<Paragraph>();
    boolean mustReset = false;
    boolean recordSibilings = false;
    for(Paragraph paragraph : (Iterable<Paragraph>) paragraphs){
        if(paragraph.isListItem()){
            if(paragraph.getListFormat().getListLevelNumber() == 0 &&
                    (int) paragraph.getListFormat().getListLevel().getNumberFormat().charAt(0) == 0) {
                mustReset = true;
            }
            if(paragraph.getListFormat().getListLevelNumber() == 1) {
                System.out.println(paragraph.getText());
                if (mustReset) {
                    firstParagraphs.add(paragraph);
                    sibilingParagraphs.clear();

                    recordSibilings = true;
                    mustReset = false;
                }
                if(recordSibilings){
                    sibilingParagraphs.add(paragraph);
                }
            }
        }
    }
    if(firstParagraphs.size() > 1){
        Paragraph para = firstParagraphs.get(firstParagraphs.size()-1);
        // Create copy of list
        com.aspose.words.List listCopy = document.getLists().addCopy(para.getListFormat().getList());
        for(Node node : sibilingParagraphs){
            while (true)
            {
                try {
                    para = (com.aspose.words.Paragraph) node;
                    if (para.isListItem() && para.getListFormat().getListLevelNumber() != 0) {
                        para.getListFormat().setList(listCopy);
                    }
                } catch (Exception e) {
                    System.err.println(e.getMessage());
                }
                node =  node.getNextSibling();
                if(node == null)
                    break;
            }
        }

    }
}

@mjz,

It is great that you were able to resolve this issue on your end. Please let us know any time you have any further queries.