การทำงานกับรูปร่าง
หัวข้อนี้อธิบายถึงวิธีการทำงานตามโปรแกรมที่มีรูปร่างโดยใช้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-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); |
ตั้งค่าอัตราส่วนภาพล็อค
ใช้Aspose.Wordsคุณสามารถระบุว่าอัตราส่วนของรูปร่างถูกล็อคผ่านคุณสมบัติAspectRatioLocked.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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); |
ตั้งรูปแบบรูปร่างในเซลล์
นอกจากนี้คุณยังสามารถระบุว่ารูปร่างจะแสดงภายในตารางหรือภายนอกโดยใช้คุณสมบัติIsLayoutInCell.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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); |
สร้างมุมตัดสี่เหลี่ยมผืนผ้า
คุณสามารถสร้างสี่เหลี่ยมมุมตัดด้วย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-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); |
รับรูปร่างขอบเขตที่เกิดขึ้นจริงจุด
ใช้Aspose.WordsAPIคุณจะได้รับตำแหน่งและขนาดของรูปร่างที่มีบล็อกในจุดเมื่อเทียบกับสมอของรูปร่างบ เมื่อต้องการทำเช่นนี้ให้ใช้คุณสมบัติBoundsInPoints.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการทำงานกับคุณสมบัติ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; |
รูปแบบกฎแนวนอน
Aspose.WordsAPIให้คุณสมบัติHorizontalRuleFormatเพื่อเข้าถึงคุณสมบัติของรูปร่างกฎแนวนอน HorizontalRuleFormatชั้นแสดงคุณสมบัติพื้นฐานเช่นความสูง,สี,สี,ฯลฯ สำหรับการจัดรูปแบบของกฎแนวนอน.
ตัวอย่างรหัสต่อไปนี้สาธิตวิธีการตั้งค่า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"); |
แทรกOLEวัตถุเป็นไอคอน
Aspose.WordsAPIให้Shapeฟังก์ชัน{InsertOleObjectAsIconเพื่อแทรกวัตถุฝังตัวหรือเชื่อมโยงOLEเป็นไอคอนในเอกสาร ฟังก์ชันนี้อนุญาตให้ระบุแฟ้มไอคอนและคำอธิบายภาพได้ ชนิดของวัตถุOLE
จะถูกตรวจพบโดยใช้นามสกุลของไฟล์.
ตัวอย่างรหัสต่อไปนี้แสดงวิธีการตั้งค่าแทรกOLEวัตถุเป็นไอคอนในเอกสาร:
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; |