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

Free Support Forum - aspose.com

Wrong indentation when merging documents

Hi,

Let me try to give you a brief background of our requirement. Our use case is to create document and then split the document in single page document and then merge the splitted documents and convert them to html and send it to the user. This is done to provide pagination .

Now the issue is that when we merge the documents , the indentation/list numbering is incorrect . Numbering in single page documents is correct , but when we merge , the numbering is incorrect. Attaching the main document , single page documents and screen-shot of the incorrect numbering.

Code to split the document :

LayoutCollector layoutCollector = new LayoutCollector(document);

    DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector);

    Integer priviousListLevel = null;
    for (int page = 1; page <= document.getPageCount(); page++) {
        Document newDoc = splitter.getDocumentOfPage(page);
        newDoc.updateListLabels();

        if (priviousListLevel != null) {

            for (Paragraph para : (Iterable<Paragraph>) newDoc.getChildNodes(NodeType.PARAGRAPH, true)) {
                if (para.isListItem()) {
                    com.aspose.words.List list = para.getListFormat().getList();
                    list.getListLevels().get(0).setStartAt(priviousListLevel + 1);
                    break;
                }
            }
        }

        priviousListLevel = null;
        int labelValue = 0;
        Node[] nodes = newDoc.getChildNodes(NodeType.PARAGRAPH, true)
                .toArray();

        for (int i = nodes.length - 1; i >= 0; i--) {
            Paragraph paragraph = (Paragraph) nodes[i];
            if (paragraph.getListFormat().isListItem() && paragraph.getListFormat().getListLevelNumber() == 0) {
                ListLabel label = paragraph.getListLabel();
                labelValue = label.getLabelValue();
                priviousListLevel = labelValue;
                break;
            }
        }

        newDoc.getRange().replace(AdminTemplateConstants.TAG_IDENTIFIER_PATTERN, new ReplaceEvaluator(false, templateStyle.getGeneralFont()), false);
        newDoc.save(folder + "/" + page + AdminTemplateConstants.DOC_EXTENSION);
    }

Code for merging document :

for (Document sourceDocument : documentsToMerge) {
com.aspose.words.List list = null;

        for (Paragraph para : (Iterable<Paragraph>) startdocument.getChildNodes(
                NodeType.PARAGRAPH, true)) {
            if (para.isListItem() && para.getListFormat().getListLevelNumber() == 0) {
                list = para.getListFormat().getList();
                break;
            }
        }
        if (list != null) {
            com.aspose.words.List newList = sourceDocument.getLists().addCopy(list);
            for (Paragraph para : (Iterable<Paragraph>) sourceDocument.getChildNodes(
                    NodeType.PARAGRAPH, true)) {
                if (para.isListItem() && para.getListFormat().getListLevelNumber() == 0) {
                    para.getListFormat().setList(newList);
                }
            }
        }
        startdocument.appendDocument(sourceDocument, ImportFormatMode.USE_DESTINATION_STYLES);
    }


Code for converting doc to html

HtmlFixedSaveOptions saveOptions = new HtmlFixedSaveOptions();
saveOptions.setExportEmbeddedCss(true);
saveOptions.setExportEmbeddedFonts(true);
saveOptions.setExportEmbeddedImages(true);
saveOptions.setExportEmbeddedSvg(true);
saveOptions.setExportFormFields(true);
saveOptions.setEncoding(StandardCharsets.ISO_8859_1);
return saveOptions;

We are really struck at that. Please help

Documents.zip (191.9 KB)

@saurabh.arora,

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

  • Your input Word document. The 6.docx has only one page and 7.docx is empty document.
  • Please attach the output Word/HTML file that shows the undesired behavior.
  • Please attach the expected output Word/HTML file that shows the desired behavior.
  • Please create a simple 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’ll start investigation into your issue and provide you more information. Thanks for your cooperation.

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

Hi Tahir,

I was able to solve the problem by making a small code change.

com.aspose.words.List list = null;
for (Document sourceDocument : documentsToMerge) {

        for (Paragraph para : (Iterable<Paragraph>) startdocument.getChildNodes(
                NodeType.PARAGRAPH, true)) {
            if (para.isListItem()) {
                list = para.getListFormat().getList();
                break;
            }
        }
        if (list != null) {
            com.aspose.words.List newList = sourceDocument.getLists().addCopy(list);
            for (Paragraph para : (Iterable<Paragraph>) sourceDocument.getChildNodes(
                    NodeType.PARAGRAPH, true)) {
                if (para.isListItem()) {
                    para.getListFormat().setList(newList);
                }
            }
        }
        startdocument.appendDocument(sourceDocument, ImportFormatMode.USE_DESTINATION_STYLES);
    }
}

@saurabh.arora,

It is nice to hear from you that you have resolved your query. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.