図形の操作

このトピックでは、Aspose.Wordsを使用して図形をプログラムで操作する方法について説明します。

Aspose.Wordsの図形は、AutoShape、textbox、freeform、OLEオブジェクト、ActiveXコントロール、または画像など、描画レイヤー内のオブジェクトを表します。 Word文書には、1つ以上の異なる図形を含めることができます。 ドキュメントの図形はShapeクラスで表されます。

ドキュメントビルダーを使用した図形の挿入

InsertShapeメソッドを使用して、指定されたタイプとサイズのインライン図形と、指定された位置、サイズ、テキスト折り返しタイプの自由浮動図形をドキュメントに挿入できます。 InsertShapeメソッドを使用すると、dml図形をドキュメントモデルに挿入できます。 それ以外の場合は、ドキュメントの保存中にそのようなノードがVML形状に変換されます。

次のコード例は、これらの種類の図形をドキュメントに挿入する方法を示しています:

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>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
//Free-floating shape insertion.
System::SharedPtr<Shape> shape = builder->InsertShape(ShapeType::TextBox, RelativeHorizontalPosition::Page, 100, RelativeVerticalPosition::Page, 100, 50, 50, WrapType::None);
shape->set_Rotation(30.0);
builder->Writeln();
//Inline shape insertion.
shape = builder->InsertShape(ShapeType::TextBox, 50, 50);
shape->set_Rotation(30.0);
System::SharedPtr<OoxmlSaveOptions> so = System::MakeObject<OoxmlSaveOptions>(SaveFormat::Docx);
// "Strict" or "Transitional" compliance allows to save shape as DML.
so->set_Compliance(OoxmlCompliance::Iso29500_2008_Transitional);
System::String outputPath = outputDataDir + u"WorkingWithShapes.InsertShapeUsingDocumentBuilder.docx";
// Save the document to disk.
doc->Save(outputPath, so);

ロックされたアスペクト比を設定

Aspose.Wordsを使用すると、図形のアスペクト比をAspectRatioLockedプロパティでロックするかどうかを指定できます。

次のコード例は、AspectRatioLockedプロパティを操作する方法を示しています:

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>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Shape> shape = builder->InsertImage(inputDataDir + u"Test.png");
shape->set_AspectRatioLocked(false);
System::String outputPath = outputDataDir + u"WorkingWithShapes.SetAspectRatioLocked.doc";
// Save the document to disk.
doc->Save(outputPath);

セル内の図形レイアウトを設定する

また、IsLayoutInCellプロパティを使用して、図形をテーブルの内側に表示するか、テーブルの外側に表示するかを指定することもできます。

次のコード例は、IsLayoutInCellプロパティを操作する方法を示しています:

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"LayoutInCell.docx");
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Shape> watermark = System::MakeObject<Shape>(doc, ShapeType::TextPlainText);
watermark->set_RelativeHorizontalPosition(RelativeHorizontalPosition::Page);
watermark->set_RelativeVerticalPosition(RelativeVerticalPosition::Page);
watermark->set_IsLayoutInCell(true);
// Display the shape outside of table cell if it will be placed into a cell.
watermark->set_Width(300);
watermark->set_Height(70);
watermark->set_HorizontalAlignment(HorizontalAlignment::Center);
watermark->set_VerticalAlignment(VerticalAlignment::Center);
watermark->set_Rotation(-40);
watermark->get_Fill()->set_Color(System::Drawing::Color::get_Gray());
watermark->set_StrokeColor(System::Drawing::Color::get_Gray());
watermark->get_TextPath()->set_Text(u"watermarkText");
watermark->get_TextPath()->set_FontFamily(u"Arial");
watermark->set_Name(System::String::Format(u"WaterMark_{0}",System::Guid::NewGuid()));
watermark->set_WrapType(WrapType::None);
System::SharedPtr<Run> run = System::DynamicCast_noexcept<Run>(doc->GetChildNodes(NodeType::Run, true)->idx_get(doc->GetChildNodes(NodeType::Run, true)->get_Count() - 1));
builder->MoveTo(run);
builder->InsertNode(watermark);
doc->get_CompatibilityOptions()->OptimizeFor(MsWordVersion::Word2010);
System::String outputPath = outputDataDir + u"WorkingWithShapes.SetShapeLayoutInCell.docx";
// Save the document to disk.
doc->Save(outputPath);

スニップ角の長方形を作成します。

Aspose.Wordsを使用してスニップ角矩形を作成することができます。 図形の種類は*SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded,DiagonalCornersRounded.*です

DML図形は、これらの図形タイプのInsertShapeメソッドを使用して作成されます。 これらの型を使用してVML図形を作成することはできません。 “Shape"クラスのパブリックコンストラクタを使用してshapeを作成しようとすると、“NotSupportedException"例外が発生します。

次のコード例は、これらの種類の図形をドキュメントに挿入する方法を示しています:

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>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Shape> shape = builder->InsertShape(ShapeType::TopCornersSnipped, 50, 50);
System::SharedPtr<OoxmlSaveOptions> so = System::MakeObject<OoxmlSaveOptions>(SaveFormat::Docx);
so->set_Compliance(OoxmlCompliance::Iso29500_2008_Transitional);
System::String outputPath = outputDataDir + u"WorkingWithShapes.AddCornersSnipped.docx";
//Save the document to disk.
doc->Save(outputPath, so);

実際の形状の境界点を取得する

Aspose.WordsAPIを使用すると、一番上の図形のアンカーを基準にして、ブロックを含む図形の位置とサイズをポイント単位で取得できます。 これを行うには、BoundsInPointsプロパティを使用します。

次のコード例は、BoundsInPointsプロパティを操作する方法を示しています:

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>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Shape> shape = builder->InsertImage(inputDataDir + u"Test.png");
shape->set_AspectRatioLocked(false);
std::cout << "Gets the actual bounds of the shape in points." << shape->GetShapeRenderer()->get_BoundsInPoints().ToString().ToUtf8String() << std::endl;

水平ルール形式

Aspose.WordsAPIは、水平ルールシェイプのプロパティにアクセスするためのHorizontalRuleFormatプロパティを提供します。 HorizontalRuleFormatクラスは、Height、Color、Shadeなどの基本的なプロパティを公開します。 水平ルールの書式設定のために。

次のコード例は、HorizontalRuleFormatを設定する方法を示しています:

For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C
auto doc = MakeObject<Document>();
auto builder = MakeObject<DocumentBuilder>(doc);
builder->Writeln(u"Insert a horizontal rule shape into the document.");
builder->InsertHorizontalRule();
doc->Save(ArtifactsDir + u"AddContentUsingDocumentBuilder.InsertHorizontalRule.docx");

Oleオブジェクトをアイコンとして挿入する

Aspose.WordsAPIは、埋め込みまたはリンクされたOLEオブジェクトをアイコンとしてドキュメントに挿入するShapeInsertOleObjectAsIcon関数を提供します。 この機能は、アイコンファイルとキャプションを指定することができます。 OLEオブジェクトタイプは、ファイル拡張子を使用して検出されます。

次のコード例は、oleオブジェクトをアイコンとしてドキュメントに挿入する方法を示しています:

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>();
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
System::SharedPtr<Shape> shape = builder->InsertOleObjectAsIcon(inputDataDir + u"embedded.xlsx", false, inputDataDir + u"icon.ico", u"My embedded file");
doc->Save(outputDataDir + u"WorkingWithShapes.InsertOLEObjectAsIcon.docx");
std::cout << "The document has been saved with OLE Object as an Icon." << std::endl;