翻譯|使用教程|編輯:李顯亮|2020-12-03 10:19:11.807|閱讀 516 次
概述:為了使VSDX操作自動化,本文為您提供了有關如何在C#中從頭創建VSDX圖的基礎教程。此外,它還介紹了如何從.NET應用程序中在VSDX圖中插入頁面,形狀和文本。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
MS Visio是一種流行的應用程序,可讓您創建各種圖,例如流程圖,數據流程圖,業務流程模型等。VSDX是MS Visio用于存儲圖的文件格式。
Aspose.Diagram for .NET是專門用于處理Microsoft Visio文件的API。它允許開發人員創建,操作和轉換本機Visio文件格式。使用Aspose.Diagram for .NET,您不僅可以繪制基本但復雜的形狀,如 Bezier,Spline,Polyline,并且只使用幾行代碼。
(安裝包僅提供部分功能,并設置限制,如需試用完整功能請。)
為了使VSDX操作自動化,本文為您提供了有關如何在C#中從頭創建VSDX圖的基礎教程。此外,它還介紹了如何從.NET應用程序中在VSDX圖中插入頁面,形狀和文本。
使用C#創建MS Visio VSDX圖
首先,讓我們從頭開始創建一個空的VSDX圖。以下是執行此操作的步驟:
下面的代碼示例演示如何在C#中創建MS Visio VSDX圖表。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_Diagrams(); // Create directory if it is not already present. bool IsExists = System.IO.Directory.Exists(dataDir); if (!IsExists) System.IO.Directory.CreateDirectory(dataDir); // Initialize a new Visio Diagram diagram = new Diagram(); dataDir = dataDir + "CreateDiagram_out.vsdx"; // Save in the VSDX format diagram.Save(dataDir, SaveFileFormat.VSDX);
在C#中將Master添加到VSDX圖
母版用于添加模具,該模具包含將在圖中使用的形狀的集合。如果要添加母版,則需要VSS模板文件和母版ID。以下是使用Aspose.Diagram向Visio圖表添加母版的步驟。
下面的代碼示例演示如何使用C#將母版添加到Visio圖。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_Master(); // Load diagram Diagram diagram = new Diagram(); // Load stencil to a stream string templateFileName = dataDir + "NetApp-FAS-series.vss"; Stream stream = new FileStream(templateFileName, FileMode.Open); // Add master with stencil file path and master id string masterName = "FAS80xx rear empty"; diagram.AddMaster(templateFileName, 2); // Add master with stencil file path and master name diagram.AddMaster(templateFileName, masterName); // Add master with stencil file stream and master id diagram.AddMaster(stream, 2); // Adds master to diagram from source diagram Diagram src = new Diagram(templateFileName); diagram.AddMaster(src, masterName); // Add master with stencil file stream and master id diagram.AddMaster(stream, masterName); // Adds shape with defined PinX and PinY. diagram.AddShape(2.0, 2.0, masterName, 0); diagram.AddShape(6.0, 6.0, masterName, 0); // Adds shape with defined PinX,PinY,Width and Height. diagram.AddShape(7.0, 3.0, 1.5, 1.5, masterName, 0);
在C#中的Visio圖表中插入頁面
MS Visio圖表由一頁或多頁組成,每頁都包含圖表。因此,在添加形狀之前,您需要使用以下步驟添加頁面。
下面的代碼示例演示如何使用C#在Visio VSDX圖中添加頁面。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_VisioPages(); // Load diagram Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); // It calculates max page id int max = 0; if (diagram.Pages.Count != 0) max = diagram.Pages[0].ID; for (int i = 1; i < diagram.Pages.Count; i++) { if (max < diagram.Pages[i].ID) max = diagram.Pages[i].ID; } // Set max page ID int MaxPageId = max; // Initialize a new page object Page newPage = new Page(); // Set name newPage.Name = "new page"; // Set page ID newPage.ID = MaxPageId + 1; // Or try the Page constructor // Page newPage = new Page(MaxPageId + 1); // Add a new blank page diagram.Pages.Add(newPage); // Save diagram diagram.Save(dataDir + "InsertBlankPage_out.vsdx", SaveFileFormat.VSDX);
使用C#在VSDX圖中創建形狀
形狀是Visio圖的基礎。MS Visio支持各種形狀,可以在各個領域中創建圖表。以下步驟顯示了如何在Visio圖表中插入形狀。
下面的代碼示例演示如何使用C#在VSDX圖中添加形狀。
// The path to the documents directory. string dataDir = RunExamples.GetDataDir_Shapes(); // Load a diagram Diagram diagram = new Diagram(dataDir + "Drawing1.vsdx"); // Get page by name Page page = diagram.Pages.GetPage("Page-2"); // Add master with stencil file path and master name string masterName = "Rectangle"; diagram.AddMaster(dataDir + "Basic Shapes.vss", masterName); // Page indexing starts from 0 int PageIndex = 1; double width = 2, height = 2, pinX = 4.25, pinY = 4.5; // Add a new rectangle shape long rectangleId = diagram.AddShape(pinX, pinY, width, height, masterName, PageIndex); // Set shape properties Shape rectangle = page.Shapes.GetShape(rectangleId); rectangle.XForm.PinX.Value = 5; rectangle.XForm.PinY.Value = 5; rectangle.Type = TypeValue.Shape; rectangle.Text.Value.Add(new Txt("Aspose Diagram")); rectangle.TextStyle = diagram.StyleSheets[3]; rectangle.Line.LineColor.Value = "#ff0000"; rectangle.Line.LineWeight.Value = 0.03; rectangle.Line.Rounding.Value = 0.1; rectangle.Fill.FillBkgnd.Value = "#ff00ff"; rectangle.Fill.FillForegnd.Value = "#ebf8df"; diagram.Save(dataDir + "AddShape_out.vsdx", SaveFileFormat.VSDX); Console.WriteLine("Shape has been added.");
在C#中的Visio頁面中添加文本形狀
在各種情況下,您還需要向Visio圖中添加文本。為此,您可以按照以下步驟操作。
下面的代碼示例演示如何使用C#在VSDX圖中添加文本。
// For complete examples and data files, please go to //github.com/aspose-diagram/Aspose.Diagram-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_ShapeText(); // Create a new diagram Diagram diagram = new Diagram(); // Set parameters and add text to a Visio page double PinX = 1, PinY = 1, Width = 1, Height = 1; diagram.Pages[0].AddText(PinX, PinY, Width, Height, "Test text"); // Save diagram diagram.Save(dataDir + "InsertTextShape_out.vsdx", SaveFileFormat.VSDX);
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn