Praca z obrazami

Aspose.Words umożliwia użytkownikom pracę z obrazami w bardzo elastyczny sposób. W tym artykule możesz zbadać tylko niektóre możliwości pracy z obrazami.

Jak wyodrębnić obrazy z dokumentu

Wszystkie obrazy są przechowywane wewnątrz Shape węzłów w Document. Aby wyodrębnić z dokumentu Wszystkie obrazy lub obrazy o określonym typie, wykonaj następujące kroki:

  • Użyj metody GetChildNodes, aby zaznaczyć wszystkie węzły Shape.
  • Iteruj przez wynikowe Kolekcje węzłów.
  • Sprawdź Właściwość HasImage boolean.
  • Wyodrębnij dane obrazu za pomocą właściwości ImageData.
  • Zapisz dane obrazu w pliku.

Poniższy przykład kodu pokazuje, jak wyodrębnić obrazy z dokumentu i zapisać je jako pliki:

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++;
}
}

Zapisywanie obrazów jako WMF

Aspose.Words zapewnia funkcjonalność, aby zapisać wszystkie dostępne obrazy w dokumencie do WMF Formatuj podczas konwersji DOCX na RTF.

Poniższy przykład kodu pokazuje, jak zapisywać obrazy jako WMF z opcjami zapisu RTF:

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);