Word格式处理控件Aspose.Words for .NET形状处理教程——处理Aspose.Words中的形状
Aspose.Words For .NET是一种高级Word文档处理API,用于执行各种文档管理和操作任务。API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word。此外,API支持所有流行的Word处理文件格式,并允许将Word文档导出或转换为固定布局文件格式和最常用的图像/多媒体格式。
本文讨论如何使用Aspose.Words以编程方式处理形状。Aspose.Words中的形状表示 绘图层中的对象,例如AutoShape,文本框,自由格式,OLE对象,ActiveX控件或图片。 Word文档可以包含一个或多个不同的形状。文档的形状由Shape类表示。
>>Aspose.Words for .NET已经更新至v20.3,新增4大新功能,包括Xamarin不再需要单独的DLL,FindReplaceOptions类扩展了新属性,实现了“ Letterlike”符号的正确呈现以及支持在文本框范围内动态拉伸图像,以及3钟增强型功能,点击下载体验
使用文档生成器插入形状
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);
设置宽高比锁定
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);
在单元格中设置形状布局
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);
添加角落
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);
获取实际形状边界点
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);
指定垂直锚
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形状
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);
水平尺格式
Aspose.Words API提供Shape.HorizontalRuleFormat属性,以访问水平规则形状的属性。HorizontalRuleFormat类公开了用于设置水平尺的基本属性,例如Height,Color,NoShade等。下面的代码示例演示如何设置HorizontalRuleFormat。
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");
插入OLE对象作为图标
Aspose.Words API提供了Shape.InsertOleObjectAsIcon函数,以将嵌入式或链接的OLE对象作为图标插入文档。此功能允许指定图标文件和标题。OLE对象类型应使用文件扩展名进行检测。下面的代码示例演示如何将OLE对象作为Icon设置到文档中。
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape shape = builder.InsertOleObjectAsIcon(dataDir + "embedded.xlsx", false, dataDir + "icon.ico", "My embedded file");
doc.Save(dataDir + "EmbeddeWithIcon_out.docx");
Console.WriteLine("The document has been saved with OLE Object as an Icon.");
还想要更多吗?您可以点击阅读【2019 · Aspose最新资源整合】,查找需要的教程资源。如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线