使用aspose单词

时间:2018-07-06 14:01:35

标签: aspose aspose.words

使用Aspose实现:用于Java的Aspose.Words Aspose实施版本: 13.5.0.0


以下是文档:

  1. Source.docx

  2. A1.docx

  3. A2.docx

  4. A3.docx

使用下面的代码附加单词文档:

public static void main(String[] args) {
        try {
            List<Document> documentsToBeMerged=new ArrayList<Document>();
            Document source=new Document("D:/Source.docx");
            Document identicalDoc1=new Document("D:/A1.docx");
            Document identicalDoc2=new Document("D:/A2.docx");
            Document identicalDoc3=new Document("D:/A3.docx");
            documentsToBeMerged.add(identicalDoc1);
            documentsToBeMerged.add(identicalDoc2);
            documentsToBeMerged.add(identicalDoc3);
            mergeDocumentsWithSourceDocumentHeaderFooter(source, documentsToBeMerged, "D:/output.docx");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public static void mergeDocumentsWithSourceDocumentHeaderFooter(
            Document destinationDocument, List<Document> mergingdocument,
            String outputFileName) throws Exception {
        try {
            for (Document document : mergingdocument) {
                document.getFirstSection().getPageSetup()
                        .setSectionStart(SectionStart.NEW_PAGE);
                document.getFirstSection().getHeadersFooters()
                        .linkToPrevious(true);
                destinationDocument.appendDocument(document,
                        ImportFormatMode.KEEP_SOURCE_FORMATTING);
            }
            AsposeUtil.convertNumPageFieldsToPageRef(destinationDocument);
            destinationDocument.updatePageLayout();
            destinationDocument.save(outputFileName);
        } catch (Exception e) {
            throw e;
        }
    }


 public static void convertNumPageFieldsToPageRef(Document doc) throws Exception
    {
        final String BOOKMARK_PREFIX = "_SubDocumentEnd";
        final String NUM_PAGES_FIELD_NAME = "NUMPAGES";
        final String PAGE_REF_FIELD_NAME = "PAGEREF";
        DocumentBuilder builder = new DocumentBuilder(doc);
        int subDocumentCount = 0;
        for (Section section : doc.getSections())
        {
            if (section.getPageSetup().getRestartPageNumbering())
            {
                if (!section.equals(doc.getFirstSection()))
                {
                    Section prevSection = (Section)section.getPreviousSibling();
                    Node lastNode = prevSection.getBody().getLastChild();
                    builder.moveTo(lastNode);
                    builder.startBookmark(BOOKMARK_PREFIX + subDocumentCount);
                    builder.endBookmark(BOOKMARK_PREFIX + subDocumentCount);
                    subDocumentCount++;
                }
            }
            if (section.equals(doc.getLastSection()))
            {
                Node lastNode = doc.getLastSection().getBody().getLastChild();
                builder.moveTo(lastNode);
                builder.startBookmark(BOOKMARK_PREFIX + subDocumentCount);
                builder.endBookmark(BOOKMARK_PREFIX + subDocumentCount);
            }
            for (Node node : section.getChildNodes(NodeType.FIELD_START, true).toArray())
            {
                FieldStart fieldStart = (FieldStart)node;
                if (fieldStart.getFieldType() == FieldType.FIELD_NUM_PAGES)
                {
                    String fieldCode = getFieldCode(fieldStart);
                    String fieldSwitches = fieldCode.replace(NUM_PAGES_FIELD_NAME, "").trim();
                    Node previousNode = fieldStart.getPreviousSibling();
                    if (previousNode == null)
                        previousNode = fieldStart;
                    builder.moveTo(previousNode);
                    Field newField = builder.insertField(MessageFormat.format(" {0} {1}{2} {3} ", PAGE_REF_FIELD_NAME, BOOKMARK_PREFIX, subDocumentCount, fieldSwitches));
                    previousNode.getParentNode().insertBefore(previousNode, newField.getStart());
                    removeField(fieldStart);
                }
            }
        }
    }


        private static String getFieldCode(FieldStart fieldStart) throws Exception
        {
            StringBuilder builder = new StringBuilder();
            for (Node node = fieldStart; node != null && node.getNodeType() != NodeType.FIELD_SEPARATOR &&
                    node.getNodeType() != NodeType.FIELD_END; node = node.nextPreOrder(node.getDocument()))
            {
                if (node.getNodeType() == NodeType.RUN)
                    builder.append(node.getText());
            }
            return builder.toString();
        }

        private static void removeField(FieldStart fieldStart) throws Exception
        {
            Node currentNode = fieldStart;
            boolean isRemoving = true;
            while (currentNode != null && isRemoving)
            {
                if (currentNode.getNodeType() == NodeType.FIELD_END)
                    isRemoving = false;
                Node nextNode = currentNode.nextPreOrder(currentNode.getDocument());
                currentNode.remove();
                currentNode = nextNode;
            }
}

问题:总页面output.docx包含12,最后一页的页脚将页面编号显示为12 of 11,这是错误的。

注意:Identical1.docxIdentical2.docx这两个文档都是相同的,并且包含相同的引用。

请向我建议任何解决页码问题的方法。

1 个答案:

答案 0 :(得分:0)

我们已经使用最新版本的Aspose.Words for Java 18.6测试了该方案,但未发现共享问题。请使用Aspose.Words for Java 18.6。请注意,Aspose.Words模仿MS Word的行为。如果使用MS Word加入这些文档,则最终的输出文档将有9页。

此外,请共享AsposeUtil.convertNumPageFieldsToPageRef方法的代码以进行进一步测试。

我与Aspose一起担任开发人员推广人员。