کار با اشکال
این موضوع در مورد چگونگی کار برنامه نویسی با اشکال با استفاده از Aspose.Words بحث می کند.
اشکال در Aspose.Words یک شی را در لایه نقاشی نشان می دهند، مانند AutoShape، textbox، freeform، OLE object، ActiveX control یا picture. یک سند ورد می تواند شامل یک یا چند شکل مختلف باشد. شکل های سند توسط کلاس 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); |
ایجاد مستطیل گوشه 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-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.Words API، می توانید مکان و اندازه شکل حاوی بلوک را در نقاط نسبت به لنگر بالاترین شکل بدست آورید. برای این کار از خاصیت 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.Words API ویژگی 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.Words API تابع Shape → InsertOleObjectAsIcon را برای قرار دادن یک شیء جاسازی شده یا مرتبط OLE به عنوان یک آیکون در سند فراهم می کند. این تابع اجازه می دهد تا فایل آیکون و عنوان را مشخص کنید. نوع شی OLE
باید با استفاده از پسوند فایل شناسایی شود.
مثال کد زیر نشان می دهد که چگونه OLE object را به عنوان یک آیکون در سند قرار دهید:
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; |