การทำงานกับรูปร่าง

หัวข้อนี้อธิบายถึงวิธีการทำงานตามโปรแกรมที่มีรูปร่างโดยใช้Aspose.Words.

รูปร่างในAspose.Wordsเป็นตัวแทนของวัตถุในเลเยอร์รูปวาดเช่นAutoShape,กล่องข้อความ,แบบอิสระ,OLEวัตถุ,การควบ เอกสารคำสามารถประกอบด้วยรูปทรงที่แตกต่างกันอย่างน้อยหนึ่ง รูปร่างของเอกสารจะแสดงโดยคลาสShape.

แทรกรูปร่างโดยใช้ตัวสร้างเอกสาร

คุณสามารถแทรกรูปร่างแบบอินไลน์ที่มีชนิดและขนาดที่ระบุและรูปร่างแบบลอยตัวได้ด้วยตำแหน่งขนาดและชนิดการตัดข้อความที่ระบุในเอกสารโดยใช้วิธีการInsertShape วิธีInsertShapeอนุญาตให้แทรกDMLรูปร่างลงในโมเดลเอกสาร เอกสารจะต้องถูกบันทึกในรูปแบบที่รองรับDMLรูปร่างมิฉะนั้นโหนดดังกล่าวจะถูกแปลงเป็นVMLรูปร่างในขณะที่บันทึกเอกสาร.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกรูปร่างชนิดนี้ลงในเอกสาร:

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

ตั้งค่าอัตราส่วนภาพล็อค

ใช้Aspose.Wordsคุณสามารถระบุว่าอัตราส่วนของรูปร่างถูกล็อคผ่านคุณสมบัติAspectRatioLocked.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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);

ตั้งรูปแบบรูปร่างในเซลล์

นอกจากนี้คุณยังสามารถระบุว่ารูปร่างจะแสดงภายในตารางหรือภายนอกโดยใช้คุณสมบัติIsLayoutInCell.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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);

เพิ่มมุมตัด

คุณสามารถสร้างสี่เหลี่ยมมุมตัดด้วยAspose.Words ประเภทรูปร่างคือSingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded,และDiagonalCornersRounded.

รูปร่างDMLถูกสร้างขึ้นโดยใช้วิธีการInsertShapeกับประเภทรูปร่างเหล่านี้ ชนิดเหล่านี้ไม่สามารถใช้เพื่อสร้างVMLรูปร่าง พยายามที่จะสร้างรูปร่างโดยใช้ตัวสร้างสาธารณะของชั้นเรียน"รูปร่าง"เพิ่มข้อยกเว้น"NotSupportedException".

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการแทรกชนิดของรูปร่างเหล่านี้ลงในเอกสาร:

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

รับรูปร่างขอบเขตที่เกิดขึ้นจริงจุด

ใช้Aspose.WordsAPIคุณจะได้รับตำแหน่งและขนาดของรูปร่างที่มีบล็อกในจุดเมื่อเทียบกับสมอของรูปร่างบ เมื่อต้องการทำเช่นนี้ให้ใช้คุณสมบัติBoundsInPoints.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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());

ระบุสมอแนวตั้ง

คุณสามารถระบุการจัดตำแหน่งข้อความในแนวตั้งภายในรูปร่างโดยใช้คุณสมบัติVerticalAnchor.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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");

ตรวจหาSmartArtรูปร่าง

Aspose.Wordsยังช่วยให้การตรวจสอบว่ารูปร่างมีวัตถุSmartArt เมื่อต้องการทำเช่นนี้ให้ใช้คุณสมบัติHasSmartArt.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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.");

รูปแบบกฎแนวนอน

คุณสามารถแทรกรูปร่างกฎแนวนอนลงในเอกสารโดยใช้วิธีการInsertHorizontalRule.

Aspose.WordsAPIให้HorizontalRuleFormatคุณสมบัติในการเข้าถึงคุณสมบัติของรูปร่างกฎแนวนอน HorizontalRuleFormatชั้นแสดงคุณสมบัติพื้นฐานเช่นความสูง,สี,NoShadeฯลฯ สำหรับการจัดรูปแบบของกฎแนวนอน.

ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่า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");