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

Free Support Forum - aspose.com

Merging two identical word document displaying wrong page number using aspose word

Using Aspose Implementation:Aspose.Words for Java and Aspose Implementation-Version: 13.5.0.0

Below are the documents:

Source.docx
A1.docx
A2.docx
A3.docx

Using below code to append the word documents:

    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;
		}
	}

Below code convertNumPageFieldsToPageRef:

    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;
    	        }
    	    }

Issue: Total page output.docx contains 12 and last page footer is showing page number as 12 of 11 which is wrong.

Note: Documents contains End note References.

Please suggest me any solution how to resolve the page number issue.

@SHOME,

Thanks for your inquiry. We have tested the scenario using latest version of Aspose.Words for Java 18.6 and have not found the shared issue. Please use Aspose.Words for Java 18.6. We have attached the output DOCX with this post for your kind reference. Merged document.zip (58.8 KB)

Please note that Aspose.Words mimics the behavior of MS Word. If you join these documents, the final output document will have 9 pages.

Moreover, please share the code of AsposeUtil.convertNumPageFieldsToPageRef method for further testing. Thanks for your cooperation.

Thanks for the response. Now I added convertNumPageFieldsToPageRef to the post.Please assist me to resolve the issue.

@SHOME,

Thanks for sharing the code. Please check the attached output document generated by Aspose.Words for Java 18.6. 18.7.zip (58.8 KB)

In the attached output document Last page displaying correctly Page 9 of 9 and other pages showing wrong page numbers 2nd page showing as Page 2 of 6.

@SHOME,

Thanks for your inquiry. We have not found the shared issue when we open the 18.7.docx in MS Word 2016. Please check the attached screenshot. page number.png (46.4 KB)

Moreover, please update your code according to latest API. E.g. please use Field.Remove method instead of removeField(fieldStart), use Field.GetFieldCode method instead of getFieldCode(fieldStart).

Thanks for your support. I updated the post and shared the documents again with the original issue I am facing.I am getting page number issue in last page. Please assist me.Please help me.

@SHOME,

Thanks for your inquiry. You are inserting hidden bookmark “_SubDocumentEnd” in the document. It is on page 10. The total number of pages are 11. The last page’s footer shows 11 of 10. This is because the bookmark ("_SubDocumentEnd") is on tenth page. Please check the attached output document. 18.7-RefPage.zip (69.8 KB)

Please insert the bookmark on the last page to get the desired output.

Thanks a lot for the support.Is there any solution to display correct page number without modifying the original documents.Is there any solution programmatically can we remove bookmark ("_SubDocumentEnd") on tenth page.

As I can see in convertNumPageFieldsToPageRef it contains final String BOOKMARK_PREFIX = "_SubDocumentEnd"; Is that causing the issue?

@SHOME,

Thanks for your inquiry. Please note that Aspose.Words mimic the behavior of MS Word. You are facing the expected behavior of Aspose.Words.

Could you please share some detail about your requirement? Please share why are you converting NUMPAGES with PAGEREF field. We will then provide you more information about your query.

Thanks for your support.We are using and converting NUMPAGES with PAGEREF field to generate the page numbers.But it is displaying the last page number incorrectly.

Additionally I observe after merging word files when I scroll through word document first page number is not displaying and from second page it is displaying incorrect page number But page number is getting update automatically If I scroll to last page and come again to see the top pages.

Kindly assist me to resolve the issue.Thanks for your help.

@SHOME,

Thanks for your inquiry.

In your case, we suggest you please do not use convertNumPageFieldsToPageRef method. There is no need to replace NUMPAGES with PAGEREF.

The source.docx has different header/footer for first page. Please check this screenshot. You can disable it using PageSetup.DifferentFirstPageHeaderFooter property as shown below.

public static void mergeDocumentsWithSourceDocumentHeaderFooter(
        Document destinationDocument, java.util.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);
        }

        //convertNumPageFieldsToPageRef(destinationDocument);
        destinationDocument.getFirstSection().getPageSetup().setDifferentFirstPageHeaderFooter(false);
        destinationDocument.updatePageLayout();
        destinationDocument.save(outputFileName);
    } catch (Exception e) {
        throw e;
    }
}