図形の操作
このトピックでは、Aspose.Words を使用してプログラムでシェイプを操作する方法について説明します。
Aspose.Words の図形は、オートシェイプ、テキストボックス、フリーフォーム、OLE オブジェクト、ActiveX コントロール、画像などの描画レイヤー内のオブジェクトを表します。 Word 文書には 1 つ以上の異なる図形を含めることができます。文書の形状はShapeクラスで表現されます。
ドキュメントビルダーを使用した図形の挿入
insert_shape メソッドを使用して、指定したタイプとサイズのインライン図形、および指定した位置、サイズ、テキストの折り返しタイプのフリーフローティング図形をドキュメントに挿入できます。 insert_shape メソッドを使用すると、DML 形状をドキュメント モデルに挿入できます。ドキュメントは、DML 形状をサポートする形式で保存する必要があります。そうでない場合、ドキュメントの保存中に、そのようなノードは VML 形状に変換されます。
次のコード例は、これらの種類の図形をドキュメントに挿入する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
shape = builder.insert_shape(aw.drawing.ShapeType.TEXT_BOX, aw.drawing.RelativeHorizontalPosition.PAGE, 100, | |
aw.drawing.RelativeVerticalPosition.PAGE, 100, 50, 50, aw.drawing.WrapType.NONE) | |
shape.rotation = 30.0 | |
builder.writeln() | |
shape = builder.insert_shape(aw.drawing.ShapeType.TEXT_BOX, 50, 50) | |
shape.rotation = 30.0 | |
saveOptions = aw.saving.OoxmlSaveOptions(aw.SaveFormat.DOCX) | |
saveOptions.compliance = aw.saving.OoxmlCompliance.ISO29500_2008_TRANSITIONAL | |
doc.save(docs_base.artifacts_dir + "WorkingWithShapes.insert_shape.docx", saveOptions) |
アスペクト比を設定ロック
Aspose.Words を使用すると、aspect_ratio_locked プロパティを通じてシェイプのアスペクト比をロックするかどうかを指定できます。
次のコード例は、AspectRatioLocked プロパティを操作する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
shape = builder.insert_image(docs_base.images_dir + "Transparent background logo.png") | |
shape.aspect_ratio_locked = False | |
doc.save(docs_base.artifacts_dir + "WorkingWithShapes.aspect_ratio_locked.docx") |
セル内の図形レイアウトを設定
is_layout_in_cell プロパティを使用して、図形をテーブルの内側に表示するかテーブルの外側に表示するかを指定することもできます。
次のコード例は、IsLayoutInCell プロパティを操作する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.start_table() | |
builder.row_format.height = 100 | |
builder.row_format.height_rule = aw.HeightRule.EXACTLY | |
for i in range(0, 31) : | |
if (i != 0 and i % 7 == 0) : | |
builder.end_row() | |
builder.insert_cell() | |
builder.write("Cell contents") | |
builder.end_table() | |
watermark = aw.drawing.Shape(doc, aw.drawing.ShapeType.TEXT_PLAIN_TEXT) | |
watermark.relative_horizontal_position = aw.drawing.RelativeHorizontalPosition.PAGE | |
watermark.relative_vertical_position = aw.drawing.RelativeVerticalPosition.PAGE | |
watermark.is_layout_in_cell = True # Display the shape outside of the table cell if it will be placed into a cell. | |
watermark.width = 300 | |
watermark.height = 70 | |
watermark.horizontal_alignment = aw.drawing.HorizontalAlignment.CENTER | |
watermark.vertical_alignment = aw.drawing.VerticalAlignment.CENTER | |
watermark.rotation = -40 | |
watermark.fill_color = drawing.Color.gray | |
watermark.stroke_color = drawing.Color.gray | |
watermark.text_path.text = "watermarkText" | |
watermark.text_path.font_family = "Arial" | |
watermark.name = "WaterMark_" + str(uuid.uuid4()) | |
watermark.wrap_type = aw.drawing.WrapType.NONE | |
run = doc.get_child_nodes(aw.NodeType.RUN, True)[doc.get_child_nodes(aw.NodeType.RUN, True).count - 1].as_run() | |
builder.move_to(run) | |
builder.insert_node(watermark) | |
doc.compatibility_options.optimize_for(aw.settings.MsWordVersion.WORD2010) | |
doc.save(docs_base.artifacts_dir + "WorkingWithShapes.layout_in_cell.docx") |
切り取り角長方形の作成
Aspose.Words を使用して角を切り取る長方形を作成できます。形状タイプは、SINGLE_CORNER_SNIPPED、TOP_CORNERS_SNIPPED、DIAGONAL_CORNERS_SNIPPED、TOP_CORNERS_ONE_ROUNDED_ONE_SNIPPED、SINGLE_CORNER_ROUNDED、TOP_CORNERS_ROUNDED、DIAGONAL_CORNERS_ROUNDED です。
DML 形状は、これらの形状タイプを使用して insert_shape メソッドを使用して作成されます。これらのタイプを使用して VML 形状を作成することはできません。 Shape クラスのパブリック コンストラクターを使用してシェイプを作成しようとすると、「NotSupportedException」例外が発生します。
次のコード例は、これらの種類の図形をドキュメントに挿入する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.insert_shape(aw.drawing.ShapeType.TOP_CORNERS_SNIPPED, 50, 50) | |
saveOptions = aw.saving.OoxmlSaveOptions(aw.SaveFormat.DOCX) | |
saveOptions.compliance = aw.saving.OoxmlCompliance.ISO29500_2008_TRANSITIONAL | |
doc.save(docs_base.artifacts_dir + "WorkingWithShapes.add_corners_snipped.docx", saveOptions) |
実際の形状の境界点を取得する
Aspose.Words API を使用すると、最上位のシェイプのアンカーを基準とした、ブロックを含むシェイプの位置とサイズをポイント単位で取得できます。これを行うには、bounds_in_points プロパティを使用します。
次のコード例は、BoundsInPoints プロパティを操作する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
shape = builder.insert_image(docs_base.images_dir + "Transparent background logo.png") | |
shape.aspect_ratio_locked = False | |
print("\nGets the actual bounds of the shape in points: ") | |
print(shape.get_shape_renderer().bounds_in_points) |
垂直アンカーを指定
vertical_anchor プロパティを使用して、図形内のテキストの垂直方向の配置を指定できます。
次のコード例は、VerticalAnchor プロパティを操作する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
textBox = builder.insert_shape(aw.drawing.ShapeType.TEXT_BOX, 200, 200) | |
textBox.text_box.vertical_anchor = aw.drawing.TextBoxAnchor.BOTTOM | |
builder.move_to(textBox.first_paragraph) | |
builder.write("Textbox contents") | |
doc.save(docs_base.artifacts_dir + "WorkingWithShapes.vertical_anchor.docx") |
SmartArt 形状の検出
Aspose.Words では、Shape に SmartArt
オブジェクトがあるかどうかを検出することもできます。これを行うには、has_smart_art プロパティ プロパティを使用します。
次のコード例は、HasSmartArt プロパティを操作する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "SmartArt.docx") | |
count = 0 | |
for shape in doc.get_child_nodes(aw.NodeType.SHAPE, True) : | |
shape = shape.as_shape() | |
if(shape.has_smart_art) : | |
count += 1 | |
print("The document has 0 shapes with SmartArt.", count) |
文書に横罫線を挿入する
insert_horizontal_rule メソッドを使用して、横罫線の形状を文書に挿入できます。
次のコード例は、これを行う方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.writeln("Insert a horizontal rule shape into the document.") | |
builder.insert_horizontal_rule() | |
doc.save(docs_base.artifacts_dir + "AddContentUsingDocumentBuilder.insert_horizontal_rule.docx") |
Aspose.Words API は、水平罫線形状のプロパティにアクセスするための horizontal_rule_format プロパティを提供します。 HorizontalRuleFormat クラスは、水平罫線の書式設定のための height、color、no_shade などの基本プロパティを公開します。
次のコード例は、HorizontalRuleFormat を設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
builder = aw.DocumentBuilder() | |
shape = builder.insert_horizontal_rule() | |
horizontalRuleFormat = shape.horizontal_rule_format | |
horizontalRuleFormat.alignment = aw.drawing.HorizontalRuleAlignment.CENTER | |
horizontalRuleFormat.width_percent = 70 | |
horizontalRuleFormat.height = 3 | |
horizontalRuleFormat.color = drawing.Color.blue | |
horizontalRuleFormat.no_shade = True | |
builder.document.save(docs_base.artifacts_dir + "AddContentUsingDocumentBuilder.horizontal_rule_format.docx") |
OLE オブジェクトをアイコンとして挿入
Aspose.Words API は、埋め込みまたはリンクされた OLE オブジェクトをアイコンとしてドキュメントに挿入する Shape.insert_ole_object_as_icon 関数を提供します。この機能ではアイコンファイルとキャプションを指定できます。 OLE
オブジェクト タイプは、ファイル拡張子を使用して検出されます。
次のコード例は、OLE オブジェクトをアイコンとしてドキュメントに挿入するように設定する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.insert_ole_object_as_icon(docs_base.my_dir + "Presentation.pptx", False, docs_base.images_dir + "Logo icon.ico", "My embedded file") | |
doc.save(docs_base.artifacts_dir + "WorkingWithOleObjectsAndActiveX.insert_ole_object_as_icon.docx") |
Math XML を含むシェイプをシェイプとして DOM にインポート
convert_shape_to_office_math プロパティを使用して、EquationXML の図形を Office Math オブジェクトに変換できます。このプロパティのデフォルト値は MS Word の動作に対応しています。つまり、方程式 XML を含む図形は Office 数学オブジェクトに変換されません。
次のコード例は、図形を Office Math オブジェクトに変換する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
loadOptions = aw.loading.LoadOptions() | |
loadOptions.convert_shape_to_office_math = True | |
doc = aw.Document(docs_base.my_dir + "Office math.docx", loadOptions) | |
doc.save(docs_base.artifacts_dir + "WorkingWithLoadOptions.convert_shape_to_office_math.docx", aw.SaveFormat.DOCX) |