العمل مع الأشكال
يناقش هذا الموضوع كيفية العمل برمجيًا مع الأشكال باستخدام 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); |
إنشاء مستطيل زاوية القصاصة
يمكنك إنشاء مستطيل زاوية باستخدام Aspose.Words. أنواع الأشكال هي SingleCornerSnipped، وTopCornerSnipped، و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); |