Bekerja dengan Bentuk

Topik ini membahas cara bekerja secara terprogram dengan bentuk menggunakan Aspose.Words.

Bentuk di Aspose.Words mewakili objek di lapisan gambar, seperti BentukOtomatis, kotak teks, bentuk bebas, objek OLE, kontrol ActiveX, atau gambar. Dokumen Word bisa berisi satu atau lebih bentuk berbeda. Bentuk dokumen diwakili oleh kelas Shape.

Memasukkan Bentuk Menggunakan Pembuat Dokumen

Anda dapat menyisipkan bentuk sebaris dengan tipe dan ukuran tertentu serta bentuk mengambang bebas dengan posisi, ukuran, dan jenis bungkus teks tertentu ke dalam dokumen menggunakan metode insert_shape. Metode insert_shape memungkinkan penyisipan bentuk DML ke dalam model dokumen. Dokumen harus disimpan dalam format yang mendukung bentuk DML, jika tidak, node tersebut akan dikonversi ke bentuk VML, saat menyimpan dokumen.

Contoh kode berikut menunjukkan cara menyisipkan tipe bentuk ini ke dalam dokumen:

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

Setel Rasio Aspek Terkunci

Dengan menggunakan Aspose.Words, Anda dapat menentukan apakah rasio aspek bentuk dikunci melalui properti aspect_ratio_locked.

Contoh kode berikut menunjukkan cara bekerja dengan properti 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")

Atur Tata Letak Bentuk Di Sel

Anda juga dapat menentukan apakah bentuk ditampilkan di dalam tabel atau di luar tabel menggunakan properti is_layout_in_cell.

Contoh kode berikut menunjukkan cara bekerja dengan properti 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")

Buat Snip Sudut Persegi Panjang

Anda dapat membuat persegi panjang sudut snip menggunakan Aspose.Words. Tipe bentuknya adalah SINGLE_CORNER_SNIPPED, TOP_CORNERS_SNIPPED, DIAGONAL_CORNERS_SNIPPED, TOP_CORNERS_ONE_ROUNDED_ONE_SNIPPED, SINGLE_CORNER_ROUNDED, TOP_CORNERS_ROUNDED, dan DIAGONAL_CORNERS_ROUNDED.

Bentuk DML dibuat menggunakan metode insert_shape dengan tipe bentuk berikut. Tipe ini tidak dapat digunakan untuk membuat bentuk VML. Upaya membuat bentuk dengan menggunakan konstruktor publik kelas Shape memunculkan pengecualian “NotSupportedException”.

Contoh kode berikut menunjukkan cara menyisipkan tipe bentuk ini ke dalam dokumen:

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

Dapatkan Poin Batas Bentuk Aktual

Dengan menggunakan Aspose.Words API, Anda bisa mendapatkan lokasi dan ukuran bentuk yang berisi blok dalam titik, relatif terhadap jangkar bentuk paling atas. Untuk melakukan ini, gunakan properti bounds_in_points.

Contoh kode berikut menunjukkan cara bekerja dengan properti 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)

Tentukan Jangkar Vertikal

Anda dapat menentukan perataan vertikal teks dalam bentuk menggunakan properti vertical_anchor.

Contoh kode berikut menunjukkan cara bekerja dengan properti 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")

Deteksi Bentuk SmartArt

Aspose.Words juga memungkinkan untuk mendeteksi apakah Bentuk memiliki objek SmartArt. Untuk melakukan ini, gunakan properti properti has_smart_art.

Contoh kode berikut menunjukkan cara bekerja dengan properti 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)

Masukkan Aturan Horizontal ke dalam Dokumen

Anda dapat menyisipkan bentuk aturan horizontal ke dalam dokumen menggunakan metode insert_horizontal_rule.

Contoh kode berikut menunjukkan cara melakukannya:

# 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 menyediakan properti horizontal_rule_format untuk mengakses properti bentuk aturan horizontal. Kelas HorizontalRuleFormat memperlihatkan properti dasar seperti height, color, no_shade, dll. untuk pemformatan aturan horizontal.

Contoh kode berikut menunjukkan cara mengatur 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")

Sisipkan Objek OLE sebagai Ikon

Aspose.Words API menyediakan fungsi Bentuk.masukkan_ole_object_as_icon untuk menyisipkan objek OLE yang tertanam atau tertaut sebagai ikon ke dalam dokumen. Fungsi ini memungkinkan menentukan file ikon dan keterangannya. Jenis objek OLE harus dideteksi menggunakan ekstensi file.

Contoh kode berikut menunjukkan cara mengatur penyisipan objek OLE sebagai Ikon ke dalam dokumen:

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

Impor Bentuk dengan Math XML sebagai Bentuk ke DOM

Anda dapat menggunakan properti convert_shape_to_office_math untuk mengonversi bentuk dengan EquationXML ke objek Office Math. Nilai default properti ini sesuai dengan perilaku MS Word yaitu bentuk dengan persamaan XML tidak dikonversi ke objek matematika Office.

Contoh kode berikut memperlihatkan cara mengonversi bentuk menjadi objek 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)