OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Create PDF from writer document on command line

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
milstead
Newbie
Newbie


Joined: 05 Nov 2004
Posts: 1

PostPosted: Fri Nov 05, 2004 4:25 am    Post subject: Create PDF from writer document on command line Reply with quote

I am using MS windows.

I would like to automatically create a pdf document from a writer document using the command line from within a batch script. In other words I don't want to manually load the file and select pdf export instead I would like to have it done as some sort of scripted build process.

Is this possible? How can I do it? Does swriter accept command line options to a file and a macro to run once loaded (a macro that would export the pdf file and then close writer)?

Thanks for your help,

Tim.
Back to top
View user's profile Send private message
JacquesDuPreez
General User
General User


Joined: 05 Nov 2004
Posts: 8
Location: South Africa

PostPosted: Fri Nov 05, 2004 5:46 am    Post subject: Reply with quote

Check out this code sample which I found on the forum; might help you:

Code:

Imports System.IO

Module Module1

    Function JumpToBookmark(ByVal oDoc As Object, ByVal sBookmarkName As String) As Object
        ' Jump to the Bookmark and get the Cursor from the selection
        Dim oBookmark As Object
        oBookmark = oDoc.Bookmarks.getByName(sBookmarkName)
        Dim oBookmarkAnchor As Object = oBookmark.Anchor
        Dim oRangeCursor As Object = oDoc.Text.createTextCursorByRange(oBookmarkAnchor)
        JumpToBookmark = oRangeCursor
    End Function


    Function CreateStruct(ByRef objCoreReflection As Object, ByVal strTypeName As String)
        Dim classSize
        classSize = objCoreReflection.forName(strTypeName)
        Dim aStruct
        classSize.createObject(aStruct)
        CreateStruct = aStruct
    End Function

    Sub Main()


        Dim objDummy As Object = New Object   'used only to get a type

        'The service manager is always the starting point
        'If there is no office running then an office is started up
        Dim objSM As Object = CreateObject("com.sun.star.ServiceManager")

        'Create the CoreReflection service that is later used to create structs
        Dim objCR As Object = objSM.CreateInstance("com.sun.star.reflection.CoreReflection")

        'Create the Desktop
        Dim objDesktop As Object = objSM.CreateInstance("com.sun.star.frame.Desktop")

        'Here is the trick:
        'instead of doing Dim args() which is really doing Dim args() as Object,
        'you dim args as System.Array
        'The second part of the trick is to initialise the array with the type Object so that
        'it is marshaled as a safearray.
        Dim _args As System.Array
        _args = System.Array.CreateInstance(objDummy.GetType, 0)
        objDummy = Nothing ' free ressource

        'Open a new empty writer document
        Dim objDocument As Object = objDesktop.LoadComponentFromURL( _
        "private:factory/swriter", "_blank", 0, _args)

        'Create a text object
        Dim objText As Object
        objText = objDocument.GetText

        'Create a cursor object
        Dim objCursor As Object
        objCursor = objText.createTextCursor

        'Inserting some Text
        objText.insertString(objCursor, "Hello World!" & vbLf, False)

        'Inserting a second line
        For I As Integer = 1 To 1000
            objText.insertString(objCursor, "Now we're in a new line" & _
                I.ToString() & vbLf, False)
        Next I

        ' Save the document

        ' Build necessary argument list for store properties.
        ' Use flag "Overwrite" to prevent exceptions, if file already exists.
        Dim poOverwrite As Object = CreateStruct(objCR, "com.sun.star.beans.PropertyValue")
        poOverwrite.name = "Overwrite"
        poOverwrite.value = False

        Dim poFilterName As Object = CreateStruct(objCR, "com.sun.star.beans.PropertyValue")
        poFilterName.name = "FilterName"
        'poFilterName.value = "swriter: StarWriter 5.0"
        poFilterName.value = "writer_pdf_Export"

        Dim poCompress As Object = CreateStruct(objCR, "com.sun.star.beans.PropertyValue")
        poCompress.name = "CompressMode"
        poCompress.value = 1

        Dim fileProps As System.Array
        fileProps = System.Array.CreateInstance(poOverwrite.GetType, 3)
        fileProps(0) = poOverwrite
        fileProps(1) = poFilterName
        fileProps(2) = poCompress

        'Create the Desktop
        ' Dim xStore As Object = objDocument.CreateInstance("com.sun.star.frame.XStorable")
        REM Set this to True if you want to overwrite the document.
        ' com.sun.star.beans.PropertyValue()
        Dim pdfFName As String = "testoo.pdf"
        Dim urlFName As String = "file:///c|/temp/" + pdfFName
        Dim wFName As String = "c:\temp\" + pdfFName

        ' Make sure the file does not already exist and then create if
        If File.Exists(wFName) Then
            Try
                File.Delete(wFName)
            Catch e As Exception
                Console.WriteLine("Deletion failed : {0}", e.ToString())
                Return
            End Try
        End If

        objDocument.storeToURL(urlFName, fileProps)
        ' objDocument.saveToURL(urlFName, fileProps)

        ' Set the document flag to not modified, to avoid any warning on saving
        objDocument.setModified(False)

        ' Close the document
        ' Even if we dispose, apparently the document stays open?
        ' objDocument.dispose()


        ' Terminate the application
        objDesktop.terminate()

        ' objCursor.setPropertyValue("CharColor", 65536)

        ' Disconnect
        objSM = Nothing

    End Sub

End Module
 
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
JacquesDuPreez
General User
General User


Joined: 05 Nov 2004
Posts: 8
Location: South Africa

PostPosted: Fri Nov 05, 2004 5:47 am    Post subject: Reply with quote

Sorry, was in a bit of a hurry didn't see you actually want to use a batch file for this task ... Embarassed Embarassed Embarassed
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group