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

本文介绍了如何使用Aspose.WordsforC++API来转换DOCX和DOC文件格式。API支持在C++中加载、修改和保存这两种格式,同时提供了批量转换功能,方便处理大量文档。示例代码展示了将DOCX转换为DOC以及将DOC转换为DOCX的详细步骤。
摘要由CSDN通过智能技术生成

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

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

Aspose.words 最新下载(qun:761297826)icon-default.png?t=N0U7https://www.evget.com/product/4116/download

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 class
auto loadOptions = System::MakeObject<LoadOptions>();

// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Docx);

// Load source DOCX file
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"SourceDirectory\\Word\\Sample 4.docx", loadOptions);

// Save the DOC file
doc->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 directory
for (directory_entry& file : directory_iterator("SourceDirectory\\Word"))
{
// Check file extension
if (file.path().extension().string() == ".docx")
{
// Create an instance of the LoadOptions class
auto loadOptions = System::MakeObject<LoadOptions>();

// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Docx);

// Load the DOCX file
System::SharedPtr<Document> doc = System::MakeObject<Document>((System::String)file.path().string(), loadOptions);

// Change the file extension
System::String fileName = (System::String)file.path().filename().string();
fileName = fileName.Replace(u".docx", u".doc");

// Save the DOC file
doc->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 class
auto loadOptions = System::MakeObject<LoadOptions>();

// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Doc);

// Load source DOC file
System::SharedPtr<Document> doc = System::MakeObject<Document>(u"SourceDirectory\\Word\\Sample 1.doc", loadOptions);

// Save the DOCX file
doc->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 directory
for (directory_entry& file : directory_iterator("SourceDirectory\\Word"))
{
// Check file extension
if (file.path().extension().string() == ".doc")
{
// Create an instance of the LoadOptions class
auto loadOptions = System::MakeObject<LoadOptions>();

// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Doc);

// Load the DOC file
System::SharedPtr<Document> doc = System::MakeObject<Document>((System::String)file.path().string(), loadOptions);

// Change the file extension
System::String fileName = (System::String)file.path().filename().string();
fileName = fileName.Replace(u".doc", u".docx");

// Save the DOCX file
doc->Save(System::String::Concat(u"OutputDirectory\\", fileName), SaveFormat::Docx);
}
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
“com.aspose.words.filecorruptedexception: the document appears to be corrupte” 是一个在使用Aspose.Words操作文档时可能会遇到的异常错误。这个错误表示文档看起来是损坏的。 当我们使用Aspose.Words处理文档时,可能会发生一些不可预料的问题。如果文档的结构或内容不符合Aspose.Words所期望的格式,就会引发“文件损坏异常”。这可能是因为文档本身已经损坏或损坏,或者文档格式不符合Aspose的规范要求。 要解决这个问题,可以尝试以下几个方案: 1. 确保你的Aspose.Words的版本是最新的。有时候这个问题可能是由于旧版本的Aspose.Words引起的,更新到最新版本可能会解决该问题。 2. 检查文档本身是否损坏。你可以尝试使用其他文档查看器或编辑器打开该文档,看是否可以正常访问。如果是损坏的,那么你可能需要找到一个可用的备份文件。 3. 检查文档的格式是否符合Aspose.Words的要求。Aspose.Words文档的格式和内容有一些特定的要求,例如支持的文件类型、支持的文档版本等。确保你的文档符合Aspose.Words的要求,如果不符合,可以尝试转换文档格式或升级文档版本。 4. 如果以上方法仍然无法解决问题,可以联系Aspose的技术支持团队,向他们提供详细的错误信息和问题描述,以便他们能够更深入地帮助解决。 总之,“com.aspose.words.filecorruptedexception: the document appears to be corrupte” 这个异常表示文档看起来是损坏的,可能是由于文档本身损坏或格式不符合Aspose.Words的要求所致。通过更新Aspose.Words版本、检查文档的完整性和格式,或与Aspose的技术支持团队联系,我们有望解决该问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值