W tym artykule omówiono, jak tworzyć diagram Visio w C#. Przedstawiono w nim proces krok po kroku i działający przykładowy kod, aby zautomatyzować tworzenie diagramu Visio w C#. Ponadto opisano w nim również eksportowanie diagramu wyjściowego Visio w różnych formatach plików.
Kroki tworzenia diagramu Visio w języku C#
- Zainstaluj interfejs API Aspose.Diagram, aby tworzyć diagramy programu Visio
- Utwórz instancję klasy Diagram
- Dodaj wzorzec za pomocą pliku szablonu
- Wstaw kształt prostokąta za pomocą metody AddShape() i ustaw różne właściwości
- Eksportuj diagram wyjściowy programu Visio za pomocą metody Save()
Te kroki przedstawiają przegląd tego, jak tworzyć Visio w C#. Najpierw utwórz diagram od podstaw i załaduj szablon główny. Wstaw kształt, określając różne parametry, takie jak wymiary, położenie i numer strony. Na koniec wyrenderuj diagram wyjściowy Visio jako plik VSDX, aby zakończyć proces.
Kod do tworzenia diagramu Visio programowo w języku C#
using Aspose.Diagram; | |
class CreateVisioDiagram | |
{ | |
static void Main() | |
{ | |
new License().SetLicense("license.lic");// Initialize the Drawing License | |
// Create a new instance of a diagram | |
Aspose.Diagram.Diagram diagram = new Aspose.Diagram.Diagram(); | |
// Define the name of the master (template) to be used for creating shapes | |
string masterName = "Rectangle"; | |
diagram.AddMaster("Basic Shapes.vss", masterName); | |
// Define the dimensions and position for the new shape | |
double width = 2, height = 2, pinX = 4.25, pinY = 4.5; | |
// Add a new rectangle shape to the diagram using the specified master | |
long rectangleId = diagram.AddShape(pinX, pinY, width, height, masterName, 0); | |
// Retrieve the shape by its ID for modification | |
Aspose.Diagram.Shape rectangle = diagram.Pages[0].Shapes.GetShape(rectangleId); | |
// Set the position of the shape by modifying its PinX and PinY properties | |
rectangle.XForm.PinX.Value = 5; | |
rectangle.XForm.PinY.Value = 5; | |
// Set the type of the shape to indicate it is a standard shape | |
rectangle.Type = Aspose.Diagram.TypeValue.Shape; | |
// Add text to the shape | |
rectangle.Text.Value.Add(new Aspose.Diagram.Txt("Aspose Diagram")); | |
// Apply a predefined text style to the shape's text | |
rectangle.TextStyle = diagram.StyleSheets[3]; | |
// Save the modified diagram to a file | |
diagram.Save("Visio_out.vsdx", Aspose.Diagram.SaveFileFormat.Vsdx); | |
} | |
} |
Możesz użyć tego fragmentu kodu, aby narysować dowolny diagram procesu w programie Visio w języku C#. Ten przykładowy kod jest wersją podstawową, ale możesz go ulepszyć, aby narysować kilka innych typów kształtów i łączników, aby spełnić swoje wymagania. Podobnie dostosuj format pliku wyjściowego, aby renderować wygenerowany rysunek jako obraz rastrowy, obraz wektorowy, PDF lub inne formaty w zależności od swoich wymagań.
W tym samouczku omówiono informacje dotyczące tworzenia diagramu Visio programowo w C#. Jeśli jednak jesteś zainteresowany eksploracją konwersji plików VSD, przeczytaj artykuł na Konwersja VSD do VSDX w C#.