کار با Shapes

این مبحث نحوه کار برنامه‌نویسی با اشکال با استفاده از Aspose.Words را مورد بحث قرار می‌دهد.

اشکال در Aspose.Words یک شی را در لایه ترسیم نشان می دهند، مانند AutoShape، textbox، freeform، Object OLE، کنترل ActiveX یا تصویر. یک سند Word می تواند شامل یک یا چند شکل مختلف باشد. اشکال در Aspose.Words با کلاس Shape نشان داده می شوند.

درج اشکال با استفاده از Document Builder

می توانید شکل درون خطی با نوع و اندازه مشخص و شکل شناور آزاد با موقعیت، اندازه و نوع بسته بندی متن مشخص شده را با استفاده از روش 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، TopCornersSnipped، DiagonalCornersSnipped، TopCornersOneRoundedOneSnipped، SingleCornerRounded، TopCornersRounded و DiagonalCornersRounded.

شکل DML با استفاده از روش InsertShape با این انواع شکل ایجاد می شود. این انواع را نمی توان برای ایجاد اشکال VML استفاده کرد. تلاش برای ایجاد شکل با استفاده از سازنده عمومی کلاس “Shape” استثنای “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);

Vertical Anchor را مشخص کنید

با استفاده از ویژگی 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 همچنین اجازه می دهد تا تشخیص دهد که آیا Shape دارای یک شی 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 ویژگی های اساسی مانند Height، Color، 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 به عنوان Shapes به DOM وارد کنید

می توانید از ویژگی ConvertShapeToOfficeMath برای تبدیل اشکال با EquationXML به آبجکت های Office Math استفاده کنید. مقدار پیش فرض این ویژگی با رفتار Microsoft Word مطابقت دارد، یعنی اشکال با معادله XML به اشیاء ریاضی Office تبدیل نمی شوند.

مثال کد زیر نحوه تبدیل اشکال به اشیاء ریاضی آفیس را نشان می دهد:

// 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);