LogoASPAlliance: Articles, reviews, and samples for .NET Developers
Converting Visio files in ASP.NET
by Jason N. Gaylord
Average Rating: 
Views (Total / Last 10 Days): 39303/ 66


The other day I found myself in a bind. I needed to find a way to convert Visio files quickly to *.jpg format. So I began to research different ways of importing the Visio library. I wanted to perform this task by using just notepad and the command prompt. So I did so.

To perform this task, you'll need:

  • Visio 2003
  • .NET Framework 1.1
  • Account credentials with permission to run Visio 2003
  • Read/Write permissions to your "Output" folder (Unless importing to SQL)
Now that you have everything you'll need, let's begin.
Section 1: Converting the VISLIB.dll file

In this example, we will regenerate the vislib.dll file into a .NET version. This is done by using the Type Library Import tool provided with the framework.

Go to a command prompt and navigate to C:\Program Files\Microsoft.NET\SDK\v1.1\Bin. Inside that folder, there is an application called tlbimp.exe. We are going to use that. So, type:

tlbimp "C:\Program Files\Microsoft Office\Visio11\VISLIB.dll" /out:"C:\VisioDotNet.dll" /namespace:VisioDotNet /asmversion:11.0.3216.0
In this example, we are generating a new .dll in the C: root called VisioDotNet.dll. We are using VisioDotNot as the namespace as well. Also, just so we know what version of Visio we are using, I looked up the version of the VISLIB.dll file and used that as the assembly version. In this case it was 11.0.3216.0. Now we have the VisioDotNet library for our project.
Section 2: Creating the custom Visio.dll file
The next thing to do is to create the custom .dll library to use in your project(s). We start off by importing the new library. Then, by using a class browser, we can find that utilizing the Application class we can open a file and export the file as a particular type.

1:    Imports VisioDotNet
3:    Namespace Utilities
5:        Public Class Visio
7:            Public Sub SaveFile(infile as String, outfile as String)
8:                Dim app as New Application
10:               app.Documents.Open(infile)
11:               app.ActiveWindow.Page.Export(outfile)
12:               app.Close()
13:           End Sub
15:       End Class
17:   End Namespace
After creating the vb file, we can then compile:
vbc /t:library /r:System.dll,VisioDotNet.dll Visio.vb /out:Utilities.Visio.dll
Don't forget that we need to give the proper access to start Visio on the server. Even though this is done silently, the process still needs access. We will accomplish this in the next section.
Section 3: Setting up the Web.config file
Now, we must be sure that the proper user has proper permissions to open Visio. So, we will impersonate a local user for this particular web. To further secure our app, we can limit this to a particular folder or page. However, this example will impersonate a user for the entire web -- not best practice :-).

1:    <?xml version="1.0" encoding="utf-8" ?>
2:    <configuration>
3:        <system.web>
4:            <identity impersonate="true" userName="TEST\VisioUser"  _
5:                password="visiorocks" />
6:        </system.web>
7:    </configuration>
Finally, we can setup our ASP.NET page.
Section 4: Creating the ASP.NET page
In our ASP.NET page, we will create a new instance of the .dll and then pass the input and output file types to it.

1:    <%@ Page Language="VB" %>
3:    <script runat="server">
4:        Sub ConvertFile(s as object, e as EventArgs)
5:            Dim doc As New Utilities.Visio
6:            doc.SaveFile(InputFile.Text,OutputFile.Text)
7:        End Sub
8:    </script>
11:   <html>
13:   <head>
14:       <title>Visio Conversion</title>
15:   </head>
18:   <body>
20:   <form id="visioConvert" runat="server">
21:       Enter the file to import:<br>
22:       <input id="InputFile" type="file" runat="server" /><br><br>
24:       Enter the name and extention of the file to export:<br>
25:       <input id="fileTo" type="file" runat="server" /><br><br>
27:       <asp:button id="button1" OnClick="ConvertFile"  _ 
28:           runat="server" text="Convert!" />
29:   </form>
31:   </body>
33:   </html>
It is recommended that you use regular expressions to regulate the types of files being imported and exported. This will limit the number of potential errors.

Hopefully this article has helped you get started with using Visio in your .NET applications.

Product Spotlight
Product Spotlight 

©Copyright 1998-2022  |  Page Processed at 2022-09-24 3:45:55 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search