สร้างไดอะแกรม Visio ใน C#

บทความนี้จะอธิบายวิธีการสร้างไดอะแกรม Visio ใน C# โดยจะแบ่งปันขั้นตอนและตัวอย่างโค้ดการทำงานเพื่อให้การสร้างไดอะแกรม Visio ใน C# เป็นแบบอัตโนมัติ นอกจากนี้ ยังครอบคลุมถึงการส่งออกไดอะแกรม Visio ในรูปแบบไฟล์ต่างๆ อีกด้วย

ขั้นตอนในการสร้างไดอะแกรม Visio ใน C#

  1. ติดตั้ง Aspose.Diagram API เพื่อสร้างไดอะแกรม Visio
  2. สร้างอินสแตนซ์ของคลาส Diagram
  3. เพิ่มต้นแบบโดยใช้ไฟล์สเตนซิล
  4. แทรกรูปสี่เหลี่ยมผืนผ้าโดยใช้เมธอด AddShape() และตั้งค่าคุณสมบัติต่างๆ
  5. ส่งออกไดอะแกรม Visio เอาท์พุตโดยใช้เมธอด Save()

ขั้นตอนเหล่านี้ให้ภาพรวมเกี่ยวกับวิธีการสร้าง Visio ใน C# ขั้นแรก ให้สร้างไดอะแกรมขึ้นมาใหม่ และโหลดสเตนซิลต้นแบบ แทรกรูปร่างโดยระบุพารามิเตอร์ต่างๆ เช่น ขนาด ตำแหน่ง และหมายเลขหน้า สุดท้าย ให้เรนเดอร์ไดอะแกรม Visio เอาต์พุตเป็นไฟล์ VSDX เพื่อดำเนินการขั้นตอนให้เสร็จสมบูรณ์

โค้ดสำหรับสร้าง Visio Diagram ด้วยโปรแกรม 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);
}
}

คุณสามารถใช้ตัวอย่างโค้ดนี้เพื่อวาดไดอะแกรมกระบวนการใดๆ ใน Visio ใน C#* โค้ดตัวอย่างนี้เป็นเวอร์ชันพื้นฐาน แต่คุณสามารถปรับปรุงให้สามารถวาดรูปร่างและตัวเชื่อมต่อประเภทอื่นๆ ได้ตามความต้องการของคุณ ในทำนองเดียวกัน ให้ปรับรูปแบบไฟล์เอาท์พุตเพื่อเรนเดอร์ภาพวาดที่สร้างขึ้นเป็นภาพแรสเตอร์ ภาพเวกเตอร์ PDF หรือรูปแบบอื่นๆ ตามความต้องการของคุณ

บทช่วยสอนนี้ครอบคลุมข้อมูลในการ สร้างไดอะแกรม Visio ด้วยโปรแกรมใน C# อย่างไรก็ตาม หากคุณสนใจที่จะสำรวจการแปลงไฟล์ VSD โปรดอ่านบทความใน แปลง VSD เป็น VSDX ใน C#

 ไทย