کار با Shapes

در این مبحث نحوه کار برنامه‌نویسی با اشکال با استفاده از Aspose.Words بحث می‌شود.

اشکال در Aspose.Words یک شی را در لایه ترسیم نشان می دهند، مانند AutoShape، textbox، freeform، Object OLE، کنترل ActiveX یا تصویر. یک سند Word می تواند شامل یک یا چند شکل مختلف باشد. اشکال سند توسط کلاس Shape نشان داده می شود.

درج اشکال با استفاده از Document Builder

می توانید شکل درون خطی با نوع و اندازه مشخص و شکل شناور آزاد با موقعیت، اندازه و نوع بسته بندی متن مشخص شده را با استفاده از روش 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 را مشخص کنید

با استفاده از ویژگی 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 است یا خیر. برای این کار از ویژگی دارایی_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 به عنوان یک نماد در سند را نشان می دهد:

# 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 به عنوان Shapes به DOM وارد کنید

می توانید از ویژگی convert_shape_to_office_math برای تبدیل اشکال با EquationXML به آبجکت های Office Math استفاده کنید. مقدار پیش‌فرض این ویژگی با رفتار MS Word مطابقت دارد، یعنی اشکال با معادله XML به اشیاء ریاضی Office تبدیل نمی‌شوند.

مثال کد زیر نحوه تبدیل اشکال به اشیاء ریاضی آفیس را نشان می دهد:

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