Bekerja dengan Bentuk

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

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

Sisipkan Bentuk Menggunakan Pembuat Dokumen

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

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

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

Setel Rasio Aspek Terkunci

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

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

Mengatur Tata Letak Bentuk dalam sel

Anda juga dapat menentukan apakah bentuk ditampilkan di dalam tabel atau di luarnya menggunakan properti IsLayoutInCell.

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

Buat Persegi Panjang Sudut Snip

Anda dapat membuat persegi panjang sudut snip menggunakan Aspose.Words. Jenis bentuknya adalah *SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded,*dan DiagonalCornersRounded.

Bentuk DML dibuat menggunakan metode InsertShape dengan tipe bentuk ini. Tipe ini tidak dapat digunakan untuk membuat bentuk VML. Mencoba membuat bentuk dengan menggunakan konstruktor publik dari kelas “Bentuk” memunculkan pengecualian " NotSupportedException".

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

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

Dapatkan Titik Batas Bentuk Aktual

Dengan menggunakan Aspose.Words API, Anda bisa mendapatkan lokasi dan ukuran bentuk yang berisi blok dalam poin, relatif terhadap jangkar bentuk paling atas. Untuk melakukannya, gunakan properti BoundsInPoints.

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-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;

Format Aturan Horizontal

Aspose.Words API menyediakan properti HorizontalRuleFormat untuk mengakses properti bentuk aturan horizontal. Kelas HorizontalRuleFormat memperlihatkan properti dasar seperti Tinggi, Warna, Bayangan, dll. untuk pemformatan aturan horizontal.

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

Sisipkan Objek OLE sebagai Ikon

Aspose.Words API menyediakan fungsi ShapeInsertOleObjectAsIcon untuk menyisipkan objek OLE yang disematkan atau ditautkan sebagai ikon ke dalam dokumen. Fungsi ini memungkinkan menentukan file ikon dan keterangan. Jenis objek OLE harus dideteksi menggunakan ekstensi file.

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

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;