Lucrul cu imagini
Aspose.Words permite utilizatorilor să lucreze cu imagini într-un mod foarte flexibil. În acest articol, puteți explora doar câteva dintre posibilitățile de lucru cu imagini.
Cum să extrageți imagini dintr-un Document
Toate imaginile sunt stocate în interiorul Shape noduri într-un Document. Pentru a extrage din document toate imaginile sau imaginile cu un anumit tip, urmați acești pași:
- Utilizați metoda GetChildNodes pentru a selecta toate nodurile Shape.
- Iterați prin colecțiile de noduri rezultate.
- Verificați proprietatea booleană HasImage.
- Extrageți datele imaginii folosind proprietatea ImageData.
- Salvați datele imaginii într-un fișier.
Următorul exemplu de cod arată cum să extrageți imagini dintr-un document și să le salvați ca fișiere:
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++; | |
} | |
} |
Salvarea imaginilor ca WMF
Aspose.Words oferă funcționalitate pentru a salva toate imaginile disponibile într - un document WMF formatați în timp ce convertiți DOCX în RTF.
Următorul exemplu de cod arată cum să salvați imaginile ca WMF cu RTF opțiuni de salvare:
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); |