aspose -java25,原来的A.docx中有个节需要复制到B.docx,这个节有修订内容,怎么才能保留这些内容到B.docx中
要将A.docx中的一个节(包含修订内容)复制到B.docx中,并保留这些修订,您可以使用Aspose.Words for Java的Document类和相关方法。以下是一个示例代码,展示如何实现这一点:
import com.aspose.words.Document;
import com.aspose.words.ImportFormatMode;
public class CopySectionWithRevisions {
    public static void main(String[] args) throws Exception {
        // 加载源文档A.docx
        Document sourceDoc = new Document("A.docx");
        // 加载目标文档B.docx
        Document destinationDoc = new Document("B.docx");
        // 获取要复制的节(假设是第一个节)
        Document sectionToCopy = new Document();
        sectionToCopy.appendDocument(sourceDoc.getSections().get(0), ImportFormatMode.KEEP_SOURCE_FORMATTING);
        // 将节添加到目标文档
        destinationDoc.appendDocument(sectionToCopy, ImportFormatMode.KEEP_SOURCE_FORMATTING);
        // 保存目标文档
        destinationDoc.save("B_with_copied_section.docx");
        System.out.println("节已成功复制并保存到B_with_copied_section.docx");
    }
}
在这个示例中,我们首先加载源文档和目标文档,然后获取要复制的节,并将其添加到目标文档中。最后,保存目标文档。这样可以确保修订内容被保留。
请确保在使用此代码之前,您已经将Aspose.Words for Java库添加到您的项目中。
@yiranzhiyuan 您可以使用以下代码复制该部分:
Document sourceDoc = new Document("A.docx");
Document destDoc = new Document("B.docx");
Section sourceSection = sourceDoc.getSections().get(0);
Section clonedSection = (Section) sourceSection.deepClone(true);
Node importedSection = destDoc.importNode(clonedSection, true, ImportFormatMode.KEEP_SOURCE_FORMATTING);
destDoc.getSections().add(importedSection);
destDoc.save("output.docx");