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已经更新至最新版,新增4大新功能,包括Xamarin不再需要单独的DLL,FindReplaceOptions类扩展了新属性,实现了“ Letterlike”符号的正确呈现以及支持在文本框范围内动态拉伸图像,以及3钟增强型功能,点击下载体验
Aspose文档管理产品 Aspose技术交流群(761297826)
使用文档生成器插入形状
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.");
相关文章
- 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式
- Word控件Spire.Doc 【页面设置】教程(1):在C#/VB.NET:在 Word 文档中插入分页符
- Word控件Spire.Doc 【段落处理】教程(十三):如何在 C#、VB.NET 中将 RTF 字符串插入 Word 文档
- Word控件Spire.Doc 【段落处理】教程(五):如在 C#、VB.NET 中将 Word 段落从一个文档复制到另一个文档
- Word控件Spire.Doc 【文档操作】教程(二十):通过比较 C#/VB.NET 中的两个 Word 文档来获取差异
- Word控件Spire.Doc 转换教程(八):如何在 C#、VB.NET 中将 RTF 转换为 HTML
- Word控件Spire.Doc 【文档操作】教程(七):在 C#、VB.NET 中从 URL 下载 Word 文档
- Word控件Spire.Doc 转换教程(三十):C#、VB.NET 在 Azure 应用程序中将 Word 转换为 PDF
- Word控件Spire.Doc 【段落处理】教程(十四):如C#/VB.NET:删除 Word 中的空行
- Word格式处理控件Aspose.Words for .NET教程——更新和删除字段
- Word格式处理控件Aspose.Words for .NET教程——使用DOM插入字段
- Word格式处理控件Aspose.Words for .NET教程——设置字体格式并获取行距
- Aspose.Words for .NET使用表格教程之处理合并的单元格
- Aspose.Words for .NET使用文档教程(2):克隆文档
- Aspose.Words for .NET使用教程(十二):在Word文档中使用图表轴
- asp.net-Creating JavaScript objects from ASP.NET objects
- ASP.NET开发中修改代码而不重启网站