Word处理控件Aspose.Words功能演示:使用 C++ 合并 MS Word 文档

一、下载用于合并 Word 文档的 C++ API

Aspose.Words for C++是一个原生 C++ 库,允许您创建、更改和转换 Microsoft Word 文档。此外,它还支持将多个 Word 文档合并到一个文件中。您可以通过NuGet安装 API,也可以直接从“下载”部分下载。

PM> Install-Package Aspose.Words.Cpp
二、使用 C++ 合并 Word 文档

要合并的 Word 文档

以下是使用 C++ 合并 Word 文档的步骤。

  • 使用Document类加载目标文档。
  • 使用Document类加载源文档。
  • 使用目标文档实例的Document->AppendDocument (System::SharedPtr Aspose::Words::Document srcDoc, Aspose::Words::ImportFormatMode importFormatMode) 方法合并两个文档。
  • 使用ImportFormatMode枚举设置导入源文档的样式首选项。
  • 使用Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat)方法保存合并的 Word 文档。

以下是合并 Word 文档的示例代码。

// Sample Word documents to be mergedSystem::String sampleFile1 = u"SourceDirectory\Sample 1.docx";System::String sampleFile2 = u"SourceDirectory\Sample 2.docx";// Load Word documents to be mergedSystem::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);// Merge documents keeping source file's formattingdocument1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting);// Output file pathSystem::String outputPath = u"OutputDirectory\merged-doc-out.docx";// Save merged document as DOCX filedocument1->Save(outputPath, Aspose::Words::SaveFormat::Docx);

下图比较了使用ImportFormatMode ::KeepSourceFormatting 和ImportFormatMode ::UseDestinationStyles 模式生成的合并文档。

使用 ImportFormatOptions::KeepSourceFormatting 生成的合并 Word 文档

使用ImportFormatMode::KeepSourceFormatting生成的输出

使用 ImportFormatOptions::UseDestinationStyles 生成的合并 Word 文档

使用ImportFormatMode::UseDestinationStyles生成的输出

三、使用其他选项合并 Word 文档

Aspose.Words for C++ API 提供ImportFormatOptions类用于自定义 Word 文件的合并。以下是ImportFormatOptions类提供的选项。

  • IgnoreHeaderFooter:指定在使用ImportFormatMode ::KeepSourceFormatting 模式时是否忽略页眉/页脚内容的格式。
  • IgnoreTextBoxes:指定在使用ImportFormatMode ::KeepSourceFormatting 模式时是否忽略文本框的源格式。
  • KeepSourceNumbering:指定在源文档和目标文档中发生冲突时如何导入编 。
  • SmartStyleBehavior:指定当样式在源文档和目标文档中具有相同名称时如何导入样式。

以下是将多个 Word 文档与其他选项合并的步骤:

  • 使用Document类加载目标文档。
  • 使用Document类加载源文档。
  • 创建ImportFormatOptions类的实例并设置所需的选项。
  • 使用Document->AppendDocument( System::SharedPtr Aspose::Words::Document srcDoc, Aspose::Words::ImportFormatMode importFormatMode, System::SharedPtr Aspose::Words::ImportFormatOptions importFormatOptions) 方法合并文档。
  • 使用Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat)方法保存目标 Word 文档。

以下是使用附加选项合并 Word 文档的示例代码。

// Sample Word documents to be mergedSystem::String sampleFile1 = u"SourceDirectory\Sample 1.docx";System::String sampleFile2 = u"SourceDirectory\Sample 2.docx";// Load Word documents to be mergedSystem::SharedPtr<Aspose::Words::Document> document1 = System::MakeObject<Aspose::Words::Document>(sampleFile1);System::SharedPtr<Aspose::Words::Document> document2 = System::MakeObject<Aspose::Words::Document>(sampleFile2);// Set optionsauto options = MakeObject<Aspose::Words::ImportFormatOptions>();options->set_IgnoreHeaderFooter(false);// Merge documents keeping source file's formattingdocument1->AppendDocument(document2, Aspose::Words::ImportFormatMode::KeepSourceFormatting, options);// Output file pathSystem::String outputPath = u"OutputDirectory\merged-doc-out.docx";// Save merged document as DOCX filedocument1->Save(outputPath, Aspose::Words::SaveFormat::Docx);

下图比较了通过将IgnoreHeaderFooter选项设置为truefalse生成的合并文档。

通过将 IgnoreHeaderFooter 选项设置为 true 生成的合并 Word 文档

通过将IgnoreHeaderFooter选项设置为true生成的输出

通过将 IgnoreHeaderFooter 选项设置为 false 生成的合并 Word 文档

通过将IgnoreHeaderFooter选项设置为false生成的输出

以上便是使用 C++ 合并 MS Word 文档详细步骤,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。


欢迎下载|体验更多Aspose产品

点此获取更多Aspose产品信息 或 加入Aspose技术交流群(
标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年10月7日
下一篇 2022年10月7日

相关推荐

发表回复

登录后才能评论