如何在 C# 中读取 Visio 文档内容

发布于 2024-12-10 09:48:46 字数 647 浏览 0 评论 0原文

我的DLL库代码如下:

using System;
using IVisio=Microsoft.Office.Interop.Visio;

namespace Emix
{
public class Visio
{
    protected String path;

    public Visio(String path)
    {
        this.path = path;
    }

    public void open()
    {
        try
        {
            IVisio.Document doc = new IVisio.Application().Documents.Add(this.path);
            Console.WriteLine("Number of pages: " + doc.Pages.Count);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
}

但是,这段代码打开Visio编辑器,然后显示文档中的页数。

是否可以在不打开 Visio 的情况下读取此文件内容

My DLL library code is as follows:

using System;
using IVisio=Microsoft.Office.Interop.Visio;

namespace Emix
{
public class Visio
{
    protected String path;

    public Visio(String path)
    {
        this.path = path;
    }

    public void open()
    {
        try
        {
            IVisio.Document doc = new IVisio.Application().Documents.Add(this.path);
            Console.WriteLine("Number of pages: " + doc.Pages.Count);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }
}
}

However, this code opens the Visio editor and then displays the number of pages in the document.

Is it possible to read this file content without opening the Visio?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月静好 2024-12-17 09:48:46

这是如何在 visio 中读取形状属性的程序...

    namespace VisioEventsExample
    {
    using System;
    using Microsoft.Office.Interop.Visio;

    class Program
    {
        public static void Main(string[] args)
        {
            // Open up one of Visio's sample drawings.
            Application app = new Application();
            Document doc = app.Documents.Open(
                @"C:\Program Files\Microsoft Office\Office14\visio content\1033\ASTMGT_U.VST");

            // Get the first page in the sample drawing.
            Page page = doc.Pages[1];

            // Start with the collection of shapes on the page and 
            // print the properties we find,
            printProperties(page.Shapes);
        }

        /* This function will travel recursively through a collection of 
         * shapes and print the custom properties in each shape. 
         * 
         * The reason I don't simply look at the shapes in Page.Shapes is 
         * that when you use the Group command the shapes you group become 
         * child shapes of the group shape and are no longer one of the 
         * items in Page.Shapes.
         * 
         * This function will not recursive into shapes which have a Master. 
         * This means that shapes which were created by dropping from stencils 
         * will have their properties printed but properties of child shapes 
         * inside them will be ignored. I do this because such properties are 
         * not typically shown to the user and are often used to implement 
         * features of the shapes such as data graphics.
         * 
         * An alternative halting condition for the recursion which may be 
         * sensible sense for many drawing types would be to stop when you 
         * find a shape with custom properties.
         */
        public static void printProperties(Shapes shapes)
        {
            // Look at each shape in the collection.
            foreach (Shape shape in shapes)
            {               
                // Use this index to look at each row in the properties 
                // section.
                short iRow = (short) VisRowIndices.visRowFirst;

                // While there are stil rows to look at.
                while (shape.get_CellsSRCExists(
                    (short) VisSectionIndices.visSectionProp, 
                    iRow, 
                    (short) VisCellIndices.visCustPropsValue,
                    (short) 0) != 0)
                {
                    // Get the label and value of the current property.
                    string label = shape.get_CellsSRC(
                            (short) VisSectionIndices.visSectionProp, 
                            iRow,
                            (short) VisCellIndices.visCustPropsLabel
                        ).get_ResultStr(VisUnitCodes.visNoCast);

                    string value = shape.get_CellsSRC(
                            (short) VisSectionIndices.visSectionProp, 
                            iRow,
                            (short) VisCellIndices.visCustPropsValue
                        ).get_ResultStr(VisUnitCodes.visNoCast);

                    // Print the results.
                    Console.WriteLine(string.Format(
                        "Shape={0} Label={1} Value={2}",
                        shape.Name, label, value));

                    // Move to the next row in the properties section.
                    iRow++;
                }

                // Now look at child shapes in the collection.
                if (shape.Master == null && shape.Shapes.Count > 0)
                    printProperties(shape.Shapes);
            }
        }
    }
}

上面的示例假设您有 Visio Primary Interop Assembly 安装在您的电脑上,并且您已在项目中包含 Microsoft.Office.Interop.Visio 的引用者。

我希望这会对您有所帮助......

This is the program for how to read shape properties in visio....

    namespace VisioEventsExample
    {
    using System;
    using Microsoft.Office.Interop.Visio;

    class Program
    {
        public static void Main(string[] args)
        {
            // Open up one of Visio's sample drawings.
            Application app = new Application();
            Document doc = app.Documents.Open(
                @"C:\Program Files\Microsoft Office\Office14\visio content\1033\ASTMGT_U.VST");

            // Get the first page in the sample drawing.
            Page page = doc.Pages[1];

            // Start with the collection of shapes on the page and 
            // print the properties we find,
            printProperties(page.Shapes);
        }

        /* This function will travel recursively through a collection of 
         * shapes and print the custom properties in each shape. 
         * 
         * The reason I don't simply look at the shapes in Page.Shapes is 
         * that when you use the Group command the shapes you group become 
         * child shapes of the group shape and are no longer one of the 
         * items in Page.Shapes.
         * 
         * This function will not recursive into shapes which have a Master. 
         * This means that shapes which were created by dropping from stencils 
         * will have their properties printed but properties of child shapes 
         * inside them will be ignored. I do this because such properties are 
         * not typically shown to the user and are often used to implement 
         * features of the shapes such as data graphics.
         * 
         * An alternative halting condition for the recursion which may be 
         * sensible sense for many drawing types would be to stop when you 
         * find a shape with custom properties.
         */
        public static void printProperties(Shapes shapes)
        {
            // Look at each shape in the collection.
            foreach (Shape shape in shapes)
            {               
                // Use this index to look at each row in the properties 
                // section.
                short iRow = (short) VisRowIndices.visRowFirst;

                // While there are stil rows to look at.
                while (shape.get_CellsSRCExists(
                    (short) VisSectionIndices.visSectionProp, 
                    iRow, 
                    (short) VisCellIndices.visCustPropsValue,
                    (short) 0) != 0)
                {
                    // Get the label and value of the current property.
                    string label = shape.get_CellsSRC(
                            (short) VisSectionIndices.visSectionProp, 
                            iRow,
                            (short) VisCellIndices.visCustPropsLabel
                        ).get_ResultStr(VisUnitCodes.visNoCast);

                    string value = shape.get_CellsSRC(
                            (short) VisSectionIndices.visSectionProp, 
                            iRow,
                            (short) VisCellIndices.visCustPropsValue
                        ).get_ResultStr(VisUnitCodes.visNoCast);

                    // Print the results.
                    Console.WriteLine(string.Format(
                        "Shape={0} Label={1} Value={2}",
                        shape.Name, label, value));

                    // Move to the next row in the properties section.
                    iRow++;
                }

                // Now look at child shapes in the collection.
                if (shape.Master == null && shape.Shapes.Count > 0)
                    printProperties(shape.Shapes);
            }
        }
    }
}

The above example assumes that you have the Visio Primary Interop Assembly installed on you PC and that you have included a referee to Microsoft.Office.Interop.Visio in your project.

I hope this will helps you ....

娇纵 2024-12-17 09:48:46

如果您使应用程序不可见,您将看不到新的 Visio 实例打开(仍然会有一个 Visio 实例在运行,只是不可见)

Application application = new Application();  
application.Visible = false; 
Document doc = application.Documents.Add(this.path);
Console.WriteLine("Number of pages: " + doc.Pages.Count);

If you make the application invisible, you will not see the new instance of Visio opening (there will still be a instance of Visio running, just not a visible one)

Application application = new Application();  
application.Visible = false; 
Document doc = application.Documents.Add(this.path);
Console.WriteLine("Number of pages: " + doc.Pages.Count);
小…红帽 2024-12-17 09:48:46

Take a look at How to get shape data information on Visio with C#. It contains some useful information regarding your scenario

This post also explains how to get all shapes and additionally how to connect all shapes togather

梦明 2024-12-17 09:48:46

正如其他答案之一指出的那样,您可以使用 Application.Visible 属性来隐藏正在运行的 Visio 实例。

您的另一个选择是直接读取 Visio 的 VDX 文件格式来检索页数、自定义属性等。这避免了必须启动 Visio 才能从文件中读取信息。但此解决方案确实需要您成为 VDX 的文件 XML 模式。

As one of the other answers points out you can use the Application.Visible property to hide the running instance of Visio.

Another option for you is to read the Visio's VDX file format directly to retrieve the number of pages, custom properties, etc. This avoids have to start Visio to read information from the file. But this solution does require you to become the VDX's files XML schema.

梦晓ヶ微光ヅ倾城 2024-12-17 09:48:46

正如其他答案之一指出的那样,您可以使用 Application.Visible 属性来隐藏正在运行的 Visio 实例。

您的另一个选择是直接读取 Visio 的 VDX 文件格式来检索页数、自定义属性等。这避免了必须启动 Visio 才能从文件中读取信息。但此解决方案确实需要您成为 VDX 的文件 XML 模式。

As one of the other answers points out you can use the Application.Visible property to hide the running instance of Visio.

Another option for you is to read the Visio's VDX file format directly to retrieve the number of pages, custom properties, etc. This avoids have to start Visio to read information from the file. But this solution does require you to become the VDX's files XML schema.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文