العمل مع الأشكال

يناقش هذا الموضوع كيفية العمل برمجيا مع الأشكال باستخدام 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.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 كائن كرمز في المستند:

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;