使用图像
Contents
[
Hide
]
Aspose.Words允许用户以非常灵活的方式处理图像。 在本文中,您只能探索使用图像的一些可能性。
如何从文档中提取图像
所有图像都存储在Document中的Shape节点中。 要从文档中提取具有特定类型的所有图像或图像,请执行以下步骤:
- 使用GetChildNodes方法选择所有Shape节点。
- 迭代生成的节点集合。
- 检查HasImage布尔属性。
- 使用ImageData属性提取图像数据。
- 将图像数据保存到文件中。
下面的代码示例演示如何从文档中提取图像并将其另存为文件:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
// The path to the documents directories. | |
System::String inputDataDir = GetInputDataDir_WorkingWithImages(); | |
System::String outputDataDir = GetOutputDataDir_WorkingWithImages(); | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"Image.SampleImages.doc"); | |
System::SharedPtr<NodeCollection> shapes = doc->GetChildNodes(NodeType::Shape, true); | |
int32_t imageIndex = 0; | |
for (System::SharedPtr<Shape> shape : System::IterateOver<System::SharedPtr<Shape>>(shapes)) | |
{ | |
if (shape->get_HasImage()) | |
{ | |
System::String imageFileName = System::String::Format(u"Image.ExportImages.{0}.{1}", imageIndex, FileFormatUtil::ImageTypeToExtension(shape->get_ImageData()->get_ImageType())); | |
System::String imagePath = outputDataDir + imageFileName; | |
shape->get_ImageData()->Save(imagePath); | |
std::cout << "Image saved at " << imagePath.ToUtf8String() << std::endl; | |
imageIndex++; | |
} | |
} |
将图像保存为WMF
Aspose.Words提供将文档中所有可用图像保存到 WMF 将DOCX转换为RTF时进行格式化。
下面的代码示例演示如何使用RTF保存选项将图像保存为WMF:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C | |
System::SharedPtr<Document> doc = System::MakeObject<Document>(inputDataDir + u"TestFile.doc"); | |
System::SharedPtr<RtfSaveOptions> options = System::MakeObject<RtfSaveOptions>(); | |
options->set_SaveImagesAsWmf(true); | |
doc->Save(outputDataDir + u"WorkingWithRtfSaveOptions.SavingImagesAsWmf.rtf", options); |