עבודה עם Shapes

נושא זה דן כיצד לעבוד באופן מתודולוגי עם צורות באמצעות Aspose.Words.

הצורות Aspose.Words מייצג אובייקט בשכבת הציור, כגון AutoShape, תיבת טקסט, freeform, OLE object, ActiveX Control או תמונה. מסמך Word יכול להכיל צורות שונות או יותר. צורות Aspose.Words מיוצגים על ידי Shape מעמד.

המונחים: shapes Using Document Builder

באפשרותך להוסיף טופס inline עם סוג וגודל מוגדר וצורה חופשית עם המיקום שצוין, גודל וטקסט עטופה סוג לתוך מסמך באמצעות מסמך באמצעות המסמך באמצעות השימוש. InsertShape שיטה. The The The 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);

תגית: Aspect Ratio Locked

שימוש 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);

המונחים: shape Layout in Cell

אתה יכול גם לציין אם הצורה מוצגת בתוך שולחן או בחוץ באמצעות זה. 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);

תגית: Snip Corner Rectangle

אתה יכול ליצור מלבן פינה נופי באמצעות Aspose.Words. סוגי הצורה הם *SingleCornerSnipped, TopCornerssSnipped, DiagonalCornerssSnipped, TopCorners OneRounded OneCornersRounded, TopCornersRounded, * ו- TopCornersRounded OneRounded Ones. 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);

עקבו אחרי Actual shape Bounds

שימוש 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");

Detect Smart אמנות

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

הכנס Horizontal חוק לתוך מסמך

אתה יכול להוסיף צורה של שלטון אופקי לתוך מסמך באמצעות 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 רכוש לגשת למאפיינים של צורת השלטון האופקי. The The The 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");

ייבוא Shapes with Math XML as Shapes into DOM

אתה יכול להשתמש ConvertShapeToOfficeMath נכס להמיר את הצורות עם EquationXML לאובייקטים של Office Math. ערך ברירת המחדל של נכס זה תואם Microsoft Word התנהגות, כלומר צורות עם משוואה XML אינן מומרות לאובייקטים במתמטיקה של Office.

הדוגמה הקודית הבאה מראה כיצד להמיר צורות לאובייקטים של 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);