使用形状

本主题讨论如何使用Aspose.Words以编程方式处理形状。

Aspose.Words中的形状表示绘图层中的对象,例如AutoShape、textbox、freeform、OLE对象、ActiveX控件或图片。 Word文档可以包含一个或多个不同的形状。 文档的形状由Shape类表示。

使用文档生成器插入形状

您可以使用InsertShape方法将具有指定类型和大小的内联形状以及具有指定位置,大小和文本换行类型的自由浮动形状插入文档。 InsertShape方法允许在文档模型中插入DML形状。 文档必须以支持DML形状的格式保存,否则,这样的节点将被转换为VML形状,同时保存文档。

下面的代码示例演示如何将这些类型的形状插入到文档中:

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

设置长宽比锁定

使用Aspose.Words,您可以指定是否通过AspectRatioLocked属性锁定形状的宽高比。

下面的代码示例演示如何使用AspectRatioLocked属性:

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

在单元格中设置形状布局

您还可以使用IsLayoutInCell属性指定形状是显示在表内部还是表外部。

下面的代码示例演示如何使用IsLayoutInCell属性:

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

创建剪断角矩形

您可以使用Aspose.Words创建剪断角矩形。 形状类型为SingleCornerSnipped, TopCornersSnipped, DiagonalCornersSnipped, TopCornersOneRoundedOneSnipped, SingleCornerRounded, TopCornersRounded,DiagonalCornersRounded.

使用具有这些形状类型的InsertShape方法创建DML形状。 这些类型不能用于创建VML形状。 尝试使用"Shape"类的公共构造函数创建shape会引发"NotSupportedException"异常。

下面的代码示例演示如何将这些类型的形状插入到文档中:

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

获取实际形状边界点

使用Aspose.WordsAPI,您可以获得包含块的形状的位置和大小(以点为单位),相对于最顶层形状的锚点。 为此,请使用BoundsInPoints属性。

下面的代码示例演示如何使用BoundsInPoints属性:

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;

水平规则格式

Aspose.WordsAPI提供HorizontalRuleFormat属性来访问水平规则形状的属性。 HorizontalRuleFormat类公开了基本属性,如高度,颜色,阴影等。 用于水平规则的格式设置。

下面的代码示例演示如何设置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对象作为图标

Aspose.WordsAPI提供ShapeInsertOleObjectAsIcon函数,将嵌入或链接的OLE对象作为图标插入到文档中。 此功能允许指定图标文件和标题。 应使用文件扩展名检测OLE对象类型。

下面的代码示例演示如何将insertOLE对象设置为文档中的图标:

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;