Práce s obrázky
Contents
[
Hide
]
Aspose.Words umožňuje uživatelům pracovat s obrázky velmi flexibilním způsobem. V tomto článku můžete prozkoumat pouze některé možnosti práce s obrázky.
Jak extrahovat obrázky z dokumentu
Všechny obrázky jsou uloženy uvnitř Shape uzlů v Document. Chcete-li z dokumentu extrahovat všechny obrázky nebo obrázky s konkrétním typem, postupujte takto:
- Pomocí metody GetChildNodes vyberte všechny uzly Shape.
- Iterovat prostřednictvím výsledných kolekcí uzlů.
- Zkontrolujte logickou vlastnost HasImage.
- Extrahujte obrazová data pomocí vlastnosti ImageData.
- Uložte obrazová data do souboru.
Následující příklad kódu ukazuje, jak extrahovat obrázky z dokumentu a uložit je jako soubory:
This file contains hidden or 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++; | |
} | |
} |
Ukládání obrázků jako WMF
Aspose.Words poskytuje funkce pro uložení všech dostupných obrázků v dokumentu do WMF formátujte při převodu DOCX na RTF.
Následující příklad kódu ukazuje, jak uložit obrázky jako WMF s RTF Možnosti uložení:
This file contains hidden or 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); |