使用形状
本主题讨论如何使用 Aspose.Words 以编程方式处理形状。
Aspose.Words 中的形状表示绘图层中的对象,例如自选图形、文本框、自由格式、OLE 对象、ActiveX 控件或图片。 Word 文档可以包含一种或多种不同的形状。文档的形状由 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 提供 Shape.insert_ole_object_as_icon 功能,将嵌入或链接的 OLE 对象作为图标插入到文档中。此功能允许指定图标文件和标题。应使用文件扩展名检测 OLE
对象类型。
下面的代码示例演示了如何设置将 OLE 对象作为 Icon 插入到文档中:
# 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) |