Использование реализации Aspose: Aspose.Words для Java
Версия реализации Aspose: 13.5.0.0
Ниже приведены документы:
Source.docx
A1.docx
A2.docx
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.docx
и Identical2.docx
одинаковы и содержат одинаковые ссылки.
Пожалуйста, предложите мне любое решение, как решить проблему с номером страницы.