1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Word处理控件Aspose.Words功能演示:使用 C++ 合并 MS Word 文档

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

时间:2024-05-06 07:42:07

相关推荐

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

Aspose.Words是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose API支持流行文件格式处理,并允许将各类文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。

Aspose.words 最新下载(qun:761297826)/product/4116/download

MS Word 是一个强大的工具,它允许您使用一组出色的功能自定义和格式化您的专业文档。您可能会发现自己需要合并多个 Word 文档,例如将所有收据合并到一个文件中,或者合并不同的软件需求文档以便于管理。此外,合并文件简化了共享它们的过程。在本文中,您将学习如何使用 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 文档与 Aspose.Words for C++ API 合并是一件轻而易举的事。您只需几行代码即可加载和合并它们。下图显示了我们将在本文中合并的两个示例 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 模式生成的合并文档。

使用ImportFormatMode::KeepSourceFormatting生成的输出

使用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生成的输出

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

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。