Word处理控件Aspose.Words功能演示:使用 C++ 将 DOCX 、DOC进行互相转换

本文将介绍如何使用 C++ 将 DOCX 、DOC进行互相转换,欢迎查阅~

Microsoft Word 文档有两种格式,DOC和DOCX。DOC 是一种较旧的格式,而 DOCX 是它的继任者。您可以将 DOCX 文件转换为 DOC 格式,反之亦然。在本文中,您将学习如何将 DOCX 文件转换为 DOC 格式,以及将 DOCX 文件转换为 DOCX 格式。您还将看到如何批量执行这些转换。

一、下载用于将 DOCX与DOC 进行互相转换的C++ API

Aspose.Words for C++是一个本地 C++ 库,允许您创建、阅读和修改 Microsoft Word 文档。此外,它还支持将 DOCX 文件转换为 DOC 格式和将 DOC 文件转换为 DOCX 格式。您可以通过NuGet安装 API,也可以直接从“下载”部分下载。

PM> Install-Package Aspose.Words.Cpp
二、使用 C++ 将 DOCX 文件转换为 DOC 格式

您可以按照以下步骤将 DOCX 文件转换为 DOC 格式。

  • 创建LoadOptions类的实例。
  • 使用LoadOptions->set_LoadFormat(Aspose::Words::LoadFormat value)方法将加载格式设置为 DOCX 。
  • 使用Document类加载源 DOCX 文件。
  • 使用Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat)方法保存转换后的 DOC 文件。

以下示例代码展示了如何使用 C++ 将 DOCX 文件转换为 DOC 文件。

// Create an instance of the LoadOptions classauto loadOptions = System::MakeObject<LoadOptions>();// Specify LoadFormat of input word documentloadOptions->set_LoadFormat(LoadFormat::Docx);// Load source DOCX fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(u"SourceDirectory\Word\Sample 4.docx", loadOptions);// Save the DOC filedoc->Save(u"OutputDirectory\output.doc", SaveFormat::Doc);
三、使用 C++ 将 DOCX 文件批量转换为 DOC 格式

如果您有数百个 DOCX 文件需要转换为 DOC 格式,您只需执行一次程序即可轻松转换它们。在这个例子中,我们将使用 boost 库来遍历目录中的文件。以下是将 DOCX 文件批量转换为 DOC 格式的步骤。

  • 遍历目录中的文件。
  • 创建LoadOptions类的实例。
  • 使用LoadOptions->set_LoadFormat(Aspose::Words::LoadFormat value)方法将加载格式设置为 DOCX 。
  • 使用Document类加载 DOCX 文件。
  • 将文件名中的扩展名更改为 DOC。
  • 使用Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat)方法保存转换后的 DOC 文件。

以下示例代码演示了如何使用 C++ 将 DOCX 文件批量转换为 DOC 格式。

// Iterate through the files in the directoryfor (directory_entry& file : directory_iterator("SourceDirectory\Word")){// Check file extensionif (file.path().extension().string() == ".docx"){// Create an instance of the LoadOptions classauto loadOptions = System::MakeObject<LoadOptions>();// Specify LoadFormat of input word documentloadOptions->set_LoadFormat(LoadFormat::Docx);// Load the DOCX fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>((System::String)file.path().string(), loadOptions);// Change the file extensionSystem::String fileName = (System::String)file.path().filename().string();fileName = fileName.Replace(u".docx", u".doc");// Save the DOC filedoc->Save(System::String::Concat(u"OutputDirectory\", fileName), SaveFormat::Doc);}}
四、使用 C++ 将 DOCX 文件批量转换为 DOC 格式

以下是将 DOC 文件转换为 DOCX 文件的步骤。

  • 创建LoadOptions类的实例。
  • 使用LoadOptions->set_LoadFormat(Aspose::Words::LoadFormat value)方法将加载格式设置为 DOC 。
  • 使用Document类加载源 DOC 文件。
  • 使用Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat)方法保存转换后的 DOCX 文件。

以下示例代码展示了如何使用 C++ 将 DOC 文件转换为 DOCX 文件。

// Create an instance of the LoadOptions classauto loadOptions = System::MakeObject<LoadOptions>();// Specify LoadFormat of input word documentloadOptions->set_LoadFormat(LoadFormat::Doc);// Load source DOC fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>(u"SourceDirectory\Word\Sample 1.doc", loadOptions);// Save the DOCX filedoc->Save(u"OutputDirectory\output.docx", SaveFormat::Docx);
五、使用 C++ 将 DOC 文件批量转换为 DOCX 格式

要将 DOC 文件批量转换为 DOCX 格式,请使用以下步骤。

  • 遍历目录中的文件。
  • 创建LoadOptions类的实例。
  • 使用LoadOptions->set_LoadFormat(Aspose::Words::LoadFormat value)方法将加载格式设置为 DOC 。
  • 使用Document类加载 DOC 文件。
  • 将文件名中的扩展名更改为 DOCX。
  • 使用Document->Save(System::String fileName, Aspose::Words::SaveFormat saveFormat)方法保存转换后的 DOCX 文件。

以下示例代码演示了如何使用 C++ 将 DOC 文件批量转换为 DOCX 格式。

// Iterate through the files in the directoryfor (directory_entry& file : directory_iterator("SourceDirectory\Word")){// Check file extensionif (file.path().extension().string() == ".doc"){// Create an instance of the LoadOptions classauto loadOptions = System::MakeObject<LoadOptions>();// Specify LoadFormat of input word documentloadOptions->set_LoadFormat(LoadFormat::Doc);// Load the DOC fileSystem::SharedPtr<Document> doc = System::MakeObject<Document>((System::String)file.path().string(), loadOptions);// Change the file extensionSystem::String fileName = (System::String)file.path().filename().string();fileName = fileName.Replace(u".doc", u".docx");// Save the DOCX filedoc->Save(System::String::Concat(u"OutputDirectory\", fileName), SaveFormat::Docx);}}

以上便是使用 C++ 将 DOCX 和 DOC 进行互相转换详细步骤,要是您还有其他关于产品方面的问题,欢迎咨询我们,或者加入我们官方技术交流群。


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

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

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年9月5日
下一篇 2022年9月5日

相关推荐

发表回复

登录后才能评论