按页保存word的问题

word如果有表格跨页会自动添加上表头,但是word按页保存后,跨页后的表格并没有带上表头,请问有解决方法吗

这是示例代码
Document doc = new Document(“input.docx”);
DocumentPageSplitter splitter = new DocumentPageSplitter(doc);

    for (int page = 1; page <= doc.getPageCount(); page++) {
        Document pageDoc = splitter.getDocumentOfPage(page);
        pageDoc.save(String.format("Output_Page_%d.docx", page));
    }

@z2631632737, 能附上input.docx和DocumentPageSplitter的代码分析一下吗? 也请用屏幕截图或 PDF 显示所需的结果。

image.jpg (334.8 KB)
image.jpg (271.0 KB)
input.docx (35.9 KB)
上面是测试word,下面是测试代码

    Document doc = new Document("/Users/zhanglinchao/IdeaProjects/mrframe/src/main/resources/static/doc/SetDrByUserWord2/input.docx");
    DocumentPageSplitter splitter = new DocumentPageSplitter(doc);

    for (int page = 1; page <= doc.getPageCount(); page++) {
        Document pageDoc = splitter.getDocumentOfPage(page);
        pageDoc.save(String.format("/Users/zhanglinchao/IdeaProjects/mrframe/src/main/resources/static/doc/SetDrByUserWord2/test/Output_Page_%d.docx", page));
    }

这是分页后的word
Output_Page_1.docx (29.9 KB)
Output_Page_2.docx (27.6 KB)
第二个word表头消失了

@z2631632737, DocumentPageSplitter 不是来自 Aspose.Words API。 你能分享这个类的代码吗?

PageSplitter.java.zip (5.5 KB)
这是这个类的代码

@z2631632737, 在您的情况下,当您在页面之间拆分表格时,您需要手动复制每一页上的表格标题行。 我正在研究您共享的代码以进行适当的更改。 完成后我会发布修改后的代码。

@z2631632737, 请找到修改后的 SectionSplitter.splitCompositeAtNode() 方法。 它在表拆分后手动将标题行复制到新表。

    private CompositeNode splitCompositeAtNode(CompositeNode baseNode, Node targetNode) throws Exception {
        CompositeNode cloneNode = (CompositeNode) baseNode.deepClone(false);
        Node node = targetNode;
        int currentPageNum = this.pageNumberFinder.getPage(baseNode);

        // Move all nodes found on the next page into the copied node. Handle row nodes separately.
        if (baseNode.getNodeType() != NodeType.ROW) {
            CompositeNode composite = cloneNode;
            if (baseNode.getNodeType() == NodeType.SECTION) {
                cloneNode = (CompositeNode) baseNode.deepClone(true);
                Section section = (Section) cloneNode;
                section.getBody().removeAllChildren();
                composite = section.getBody();
            }

            // If the table has repeated header row, copy the header row to the table on the new page. 
            // 如果表格有重复的表头行,则将表头行复制到新页面上的表中。
            if (composite.getNodeType() == NodeType.TABLE && baseNode.getNodeType() == NodeType.TABLE)
            {
                Row headerRow = ((Table)baseNode).getFirstRow();
                boolean isRepeatedHeaderRow = headerRow.getRowFormat().getHeadingFormat();
                if (isRepeatedHeaderRow)
                    composite.appendChild(headerRow.deepClone(true));
            }

            while (node != null) {
                Node nextNode = node.getNextSibling();
                composite.appendChild(node);
                node = nextNode;
            }
        } else {
            // If we are dealing with a row then we need to add in dummy cells for the cloned row.
            int targetPageNum = this.pageNumberFinder.getPage(targetNode);
            Node[] childNodes = baseNode.getChildNodes().toArray();
            for (Node childNode : childNodes) {
                int pageNum = this.pageNumberFinder.getPage(childNode);
                if (pageNum == targetPageNum) {
                    cloneNode.getLastChild().remove();
                    cloneNode.appendChild(childNode);
                } else if (pageNum == currentPageNum) {
                    cloneNode.appendChild(childNode.deepClone(false));
                    if (cloneNode.getLastChild().getNodeType() != NodeType.CELL) {
                        ((CompositeNode) cloneNode.getLastChild()).appendChild(((CompositeNode) childNode).getFirstChild().deepClone(false));
                    }
                }
            }
        }

        // Insert the split node after the original.
        baseNode.getParentNode().insertAfter(cloneNode, baseNode);

        // Update the new page numbers of the base node and the clone node including its descendents.
        // This will only be a single page as the cloned composite is split to be on one page.
        int currentEndPageNum = this.pageNumberFinder.getPageEnd(baseNode);
        this.pageNumberFinder.addPageNumbersForNode(baseNode, currentPageNum, currentEndPageNum - 1);
        this.pageNumberFinder.addPageNumbersForNode(cloneNode, currentEndPageNum, currentEndPageNum);

        for (Node childNode : (Iterable<Node>) cloneNode.getChildNodes(NodeType.ANY, true)) {
            this.pageNumberFinder.addPageNumbersForNode(childNode, currentEndPageNum, currentEndPageNum);
        }

        return cloneNode;
    }
}