word合并.zip (153.6 KB)
第二个word里包含了使用aspose生成的目录和页码,再用第一个word与第二个word进行合并,页码就不见了,请问该如何解决
我的是java;这样可以么
private void mergeWord(Document mainDoc, Map<String, Document> childFileMap) throws Exception {
DocumentBuilder builder = new DocumentBuilder(mainDoc);
for (Map.Entry<String, Document> entry : childFileMap.entrySet()) {
Bookmark bm = mainDoc.getRange().getBookmarks().get(entry.getKey());
if (bm != null) {
builder.moveToBookmark(entry.getKey());
bm.setText("");
bm.remove();
} else {
builder.moveToDocumentEnd();
}
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertDocument(entry.getValue(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
}
我的是java;这个是word拼接的代码
private void mergeWord(Document mainDoc, Map<String, Document> childFileMap) throws Exception {
DocumentBuilder builder = new DocumentBuilder(mainDoc);
for (Map.Entry<String, Document> entry : childFileMap.entrySet()) {
Bookmark bm = mainDoc.getRange().getBookmarks().get(entry.getKey());
if (bm != null) {
builder.moveToBookmark(entry.getKey());
bm.setText("");
bm.remove();
} else {
builder.moveToDocumentEnd();
}
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertDocument(entry.getValue(), ImportFormatMode.KEEP_SOURCE_FORMATTING);
}
}
@liujing111, 您需要将页眉和页脚从第二个文档手动复制到第一个文档。 请看这个例子:
Document mainDoc = new Document("第一个word.doc");
Document secondDoc = new Document("第二个word(带aspose生成的页码和目录).doc");
HashMap<String, Document> childFileMap = new HashMap<String, Document>();
childFileMap.put("second", secondDoc);
DocumentBuilder builder = new DocumentBuilder(mainDoc);
for (Map.Entry<String, Document> entry : childFileMap.entrySet()) {
Bookmark bm = mainDoc.getRange().getBookmarks().get(entry.getKey());
if (bm != null) {
builder.moveToBookmark(entry.getKey());
bm.setText("");
bm.remove();
} else {
builder.moveToDocumentEnd();
}
builder.insertBreak(BreakType.PAGE_BREAK);
builder.insertDocument(entry.getValue(), ImportFormatMode.USE_DESTINATION_STYLES);
MergeHeaderFooter(secondDoc, mainDoc, HeaderFooterType.FOOTER_PRIMARY);
}
mainDoc.updateFields();
mainDoc.save("merged.docx");
public static void MergeHeaderFooter(Document srcDoc, Document dstDoc, int headerType) throws Exception
{
for (Section section : dstDoc.getSections())
{
HeaderFooter header = section.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(section.getDocument(), headerType);
section.getHeadersFooters().add(header);
}
for (var srcNode : (Iterable<Node>)srcDoc.getFirstSection().getHeadersFooters().getByHeaderFooterType(headerType).getChildNodes())
{
Node dstNode = dstDoc.importNode(srcNode, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
header.appendChild(dstNode);
}
}
}
merged.docx (92.4 KB)
但是这样的话第一个文档也会有页码,现在是第一个文档不要页码,第二个文档要页码,需要怎么写;或者有没有先将两个文档合并,再把合并后的文档从第几页开始生成页码,有没有此种方案
@liujing111, 在这种情况下,您可以在第二个文档之前开始一个新部分。 这将产生您想要的结果。
Document mainDoc = new Document("第一个word.doc");
Document secondDoc = new Document("第二个word(带aspose生成的页码和目录).doc");
HashMap<String, Document> childFileMap = new HashMap<String, Document>();
childFileMap.put("second", secondDoc);
DocumentBuilder builder = new DocumentBuilder(mainDoc);
for (Map.Entry<String, Document> entry : childFileMap.entrySet()) {
Bookmark bm = mainDoc.getRange().getBookmarks().get(entry.getKey());
if (bm != null) {
builder.moveToBookmark(entry.getKey());
bm.setText("");
bm.remove();
} else {
builder.moveToDocumentEnd();
}
// Start a new section.
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.insertDocument(entry.getValue(), ImportFormatMode.USE_DESTINATION_STYLES);
}
mainDoc.updateFields();
mainDoc.save("merged.docx");
您可能会在以下部分中找到有关页眉和页脚的有用解释:
https://forum.aspose.com/t/aspose-word-for-java/240765/10
builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE);
builder.insertDocument(entry.getValue(), ImportFormatMode.USE_DESTINATION_STYLES);
这种方法也不行呢我试了下 @denis.shvydkiy