Advertisement

02.13.2008 at 09:37AM PST, ID: 23160373
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

using word interop library

Zone: .NET
Tags: asp.net
Hello Guys,
I am trying to use the console VB.net application script in the VB.net webform. Having no good knowledge of .NET I am having problems in the script attached.
The script should read the word document 2007 file and convert it to pdf.
your help on this  is much appreciated.
regards
sam
 
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
Imports microsoft.Office.Interop.Word
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim wordApplication As ApplicationClass = Nothing
    Dim wordDocument As Document = Nothing
    Dim paramSourceDocPath As String = "E:\test.docx"
    Dim paramExportFilePath As String = "E:\test.pdf"
    Dim paramExportFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
 
Try
        wordApplication = New ApplicationClass()
        wordDocument = wordApplication.Documents.Open(paramSourceDocPath)
 
        If Not wordDocument is Nothing Then
            wordDcoument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat)
        End If
 
     Catch ex As Exception
    Finally
        If Not wordDocument Is Nothing Then
            wordDocument.Close(False)
            wordDocument = Nothing
        End If
 
        If Not wordApplication Is Nothing Then
            wordApplication.Quit()
            wordApplication = Nothing
        End If
 
        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try
End Class
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: niidmore
Solution Provided By: athapa
Participating Experts: 2
Solution Grade: A
Views: 94
Translate:
Loading Advertisement...
02.13.2008 at 10:17AM PST, ID: 20886622

Rank: Wizard

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.13.2008 at 11:26AM PST, ID: 20887219

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.15.2008 at 05:34PM PST, ID: 20907507

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.17.2008 at 04:26PM PST, ID: 20916621

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.18.2008 at 05:00AM PST, ID: 20919441

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.18.2008 at 06:35AM PST, ID: 20920173

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.18.2008 at 06:27PM PST, ID: 20925244

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.19.2008 at 04:15AM PST, ID: 20927522

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.19.2008 at 07:18PM PST, ID: 20934747

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.20.2008 at 02:02AM PST, ID: 20936295

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.20.2008 at 03:34AM PST, ID: 20936739

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.21.2008 at 03:36PM PST, ID: 20953091

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.22.2008 at 02:10AM PST, ID: 20955931

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.23.2008 at 01:37AM PST, ID: 20964302

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.23.2008 at 01:38AM PST, ID: 20964306

Rank: Master

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
02.23.2008 at 02:13PM PST, ID: 20967002

All comments and solutions are available to Premium Service Members only.

Start your 7 day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Handhelds / PDAs
  • Displays / Monitors
  • Components
  • Networking Hardware
  • Peripherals
  • Laptops/Notebooks
  • Storage
  • Servers
  • Desktops
  • New Users
  • Misc
  • Apple
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMWare
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMWare
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Community Advisor
  • Lounge
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • Community Advisor
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
02.13.2008 at 10:17AM PST, ID: 20886622

Rank: Wizard

 
02.13.2008 at 11:26AM PST, ID: 20887219
hello emoreau,
that is not free and i cannot afford to buy it.
any idea where this code going wrong please?
any help suggestion/advice is much appreciated.
regards
sam
 
02.15.2008 at 05:34PM PST, ID: 20907507

Rank: Master

Put some code in the  try/catch to see what the problem is (unless you already know which line is throwing exception and what the exception is).


     Catch ex As Exception
           MessageBox.Show (ex.Message )  
           MessageBox.Show(ex.StackTrace)
    Finally

Post the error message you see.

BTW, is your code compiling?
 
02.17.2008 at 04:26PM PST, ID: 20916621
hello athapa,
thanks for your response.
Yes, it has compiled successfullly, no issues while building it.
however, i have managed to write another piece of code which could allow user to select any docx file  and on the button click it will convert into pdf.
The concern I have now is how I am going to call this piece of .net code into my asp page.
On my current ASP page, the user can see his data stored in the word 2007 file, the task I have is to somehow allow user to convert this docx file into pdf. perhaps by creating a button next to the file which could read "Convert to PDF" or something and when the user clicks this button I want to invoke .net script for this conversion. I am not sure how I am going to do this.
your help/advice/suggestion is much appreciated.
regards
sam
 
02.18.2008 at 05:00AM PST, ID: 20919441

Rank: Master

How are you displaying the docx file? Is that some file in the server or is that something users of your site will upload?

In the code snippet you have above, you're missing a function name.  I'm adding some missing pieces and moving the class to a new class.

Create a new class file (Such as DOCX2PDF.vb)


Imports microsoft.Office.Interop.Word
 
Public Class DOCX2PDF
' Partial Class _Default
'    Inherits System.Web.UI.Page

    'ADD START OF FUNCTION HERE
    Public Function ConvertDocxToPDF(paramSourceDocPath As String, paramExportFilePath As String) As Boolean 'Or whatever return type you need

    Dim wordApplication As ApplicationClass = Nothing
    Dim wordDocument As Document = Nothing

    'THE FOLLOWING 2 variables are passed as parameter
    'Dim paramSourceDocPath As String = "E:\test.docx"
    'Dim paramExportFilePath As String = "E:\test.pdf"
   
    Dim paramExportFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
 
Try
        wordApplication = New ApplicationClass()
        wordDocument = wordApplication.Documents.Open(paramSourceDocPath)
 
        If Not wordDocument is Nothing Then
            wordDcoument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat)

            Return TRUE
 
       End If
       

     Catch ex As Exception
           'LOG THIS ERROR SO THAT YOU CAN SEE WHERE THE PROBLEM IS
           'MessageBox won't work for ASP.Net so log that to an error log file, or db, or event log, etc.
           System.Diagnostics.EventLog.WriteEntry("DOCX2PDF",ex.Message)

           Return False

    Finally
        If Not wordDocument Is Nothing Then
            wordDocument.Close(False)
            wordDocument = Nothing
        End If
 
        If Not wordApplication Is Nothing Then
            wordApplication.Quit()
            wordApplication = Nothing
        End If
 
        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()
    End Try

    'End of Function
     End Function

End Class


Once you have your class ready then whenever you need to convert a document, you'll call the convert function (for example from your asp.net web page's button click event)

         DOCX2PDFConvertDocxToPDF(source, destination)

If the files you're displaying are in your server then you'd already know the source and destination path. If the user uploads documents using InputFile then you'd save that to any temp location (or whereever your app needs) and call that function.


It is not a good idea to use COM objects such as Office in your web application. The users won't see all the exceptions and dialog boxes raised by such COM objects. You could be leaking handles, and memories. Word COM object is slow too. If you only occasionally need to do this then it is ok.

 
02.18.2008 at 06:35AM PST, ID: 20920173
Hello athapa,
thanks for your reply mate, much appreciated.
yes, i have all the docx files stored on the web server, users generally get their docx file within their myaccoun section, and it is a classic asp website. Your above illustration has given me good idea of how I can make the script available on aspx pages.However, in my case I would have to call on the current asp page.
All I wish to do is to allow users to have this functionality on the click event  to  convert their docx to pdf file  and  have it available for them to view. Perhaps the best idea should be saving .pdf files on the webserver and make it available on the page after conversion.
i hope you understand my problem. please see attached snippet
thanks in advance for your advice.
regards
sam
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Office
Imports Microsoft.Office.Interop.Word
Public Class DOCX2PDF
Inherits System.Web.UI.Page
Protected objWord As Microsoft.Office.Interop.Word.ApplicationClass = New ApplicationClass()
Protected strPathToUpload As String
Protected strPathToConvert As String
Private fltDocFormat As Object = 17
'Private fltDocFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
Protected missing As Object = System.Reflection.Missing.Value
Protected [readOnly] As Object = False
Protected isVisible As Object = False
 
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
    'Code to check if user has selected any file on the form
 
    If Not (fUpload.HasFile) Then
        lblMessage.Text = "Please choose file to upload"
    Else
 
        Try
 
            'To check the file extension if it is word document or something else
 
            Dim strFileName As String = fUpload.FileName
 
                Dim strSep As String() = fUpload.FileName.Split("."c)
 
            Dim arrLength As Integer = strSep.Length - 1
 
            Dim strExt As String = strSep(arrLength).ToString().ToUpper()
 
            'Save the uploaded file to the folder
 
            strPathToUpload = Server.MapPath("Uploaded")
 
            'Map-path to the folder where html to be saved
 
                strPathToConvert = Server.MapPath("WordToPdf")
 
            Dim FileName As Object = strPathToUpload + "\" + fUpload.FileName
 
                Dim FileToSave As Object = strPathToConvert + "\" + Replace(fUpload.FileName, ".docx", "") + ".pdf"
                'Dim FileToSave As Object = strPathToConvert + "\test.xps"
 
            If strExt.ToUpper().Equals("DOCX") Then
 
                fUpload.SaveAs(strPathToUpload + "\" + fUpload.FileName)
 
                lblMessage.Text = "File uploaded successfully"
 
                'open the file internally in word. In the method all the parameters should be passed by object reference
 
                objWord.Documents.Open(FileName, [readOnly], missing, missing, missing, missing, _
                missing, missing, missing, missing, isVisible, missing, _
                missing, missing, missing, missing)
 
                'Do the background activity
 
                objWord.Visible = False
 
                Dim oDoc As Microsoft.Office.Interop.Word.Document = objWord.ActiveDocument
 
 
 
                    oDoc.SaveAs(FileToSave, fltDocFormat, missing, missing, missing, missing, _
                missing, missing, missing, missing, missing, missing, _
                missing, missing, missing, missing)
 
 
                    lblMessage.Text = fUpload.FileName + " converted  successfully"
            Else
 
 
 
                lblMessage.Text = "Invalid file selected!"
            End If
 
            'Close/quit word
 
 
            objWord.Quit(missing, missing, missing)
        Catch ex As Exception
 
 
 
            Response.Write(ex.Message)
 
        End Try
    End If
 
End Sub
 End Class
Open in New Window
 
02.18.2008 at 06:27PM PST, ID: 20925244

Rank: Master

I don't think  you'll be able to call asp.net functions from asp. If you rename asp pages to .aspx page then they'll run in IIS and then be able to use .Net dlls but I'm not sure if that will work in your situation.

I think you need to put the docx2pdf class in a vb.net class library project. Make the class COM visible. And use that as any other com object in your asp.

Take a look at this discussion.
http://www.experts-exchange.com/Programming/Languages/.NET/Q_20451993.html?qid=20451993

-AT
 
02.19.2008 at 04:15AM PST, ID: 20927522
hello there athapa,
ok thanks for the link above, will try to change the current asp page to aspx and see if the rest all works.
meanwhile, you could please let me know, if i just go add a new class and write the code as described above, how i am going to have that referenced on the aspx page ?
another thing, do i need to have word 2007 and the docx to pdf plugin installed? or just the plugin works?
one of my servers is on win2000 servers and i also cant see word 12.0 object library there?
please advice
regards
sam
 
02.19.2008 at 07:18PM PST, ID: 20934747

Rank: Master

You need to have Word 2007 installed in addition to the plugin in the server.

If you want to use docx2pdf in asp.net project then you can compile the class as a separate dll and add reference to that dll from your project which house the aspx. (Right Click Project in the Solution Explorer->Add References) Or you can just add the class within the same solution and you don't have to add reference to the dll.

For the legacy asp project you'd have to go the route as mentioned in the link above.

If you want, you can upload relevant code (or email) where you're having problem

AT
 
02.20.2008 at 02:02AM PST, ID: 20936295
Hello Athapa,
Thanks for your advice.
I have a situation now and I dont really understand which is the best approach for this.
As you know I have two different conversion program, one done in vb.net console and the other on aspx page on the click event. I dont know which one would fit for my purpose.
On my .asp page(is on different server), I have added an extra form and onsubmit doing a ajax request to another server where i have kept this script having word 2007 installed.
I have to do some ajax call from the .asp page, the asp page should send the .docx file to the server and the response should be the pdf link and some confirmation message.
my e-mail address is niidmore@yahoo.com please drop your address there.
regards
sam
 
02.20.2008 at 03:34AM PST, ID: 20936739
Hello Athapa,
Please advice me how I should be doing an ajax call to the aspx page from test.asp.
And that I would need to change the onclikc_event and place the conversion script straight in the class.
Please help.
Regards
Sam
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
test.asp
--------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<script type="text/javascript">
function ajaxFunction()
{
  var xmlHttp;
  xmlHttp=new XMLHttpRequest();
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.myForm.viewPDF.value=xmlHttp.responseText;
      }
    }
  xmlHttp.open("GET","http://12.345.23.1/doc2pdf/convert.aspx",true);
  xmlHttp.send(null);
  }
</script>
</head>
<body>
<form name="myForm">
<a href="browser.docx">Document</a> <input type="button" onClick="ajaxFunction();" value="Convert"/>
<div id="viewPDF"></div>
</form>
</body>
</html>
 
convert.aspx
------------
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports Microsoft.Office
Imports Microsoft.Office.Interop.Word
 
 
Partial Public Class _Default
    Inherits System.Web.UI.Page
    Protected objWord As Microsoft.Office.Interop.Word.ApplicationClass = New ApplicationClass()
    ' Protected objWord As Microsoft.Office.Interop.Word.Application = New ApplicationId()
    'This creates new object of Word.ApplicationClass
    Protected strPathToUpload As String
    'Path to upload files "Uploaded"
    Protected strPathToConvert As String
    'Path to convert uploaded files and save
    Private fltDocFormat As Object = 17
    'Private fltDocFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
    'For filtered PDF Output
    Protected missing As Object = System.Reflection.Missing.Value
    'Is just to skeep the parameters which are passed as boject reference, these are seems to be optional parameters
    Protected [readOnly] As Object = False
    Protected isVisible As Object = False
    'The process has to be in invisible mode
 
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
 
        'Code to check if user has selected any file on the form
    If Not (fUpload.HasFile) Then
        lblMessage.Text = "Please choose file to upload"
        Else
            Try
                'To check the file extension if it is word document or something else
                Dim strFileName As String = fUpload.FileName
                Dim strSep As String() = fUpload.FileName.Split("."c)
                Dim arrLength As Integer = strSep.Length - 1
                Dim strExt As String = strSep(arrLength).ToString().ToUpper()
                'Save the uploaded file to the folder
                strPathToUpload = Server.MapPath("Uploaded")
                'Map-path to the folder where html to be saved
                strPathToConvert = Server.MapPath("WordToPdf")
                Dim FileName As Object = strPathToUpload + "\" + fUpload.FileName
                Dim FileToSave As Object = strPathToConvert + "\" + Replace(fUpload.FileName, ".docx", "") + ".pdf"
                'Dim FileToSave As Object = strPathToConvert + "\test.xps"
 
                If strExt.ToUpper().Equals("DOCX") Then
                    fUpload.SaveAs(strPathToUpload + "\" + fUpload.FileName)
                    lblMessage.Text = "File uploaded successfully"
                    'open the file internally in word. In the method all the parameters should be passed by object reference
                    objWord.Documents.Open(FileName, [readOnly], missing, missing, missing, missing, _
                                            missing, missing, missing, missing, isVisible, missing, _
                                            missing, missing, missing, missing)
                    'Do the background activity
                    objWord.Visible = False
 
                    Dim oDoc As Microsoft.Office.Interop.Word.Document = objWord.ActiveDocument
                    oDoc.SaveAs(FileToSave, fltDocFormat, missing, missing, missing, missing, _
                                missing, missing, missing, missing, missing, missing, _
                                missing, missing, missing, missing)
 
                    lblMessage.Text = fUpload.FileName + " converted  successfully to pdf"
                Else
                    lblMessage.Text = "Invalid file selected!"
                End If
                'Close/quit word
                objWord.Quit(missing, missing, missing)
            Catch ex As Exception
                Response.Write(ex.Message)
            End Try
        End If
End Sub
End Class
Open in New Window
 
02.21.2008 at 03:36PM PST, ID: 20953091

Rank: Master

Assuming the docx files are stored in a location accessible to the aspx web application. (You'll  have to impersonate an account which has access to the server\share because by default  your asp.net application is running with a credential of local account.)

Here a version I created which seem to work but without understanding your end goal, I won't be able to say if this will help you or not. So please let me know how users will be viewing docx and when they would like to view pdf.

Anyway, asp page will send request to asp.net application with filename to convert.  (Folder for docx and pdfx are defined as constant value in both codes (asp as well as asp.net).
The asp.net page will look at the request parameter, convert the docx to pdf and return the pdf name. All these needs to happen in the page_load event and not in any button click.

One important note. I don't use Office 2007 (didn't wanted to learn the same thing again so I switched to Linux). So, I didn't have the option to save as pdf. So I used save as rtf option
    Private fltDocFormat As Object = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
'--------------THIS IS ASP PAGE (OR REGULAR HTML)--------------
<html>
    <head>
        <title>Untitled Page</title>
        <script type="text/javascript">
            function ajaxFunction()
            {
              var pdfpath = "http://localhost/WebApplication1/pdf/";
              var convertserver = "http://localhost/WebApplication2/convert.aspx?filetoconvert=";
              var sourcedocx = "testdocument.docx";
              //right now the code has hardcoded document name
              //get this value from other fields in the page or other source
              
              var xmlHttp;
              xmlHttp=new XMLHttpRequest();
              xmlHttp.onreadystatechange=function()
                {
                if(xmlHttp.readyState==4)
                  {
                    //document.myForm.viewPDF.value=xmlHttp.responseText;
                    var resp = xmlHttp.responseText;
                    alert(resp);
                    pdfpath = pdfpath + resp;
                    document.getElementById("PDFView").href = pdfpath;     
                    document.getElementById("PDFTitle").style.visibility='visible'; 
                  }
                }
              
              xmlHttp.open("GET",convertserver + sourcedocx, true);
              xmlHttp.send(null);
              }
        </script>
    </head>
    <body>
        <form name="myForm" action ="">            
            <input type="button" onclick="ajaxFunction();" value="Convert"/>                        
            <a href="" id="PDFView">
                <p id="PDFTitle" style="position: relative; left: 0px; top: 0px; visibility:hidden"> 
                Click here to download the PDF.               
                </p>
            </a> 
        </form>
    </body>
</html>
 
 
'-----THIS IS ASP.Net CODE - convert.aspx (Create a blank asp.net page and put these codes) --------------
 
Public Partial Class convert
    Inherits System.Web.UI.Page
 
    Protected objWord As Microsoft.Office.Interop.Word.ApplicationClass = New Microsoft.Office.Interop.Word.ApplicationClass()
    ' Protected objWord As Microsoft.Office.Interop.Word.Application = New ApplicationId()
    'This creates new object of Word.ApplicationClass
    Protected strPathToUpload As String = "\\server\share\docxfolder"
    'Path to upload files "Uploaded"
    Protected strPathToConvert As String = "\\server\share\pdffolder"
    'Path to convert uploaded files and save
    Private fltDocFormat As Object = 17
    'Private fltDocFormat As WdExportFormat = WdExportFormat.wdExportFormatPDF
    'For filtered PDF Output
    Protected missing As Object = System.Reflection.Missing.Value
    'Is just to skeep the parameters which are passed as boject reference, these are seems to be optional parameters
    Protected [readOnly] As Object = False
    Protected isVisible As Object = False
    'The process has to be in invisible mode
 
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            'get file name
            Dim strFileName As String = Request.Params.Item("filetoconvert")
            If strFileName <> Nothing AndAlso strFileName <> "" Then
 
                Dim FileName As Object = strPathToUpload + "\" & strFileName
                Dim PDFFileName As String = Replace(strFileName, ".docx", "") + ".pdf"
                Dim FileToSave As Object = strPathToConvert + "\" & PDFFileName
           
 
                'open the file internally in word. In the method all the parameters should be passed by object reference
                objWord.Documents.Open(FileName, [readOnly], missing, missing, missing, missing, _
                                        missing, missing, missing, missing, isVisible, missing, _
                                        missing, missing, missing, missing)
                'Do the background activity
                objWord.Visible = False
 
                Dim oDoc As Microsoft.Office.Interop.Word.Document = objWord.ActiveDocument
                oDoc.SaveAs(FileToSave, fltDocFormat, missing, missing, missing, missing, _
                            missing, missing, missing, missing, missing, missing, _
                            missing, missing, missing, missing)
 
                'Close/quit word
                objWord.Quit(missing, missing, missing)
 
                Response.Clear()
                Response.Write(PDFFileName)
                Response.End()
                Response.Close()
            End If
        Catch ex As Exception
            If Not (ex.Message = "Thread was being aborted.") Then
                Response.Write(ex.Message)
            End If
        End Try
 
    End Sub
 
End Class
Open in New Window
 
02.22.2008 at 02:10AM PST, ID: 20955931
hello again,
client have an area in the CMS where they can add the data into the field and the value will be stored into .docx file which is visible to them if they have added the content to it.
Can you test the script on the unix pc? i thought you would definitely need windows for it?
I think I am almost there now, I need to little trick on my asp page which could see if the .pdf already available on that location, if it is already there, i must show the pdf link otherwise, the convert on that place.
so the user can only see this for the first time, as there is only one docx to convert, it this is done then its done.
thanks for your help
regards
PS: can you also help me out on the database issue i have now please? there is one open question out there
 
02.23.2008 at 01:37AM PST, ID: 20964302

Rank: Master

It would be difficult to check if pdf document exist from Javascript because it is running in the client side. You could connect to the asp server from Javascript to see if pdf already exist or you could just leave the asp code as is and have the asp.net code return the name of existing pdf instead of converting again. However if the determination of whether pdf exists or not is made in the asp page before the page is send out to client then you should do that instead of using Javascript.

As for linux, client side javascript should work just fine. I did have issue in Firefox in Ubuntu. So I had to slightly modify the code to add error handling and request permission from the browser.

Replace this

                 xmlHttp.open("GET",convertserver + sourcedocx, true);

with

            try{
                  xmlHttp.open("GET",convertserver + sourcedocx, true);
              }
            catch(ex)
            {
                  if (ex=="Permission denied to call method XMLHttpRequest.open")
              {
                 try
                 {
                     netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
                 }
                 catch (e)
                 {
                     alert("Permission denied by browser.");
                 }
                  try{
                        xmlHttp.open("GET",convertserver + sourcedocx, true);
                    }
                  catch(ex)
                  {
                    alert(ex);
                  }
                  }
              }
Accepted Solution
 
02.23.2008 at 01:38AM PST, ID: 20964306

Rank: Master

post your question link or email to thapa_a @ hotmail .com
 
 
20080236-EE-VQP-29 / EE_QW_2_20070628