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-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
//Free-floating shape insertion.
Shape shape = builder.insertShape(ShapeType.TEXT_BOX,
RelativeHorizontalPosition.PAGE, 100,
RelativeVerticalPosition.PAGE, 100,
50, 50,
WrapType.NONE);
shape.setRotation(30.0);
builder.writeln();
//Inline shape insertion.
shape = builder.insertShape(ShapeType.TEXT_BOX, 50, 50);
shape.setRotation(30.0);
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX);
// "Strict" or "Transitional" compliance allows to save shape as DML.
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);
dataDir = dataDir + "Shape_InsertShapeUsingDocumentBuilder_out.docx";
// Save the document to disk.
doc.save(dataDir, 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-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(dataDir + "Test.png");
shape.setAspectRatioLocked(true);
// Save the document to disk.
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc";
doc.save(dataDir);

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-Java
Document doc = new Document(dataDir + "LayoutInCell.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
watermark.isLayoutInCell(false); // Display the shape outside of table cell if it will be placed into a cell.
watermark.setWidth(300);
watermark.setHeight(70);
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
watermark.setRotation(-40);
watermark.getFill().setColor(Color.GRAY);
watermark.setStrokeColor(Color.GRAY);
watermark.getTextPath().setText("watermarkText");
watermark.getTextPath().setFontFamily("Arial");
watermark.setName("WaterMark_0");
watermark.setWrapType(WrapType.NONE);
Run run = (Run) doc.getChildNodes(NodeType.RUN, true).get(doc.getChildNodes(NodeType.RUN, true).getCount() - 1);
builder.moveTo(run);
builder.insertNode(watermark);
doc.getCompatibilityOptions().optimizeFor(MsWordVersion.WORD_2010);
// Save the document to disk.
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx";
doc.save(dataDir);

Tambahkan Sudut Terpotong

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-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertShape(ShapeType.TOP_CORNERS_SNIPPED, 50, 50);
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.DOCX);
so.setCompliance(OoxmlCompliance.ISO_29500_2008_TRANSITIONAL);
dataDir = dataDir + "AddCornersSnipped_out.docx";
//Save the document to disk.
doc.save(dataDir, 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-Java
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.insertImage(dataDir + "Test.png");
shape.setAspectRatioLocked(false);
System.out.print("\nGets the actual bounds of the shape in points. ");
System.out.println(shape.getShapeRenderer().getBoundsInPoints());

Tentukan Jangkar Vertikal

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

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-Java
Document doc = new Document(dataDir + "VerticalAnchor.docx");
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
int imageIndex = 0;
for (Shape textBoxShape : (Iterable<Shape>) shapes) {
if (textBoxShape != null) {
textBoxShape.getTextBox().setVerticalAnchor(TextBoxAnchor.BOTTOM);
}
}
doc.save(dataDir + "VerticalAnchor_out.docx");

Deteksi Bentuk SmartArt

Aspose.Words juga memungkinkan untuk mendeteksi apakah Bentuk tersebut memiliki objek SmartArt. Untuk melakukannya, gunakan properti HasSmartArt.

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-Java
Document doc = new Document(dataDir + "input.docx");
NodeCollection shapes = doc.getChildNodes(NodeType.SHAPE, true);
int count = 0;
for (Shape textBoxShape : (Iterable<Shape>) shapes) {
if (textBoxShape.hasSmartArt()) {
count++;
}
}
System.out.println("The document has " + count + " shapes with SmartArt.");

Format Aturan Horizontal

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

Aspose.Words API menyediakan properti HorizontalRuleFormat untuk mengakses properti bentuk aturan horizontal. Kelas HorizontalRuleFormat memperlihatkan properti dasar seperti Tinggi, Warna, NoShade, 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-Java
DocumentBuilder builder = new DocumentBuilder();
Shape shape = builder.insertHorizontalRule();
HorizontalRuleFormat horizontalRuleFormat = shape.getHorizontalRuleFormat();
horizontalRuleFormat.setAlignment(HorizontalRuleAlignment.CENTER);
horizontalRuleFormat.setWidthPercent(70);
horizontalRuleFormat.setHeight(3);
horizontalRuleFormat.setColor(Color.BLUE);
horizontalRuleFormat.setNoShade(true);
builder.getDocument().save("HorizontalRuleFormat.docx");