Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » VB.NET » Utilities » Downloads
 
Add your own
alternative version
Go to top

Document conversion with OpenOffice

, 28 Aug 2005
Convert documents to HTML and PDF using OpenOffice.
oo2html.zip
oo2html
bin
oo2html.exe
oo2html.vshost.exe
sxw2html.exe
sxw2html.vshost.exe
My Project
Application.myapp
Settings.settings
oo2html.vbproj.user
Module OO2HTML

    Function Main(ByVal CmdArgs() As String) As Integer

        If CmdArgs.Length <> 2 Then
            Console.WriteLine("oo2html CMFolderPath WebServerPath")
            Console.WriteLine("")
            Console.WriteLine("oo2html ""C:\cmsynergy\dkeever\ccm_wa\pvsw01\psql;psql9.50_psql"" ""c:\inetpub\wwwroot\docs""")
            Return 0
        End If


        Dim rootdir As String = CmdArgs(0)
        Dim outdir As String = CmdArgs(1)


        'create an index
        Dim indexName As String = outdir & "\" & "index.html"
        Dim sw As System.IO.StreamWriter

        If Not System.IO.File.Exists(indexName) Then
            sw = System.IO.File.CreateText(indexName)
        Else : sw = New System.IO.StreamWriter(indexName)
        End If
        sw.WriteLine("<HTML>")
        sw.WriteLine("<body>")
        findDocsFolderRec(rootdir, outdir, sw, rootdir)
        sw.WriteLine("</body>")
        sw.WriteLine("</HTML>")
        sw.Close()

        Console.WriteLine(System.DateTime.Now.ToString & " Docs2Web completed" & vbCrLf)
        Return 0

    End Function

    'recursively go through the folder and find the docs folders with .sxw and .sxc files
    Private Sub findDocsFolderRec(ByVal dir As String, ByVal outdir As String, ByRef sw As System.IO.StreamWriter, ByRef rootdir As String)
        Dim dirInfo As New System.IO.DirectoryInfo(dir)

        'only do conversion for docs folders
        For Each di As System.IO.DirectoryInfo In dirInfo.GetDirectories()
            Dim componentName As String = di.Parent.Name
            If Not di.Parent.Parent Is Nothing Then
                If Not di.Parent.Parent.Name.IndexOf(":") = -1 Then
                    componentName = ""
                Else
                    componentName = di.Parent.Name
                End If
            End If

            ' get all OOo documents
            For Each fi As System.IO.FileInfo In di.GetFiles("*.sxw")
                saveOOtoHtml(fi.FullName, outdir, sw, rootdir)
            Next

            ' get all OOo spreadsheets
            For Each fi As System.IO.FileInfo In di.GetFiles("*.sxc")
                saveOOtoHtml(fi.FullName, outdir, sw, rootdir)
            Next
        Next

        'recurse
        For Each di As System.IO.DirectoryInfo In dirInfo.GetDirectories()
            findDocsFolderRec(di.FullName, outdir, sw, rootdir)
        Next

    End Sub


    Public Sub saveOOtoHtml(ByVal fromFile As String, ByVal outdir As String, ByRef sw As System.IO.StreamWriter, ByVal rootdir As String)
        Dim fs As New System.IO.FileInfo(fromFile)
        Dim Desktop As Object
        Dim Document As Object
        Dim OpenParams(1) As Object
        Dim SaveParams(1) As Object


        ' output is the physical, stored is that which is stored in the DB
        Dim outputPath As String = outdir
        Dim storedPath As String = ""
        If Not outputPath.EndsWith("\") Then
            outputPath &= "\"
        End If


        Dim toHtmlFile As String = outputPath & fs.Name.Replace(fs.Extension, ".html")
        Dim toPdfFile As String = outputPath & fs.Name.Replace(fs.Extension, ".pdf")
        Dim storedHtmlFile As String = storedPath & fs.Name.Replace(fs.Extension, ".html")
        Dim storedPdfFile As String = storedPath & fs.Name.Replace(fs.Extension, ".pdf")

        'Creating service manager
        Dim objServiceManager As Object
        Try
            objServiceManager = CreateObject("com.sun.star.ServiceManager")
        Catch ex As Exception
            Throw New Exception("OpenOffice is not installed" & vbCrLf & ex.ToString())
        End Try

        'Set the parameters for opening the file
        OpenParams(0) = OOoPropertyValue(objServiceManager, "Hidden", True)
        OpenParams(1) = OOoPropertyValue(objServiceManager, "ReadOnly", True)

        ' See http://api.openoffice.org/docs/common/ref/com/sun/star/document/MediaDescriptor.html

        'Creating a Desktop to open files
        Desktop = objServiceManager.createInstance("com.sun.star.frame.Desktop")
        ' See http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XComponentLoader.html

        'Open the file
        Document = Desktop.LoadComponentFromURL(ConvertToURL(fromFile), "_blank", 0, OpenParams)

        SaveParams(0) = OOoPropertyValue(objServiceManager, "Overwrite", True)

        'Store the file as html & pdf
        ' see http://framework.openoffice.org/files/documents/25/897/filter_description.html for other filters
        ' see http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XStorable.html#storeToURL
        If fs.Extension.Equals(".sxc") Then
            SaveParams(1) = OOoPropertyValue(objServiceManager, "FilterName", "HTML (StarCalc)")
            Document.storeToURL(ConvertToURL(toHtmlFile), SaveParams)
            'pdf export
            SaveParams(1) = OOoPropertyValue(objServiceManager, "FilterName", "calc_pdf_Export")
            Document.storeToURL(ConvertToURL(toPdfFile), SaveParams)
        ElseIf fs.Extension.Equals(".sxw") Then
            SaveParams(1) = OOoPropertyValue(objServiceManager, "FilterName", "HTML (StarWriter)")
            Document.storeToURL(ConvertToURL(toHtmlFile), SaveParams)
            ' pdf export
            SaveParams(1) = OOoPropertyValue(objServiceManager, "FilterName", "writer_pdf_Export")
            Document.storeToURL(ConvertToURL(toPdfFile), SaveParams)
        End If

        'Close Calc
        Document.Close(True)

        ' add the links to default.html
        sw.WriteLine("<a href=" & storedHtmlFile & ">" & fs.Name & "</a> " & "<a href=" & storedPdfFile & ">pdf</a><br>")

        Console.WriteLine(fs.FullName & " transformed successfully")
    End Sub

    Public Function OOoPropertyValue(ByRef objServiceMgr As Object, ByVal cName As Object, ByVal uValue As Object) As Object
        Dim oPropertyValue As Object
        ' not sure where I found this, but it does the trick!
        oPropertyValue = objServiceMgr.Bridge_GetStruct("com.sun.star.beans.PropertyValue")
        oPropertyValue.Name = cName
        oPropertyValue.Value = uValue
        Return oPropertyValue
    End Function

    Public Function ConvertToURL(ByVal path As String) As String
        'System.Diagnostics.Debug.WriteLine("file:///" & path.Replace("\", "/"))
        Return "file:///" & path.Replace("\", "/")
    End Function
End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

duwke
Web Developer
United States United States
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 28 Aug 2005
Article Copyright 2005 by duwke
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid