Microsoft Word 文档有两种格式, DOC 和 DOCX。DOC 是一种较旧的格式,而 DOCX 是它的继任者。可以将 DOCX 文件转换为 DOC 格式,反之亦然。在本文中,将学习如何将 DOCX 文件转换为 DOC 格式以及如何将 DOC 文件转换为 DOCX 格式。
让我们探索以下有关的内容:
- 使用 C++ 将 DOCX 文件转换为 DOC 格式
- 使用 C++ 将 DOCX 文件批量转换为 DOC 格式
- 使用 C++ 将 DOC 文件转换为 DOCX 格式
- 使用 C++ 将 DOC 文件批量转换为 DOCX 格式
![无需MS Office!使用Aspose在C ++中以编程方式将 DOCX 转换为 DOC](https://img-blog.csdnimg.cn/img_convert/66e9bd8987c2639e6d78c1921931e15f.png)
Aspose.Words for C++ 是一个原生的 C++ 库,允许您创建、读取和修改 Microsoft Word 文档。此外,它还支持将 DOCX 文件转换为 DOC 格式和将 DOC 文件转换为 DOCX 格式。
>>你可以下载Aspose.Words for C ++ 最新版测试体验。
使用 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();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Docx);
// Load source DOCX file
System::SharedPtrdoc = System::MakeObject(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();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Docx);
// Load the DOCX file
System::SharedPtrdoc = System::MakeObject((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++ 将 DOC 文件转换为 DOCX 格式
以下是将 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();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Doc);
// Load source DOC file
System::SharedPtrdoc = System::MakeObject(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();
// Specify LoadFormat of input word document
loadOptions->set_LoadFormat(LoadFormat::Doc);
// Load the DOC file
System::SharedPtrdoc = System::MakeObject((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);
}
}
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。