Word格式處理控件Aspose.Words for .NET形狀處理教程——處理Aspose.Words中的形狀
Aspose.Words For .NET是一種高級Word文檔處理API,用于執(zhí)行各種文檔管理和操作任務。API支持生成,修改,轉換,呈現(xiàn)和打印文檔,而無需在跨平臺應用程序中直接使用Microsoft Word。此外,API支持所有流行的Word處理文件格式,并允許將Word文檔導出或轉換為固定布局文件格式和最常用的圖像/多媒體格式。
本文討論如何使用Aspose.Words以編程方式處理形狀。Aspose.Words中的形狀表示 繪圖層中的對象,例如AutoShape,文本框,自由格式,OLE對象,ActiveX控件或圖片。 Word文檔可以包含一個或多個不同的形狀。文檔的形狀由Shape類表示。
>>Aspose.Words for .NET已經(jīng)更新至v20.3,新增4大新功能,包括Xamarin不再需要單獨的DLL,F(xiàn)indReplaceOptions類擴展了新屬性,實現(xiàn)了“ Letterlike”符號的正確呈現(xiàn)以及支持在文本框范圍內動態(tài)拉伸圖像,以及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屬性,以訪問水平規(guī)則形狀的屬性。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函數(shù),以將嵌入式或鏈接的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),我們很高興為您提供查詢和咨詢。