画像の操作

Aspose.Wordsを使用すると、ユーザーは非常に柔軟な方法で画像を操作できます。 この記事では、画像を操作する可能性のいくつかのみを探索できます。

文書から画像を抽出する方法

すべての画像はDocumentShapeノード内に格納されます。 すべての画像または特定の種類の画像をドキュメントから抽出するには、次の手順を実行します:

  • すべてのShapeノードを選択するには、GetChildNodesメソッドを使用します。
  • 結果のノードコレクションを反復処理します。
  • ブールプロパティHasImageを確認します。
  • ImageDataプロパティを使用してイメージデータを抽出します。
  • 画像データをファイルに保存します。

次のコード例は、ドキュメントから画像を抽出してファイルとして保存する方法を示しています:

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として保存する方法を示しています:

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