Using Aspose Implementation:Aspose.Words for Java and Aspose Implementation-Version: 13.5.0.0
Below are the documents:
Source.docx https://drive.google.com/open?id=1jcTwAXLw8qKmNgPUxEKBOAC2DllBBNLd
A1.docx https://drive.google.com/open?id=1TKGTwqbYvsn_4wUYhpx_q3ilQt_j9P6o
A2.docx https://drive.google.com/open?id=18f8-l4fzvlRqLdqcwcpIoiNli9eEjkc8
A3.docx https://drive.google.com/open?id=1URLue9oDUy_Nyhhba3KHq21OhXQaRw29
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.