การทำงานกับรูปทรง
หัวข้อนี้อธิบายวิธีการทำงานโดยทางโปรแกรมกับรูปร่างโดยใช้ Aspose.Words
รูปร่างใน Aspose.Words แสดงถึงวัตถุในเลเยอร์การวาด เช่น รูปร่างอัตโนมัติ กล่องข้อความ รูปแบบอิสระ วัตถุ OLE ตัวควบคุม ActiveX หรือรูปภาพ เอกสาร Word สามารถมีรูปร่างที่แตกต่างกันตั้งแต่หนึ่งรูปร่างขึ้นไป รูปร่างใน Aspose.Words จะแสดงด้วยคลาส Shape
การแทรกรูปร่างโดยใช้ตัวสร้างเอกสาร
คุณสามารถแทรกรูปร่างอินไลน์ที่มีประเภทและขนาดที่ระบุ และรูปร่างลอยอิสระพร้อมตำแหน่ง ขนาด และประเภทการตัดข้อความที่ระบุลงในเอกสารโดยใช้วิธี InsertShape วิธี InsertShape อนุญาตให้แทรกรูปร่าง DML ลงในโมเดลเอกสาร ต้องบันทึกเอกสารในรูปแบบที่รองรับรูปร่าง DML มิฉะนั้นโหนดดังกล่าวจะถูกแปลงเป็นรูปร่าง VML ในขณะที่บันทึกเอกสาร
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแทรกรูปร่างประเภทเหล่านี้ลงในเอกสาร:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
//Free-floating shape insertion. | |
Shape shape = builder.InsertShape(ShapeType.TextBox, RelativeHorizontalPosition.Page, 100, RelativeVerticalPosition.Page, 100, 50, 50, WrapType.None); | |
shape.Rotation = 30.0; | |
builder.Writeln(); | |
//Inline shape insertion. | |
shape = builder.InsertShape(ShapeType.TextBox, 50, 50); | |
shape.Rotation = 30.0; | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.Docx); | |
// "Strict" or "Transitional" compliance allows to save shape as DML. | |
so.Compliance = OoxmlCompliance.Iso29500_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-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
dataDir = dataDir + "Shape_AspectRatioLocked_out.doc"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
กำหนดเค้าโครงรูปร่างในเซลล์
คุณยังสามารถระบุได้ว่าจะแสดงรูปร่างภายในตารางหรือภายนอกตารางโดยใช้คุณสมบัติ IsLayoutInCell
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ IsLayoutInCell:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + @"LayoutInCell.docx"); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape watermark = new Shape(doc, ShapeType.TextPlainText); | |
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page; | |
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page; | |
watermark.IsLayoutInCell = true; // Display the shape outside of table cell if it will be placed into a cell. | |
watermark.Width = 300; | |
watermark.Height = 70; | |
watermark.HorizontalAlignment = HorizontalAlignment.Center; | |
watermark.VerticalAlignment = VerticalAlignment.Center; | |
watermark.Rotation = -40; | |
watermark.Fill.Color = Color.Gray; | |
watermark.StrokeColor = Color.Gray; | |
watermark.TextPath.Text = "watermarkText"; | |
watermark.TextPath.FontFamily = "Arial"; | |
watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid()); | |
watermark.WrapType = WrapType.None; | |
Run run = doc.GetChildNodes(NodeType.Run, true)[doc.GetChildNodes(NodeType.Run, true).Count - 1] as Run; | |
builder.MoveTo(run); | |
builder.InsertNode(watermark); | |
doc.CompatibilityOptions.OptimizeFor(MsWordVersion.Word2010); | |
dataDir = dataDir + "Shape_IsLayoutInCell_out.docx"; | |
// Save the document to disk. | |
doc.Save(dataDir); |
สร้างสี่เหลี่ยมผืนผ้ามุม Snip
คุณสามารถสร้างสี่เหลี่ยมผืนผ้ามุมสนิปได้โดยใช้ 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-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
Shape shape = builder.InsertShape(ShapeType.TopCornersSnipped, 50, 50); | |
OoxmlSaveOptions so = new OoxmlSaveOptions(SaveFormat.Docx); | |
so.Compliance = OoxmlCompliance.Iso29500_2008_Transitional; | |
dataDir = dataDir + "AddCornersSnipped_out.docx"; | |
//Save the document to disk. | |
doc.Save(dataDir, so); |
รับคะแนนขอบเขตรูปร่างจริง
เมื่อใช้ Aspose.Words API คุณจะได้รับตำแหน่งและขนาดของรูปร่างที่มีบล็อกเป็นจุด สัมพันธ์กับจุดยึดของรูปร่างบนสุด เมื่อต้องการทำเช่นนี้ ใช้คุณสมบัติ BoundsInPoints
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ BoundsInPoints:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
var shape = builder.InsertImage(dataDir + "Test.png"); | |
shape.AspectRatioLocked = false; | |
Console.Write("\nGets the actual bounds of the shape in points."); | |
Console.WriteLine(shape.GetShapeRenderer().BoundsInPoints); |
ระบุจุดยึดแนวตั้ง
คุณสามารถระบุการจัดตำแหน่งข้อความในแนวตั้งภายในรูปร่างได้โดยใช้คุณสมบัติ VerticalAnchor
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ VerticalAnchor:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
Document doc = new Document(dataDir + @"VerticalAnchor.docx"); | |
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true); | |
Shape textBoxShape = shapes[0] as Shape; | |
if (textBoxShape != null) | |
{ | |
textBoxShape.TextBox.VerticalAnchor = 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-.NET | |
Document doc = new Document(dataDir + "input.docx"); | |
int count = 0; | |
foreach (Shape shape in doc.GetChildNodes(NodeType.Shape, true)) | |
{ | |
if (shape.HasSmartArt) | |
count++; | |
} | |
Console.WriteLine("The document has {0} shapes with SmartArt.", count); |
แทรกกฎแนวนอนลงในเอกสาร
คุณสามารถแทรกรูปร่างกฎแนวนอนลงในเอกสารโดยใช้วิธี InsertHorizontalRule
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการทำเช่นนี้:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
// Initialize document. | |
Document doc = new Document(); | |
DocumentBuilder builder = new DocumentBuilder(doc); | |
builder.Writeln("Insert a horizontal rule shape into the document."); | |
builder.InsertHorizontalRule(); | |
dataDir = dataDir + "DocumentBuilder.InsertHorizontalRule_out.doc"; | |
doc.Save(dataDir); |
Aspose.Words API จัดเตรียมคุณสมบัติ HorizontalRuleFormat เพื่อเข้าถึงคุณสมบัติของรูปร่างกฎแนวนอน คลาส HorizontalRuleFormat เปิดเผยคุณสมบัติพื้นฐาน เช่น ความสูง สี NoShade ฯลฯ สำหรับการจัดรูปแบบของกฎแนวนอน
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการตั้งค่า HorizontalRuleFormat:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
DocumentBuilder builder = new DocumentBuilder(); | |
Shape shape = builder.InsertHorizontalRule(); | |
HorizontalRuleFormat horizontalRuleFormat = shape.HorizontalRuleFormat; | |
horizontalRuleFormat.Alignment = HorizontalRuleAlignment.Center; | |
horizontalRuleFormat.WidthPercent = 70; | |
horizontalRuleFormat.Height = 3; | |
horizontalRuleFormat.Color = Color.Blue; | |
horizontalRuleFormat.NoShade = true; | |
builder.Document.Save("HorizontalRuleFormat.docx"); |
นำเข้ารูปร่างด้วย Math XML เป็นรูปร่างลงใน DOM
คุณสามารถใช้คุณสมบัติ ConvertShapeToOfficeMath เพื่อแปลงรูปร่างด้วย EquationXML เป็นวัตถุ Office Math ค่าเริ่มต้นของคุณสมบัตินี้สอดคล้องกับลักษณะการทำงานของ Microsoft Word กล่าวคือ รูปร่างที่มีสมการ XML จะไม่ถูกแปลงเป็นวัตถุทางคณิตศาสตร์ของ Office
ตัวอย่างโค้ดต่อไปนี้แสดงวิธีการแปลงรูปร่างเป็นวัตถุ Office Math:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-.NET | |
LoadOptions lo = new LoadOptions(); | |
lo.ConvertShapeToOfficeMath = true; | |
// Specify load option to use previous default behaviour i.e. convert math shapes to office math ojects on loading stage. | |
Document doc = new Document(dataDir + @"OfficeMath.docx", lo); | |
//Save the document into DOCX | |
doc.Save(dataDir + "ConvertShapeToOfficeMath_out.docx", SaveFormat.Docx); |