Şekillerle Çalışma

Bu konuda Aspose.Words kullanarak şekillerle programlı olarak nasıl çalışılacağı anlatılmaktadır.

Aspose.Words içindeki şekiller, çizim katmanındaki AutoShape, metin kutusu, serbest form, OLE nesnesi, ActiveX denetimi veya resim gibi bir nesneyi temsil eder. Bir Word belgesi bir veya daha fazla farklı şekil içerebilir. Belgenin şekilleri Shape sınıfı ile 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 kaydırma türünde serbest kayan şekil ekleyebilirsiniz. InsertShape yöntemi, belge modeline DML şeklinin eklenmesine izin verir. Belge, DML şekilleri destekleyen biçimde kaydedilmelidir, aksi takdirde belge kaydedilirken bu tür düğümler 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-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);

En Boy Oranını Ayarla Kilitli

Aspose.Words kullanarak, şeklin en boy oranının AspectRatioLocked özelliği aracılığıyla kilitlenip kilitlenmediğini belirleyebilirsiniz.

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

Hücrede Şekil Düzenini Ayarlama

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

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

Keskin Nişancı Köşesi Dikdörtgeni Oluştur

Aspose.Words kullanarak bir kesme köşesi dikdörtgeni oluşturabilirsiniz. Şekil türleri SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded, ve DiagonalCornersRounded. ‘dir

DML şekli, bu şekil türleriyle InsertShape yöntemi kullanılarak oluşturulur. Bu türler VML şekiller oluşturmak için kullanılamaz. “Shape” sınıfının genel yapıcısını kullanarak şekil oluşturma girişimi “NotSupportedException” istisnasını yükseltir.

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

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

Aspose.Words API kullanarak, blok içeren şeklin konumunu ve boyutunu, en üstteki şeklin çapasına göre 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-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;

Yatay Kural Biçimi

Aspose.Words API yatay kural şeklinin özelliklerine erişmek için HorizontalRuleFormat özelliğini sağlar. HorizontalRuleFormat sınıfı, Yükseklik, Renk, Gölge vb. Gibi temel özellikleri ortaya çıkarır. yatay bir kuralın biçimlendirilmesi için.

Aşağıdaki kod örneği nasıl ayarlanacağını gösterir 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 Nesnesini Simge Olarak Ekle

Aspose.Words API belgeye simge olarak katıştırılmış veya bağlantılı bir OLE nesne eklemek için ShapeInsertOleObjectAsIcon işlevi sağlar. Bu işlev, simge dosyasının ve başlığın belirtilmesine izin verir. OLE nesne türü, dosya uzantısı kullanılarak algılanacaktır.

Aşağıdaki kod örneği, ınsert OLE nesnesinin belgeye Simge olarak nasıl ayarlanacağını gösterir:

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;