Şekillerle Çalışmak

Bu konu, Aspose.Words kullanarak şekillerle programlı olarak nasıl çalışılacağını anlatmaktadır.

Aspose.Words’teki şekiller çizim katmanındaki Otomatik Şekil, metin kutusu, serbest biçim, OLE nesnesi, ActiveX denetimi veya resim gibi bir nesneyi temsil eder. Bir Word belgesi bir veya daha fazla farklı şekil içerebilir. Aspose.Words’teki şekiller Shape sınıfı tarafından temsil edilir.

Belge Oluşturucuyu Kullanarak Şekil Ekleme

InsertShape yöntemini kullanarak bir belgeye, belirtilen tür ve boyutta satır içi şekil ve belirtilen konum, boyut ve metin sarma türüyle serbest kayan şekil ekleyebilirsiniz. InsertShape yöntemi, belge modeline DML şeklinin eklenmesine olanak sağlar. Belgenin DML şekillerini destekleyen formatta kaydedilmesi gerekir, aksi takdirde bu tür düğümler belge kaydedilirken VML şekline dönüştürülür.

Aşağıdaki kod örneği, bu tür şekillerin belgeye nasıl ekleneceğini gösterir:

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

En Boy Oranını Kilitli Olarak Ayarla

Aspose.Words’i kullanarak şeklin en boy oranının AspectRatioLocked özelliği aracılığıyla kilitlenip kilitlenmeyeceğini belirtebilirsiniz.

Aşağıdaki kod örneği, AspectRatioLocked özelliğiyle nasıl çalışılacağını gösterir:

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

Hücredeki Şekil Düzenini Ayarla

IsLayoutInCell özelliğini kullanarak şeklin tablonun içinde mi yoksa dışında mı görüntüleneceğini de belirleyebilirsiniz.

Aşağıdaki kod örneği, IsLayoutInCell özelliğiyle nasıl çalışılacağını gösterir:

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

Alıntı Köşesi Dikdörtgeni Oluştur

Aspose.Words’i kullanarak bir kesme köşesi dikdörtgeni oluşturabilirsiniz. Şekil türleri şunlardır: SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, ve DiagonalCornersRounded.

Bu şekil türleri ile InsertShape yöntemi kullanılarak DML şekli oluşturulur. Bu türler VML şekilleri oluşturmak için kullanılamaz. “Shape” sınıfının ortak yapıcısını kullanarak şekil oluşturma girişimi “NotSupportedException” istisnasına neden olur.

Aşağıdaki kod örneği, bu tür şekillerin belgeye nasıl ekleneceğini gösterir:

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

Gerçek Şekil Sınır Noktalarını Alın

Aspose.Words API’yi kullanarak, en üstteki şeklin bağlantısına göre, blok içeren şeklin konumunu ve boyutunu noktalar halinde alabilirsiniz. Bunu yapmak için BoundsInPoints özelliğini kullanın.

Aşağıdaki kod örneği, BoundsInPoints özelliğiyle nasıl çalışılacağını gösterir:

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

Dikey Bağlantıyı Belirtin

VerticalAnchor özelliğini kullanarak bir şeklin içindeki metnin dikey hizalamasını belirleyebilirsiniz.

Aşağıdaki kod örneği, VerticalAnchor özelliğiyle nasıl çalışılacağını gösterir:

// 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 Şeklini Algıla

Aspose.Words ayrıca Shape’in bir SmartArt nesnesine sahip olup olmadığını tespit etmeye de olanak tanır. Bunu yapmak için HasSmartArt özelliğini kullanın.

Aşağıdaki kod örneği, HasSmartArt özelliğiyle nasıl çalışılacağını gösterir:

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

Belgeye Yatay Cetvel Ekle

InsertHorizontalRule yöntemini kullanarak bir belgeye yatay kural şekli ekleyebilirsiniz.

Aşağıdaki kod örneği bunun nasıl yapılacağını gösterir:

// 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, yatay kural şeklinin özelliklerine erişim için HorizontalRuleFormat özelliğini sağlar. HorizontalRuleFormat sınıfı, yatay bir kuralın biçimlendirmesi için Yükseklik, Renk, NoShade vb. gibi temel özellikleri ortaya çıkarır.

Aşağıdaki kod örneği HorizontalRuleFormat‘in nasıl ayarlanacağını gösterir:

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

Matematik XML’li Şekilleri Şekiller olarak DOM’e aktarın

EquationXML içeren şekilleri Office Math nesnelerine dönüştürmek için ConvertShapeToOfficeMath özelliğini kullanabilirsiniz. Bu özelliğin varsayılan değeri Microsoft Word davranışına karşılık gelir; yani XML denklemine sahip şekiller Office matematik nesnelerine dönüştürülmez.

Aşağıdaki kod örneği, şekillerin Office Math nesnelerine nasıl dönüştürüleceğini gösterir:

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