Conversion Using OpenOffice

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
vinod.er2005
Posts: 2
Joined: Wed Nov 05, 2008 11:58 am

Conversion Using OpenOffice

Post by vinod.er2005 »

Hi All,

I am using openoffice SDK. I am going through the learning curve.
My query is:-

Can i convert any format to TIFF using OpenOffice SDK?
If yes, can anyone provide a little code snippet.
If no, then what approach should i use.

Thanks and Regrads,
Vinod Kumar
OOo 2.4.X on Ms Windows XP
User avatar
bobban
Volunteer
Posts: 535
Joined: Sat Nov 01, 2008 3:12 pm
Location: Australia

Re: Conversion Using OpenOffice

Post by bobban »

Hi Vinod

Start with this macro if you want. It exports each page of an Impress document to both an .sxi file and a .jpg file. The image export bit is at the bottom, and there are other image formats you can easily change to. TIFF is not currently one of them but you can just change the output file extension. The problem is how to define the TIFF export options. I don't know where these image filter are documented, but would like to find out. The other problem is that this works for Impress documents, but not for other types afaik. If you can get it working for them that would be great.

Code: Select all

Sub Main
SplitSlides()
End Sub

Sub SplitSlides()
' This is the name of the Impress document to split
' into separate documents.
' Each page of this Impress document is saved as a
' separate document, as a PDF, and also as a Flash.
cImpressDocToSplit = "C:\Documents and Settings\UserName\Desktop\testGE.odp"


' Open the document to find out how many pages it has.
oDoc = OpenDocument( cImpressDocToSplit )

nNumPages = oDoc.getDrawPages().getCount()

' Now that we know how many pages it has, close it.
oDoc.dispose()

' Get the name of the document, but without a filename suffix.
cImpressDocToSplitNoSuffix = Left( cImpressDocToSplit, Len( cImpressDocToSplit ) - 4 )


' Now loop once for each page.
nHighestPageNumber = nNumPages-1
' nPageToSave = 2
For nPageToSave = 0 To nHighestPageNumber

' Open the document.
oDoc = OpenDocument( cImpressDocToSplit )

' Delete all pages except the one we're interested in keeping
' on this loop.
DeleteAllPagesExcept( oDoc, nPageToSave )

' Prepare to save the document in multiple forms.
' First get the new filename to save it under.
cNewName = cImpressDocToSplitNoSuffix + " -- page " + CSTR( nPageToSave + 1 )

' Save the document as a new impress document.
oDoc.storeToURL( ConvertToURL( cNewName + ".sxi" ), Array() )

' Save it as a JPG
ExportToJPG( oDoc, cNewName )

' Close the document without saving it.
oDoc.dispose()
Next

End Sub


' Pass in the pathname to an Impress document.
' This opens the document, and returns it.
Function OpenDocument( cImpressDoc )

' Open the Impress document.
Dim args(0) As New com.sun.star.beans.PropertyValue
'Set the property value to be Hidden=TRUE
args(0).Name = "Hidden"
args(0).Value = TRUE
oDoc = StarDesktop.LoadComponentFromURL( ConvertToURL( cImpressDoc ), "_blank", 0, args() )

' Return the document
OpenDocument() = oDoc
End Function


' Delete all pages of an Impress or Draw document,
' EXCEPT for a certian page that we want to keep.
Function DeleteAllPagesExcept( oDoc, nPageToKeep )
nNumPages = oDoc.getDrawPages().getCount()
nHighestPageNumber = nNumPages-1

' Delete the last page, then the page before that,
' then the page before that, until we get to the
' page to keep.
' This deletes all pages AFTER the page to keep.
nPageToDelete = nHighestPageNumber
Do while nPageToDelete > nPageToKeep
' Get the page.
oPage = oDoc.getDrawPages().getByIndex( nPageToDelete )
' Tell the document to remove it.
oDoc.getDrawPages().remove( oPage )

nPageToDelete = nPageToDelete - 1
Loop

' Delete all the pages before the page to keep.
For i = 0 To nPageToKeep - 1
' Delete the first page.
nPageToDelete = 0
' Get the page.
oPage = oDoc.getDrawPages().getByIndex( nPageToDelete )
' Tell the document to remove it.
oDoc.getDrawPages().remove( oPage )
Next
End Function


Function MakePropertyValue( Optional cName As String, Optional uValue ) As com.sun.star.beans.PropertyValue
Dim oPropertyValue As New com.sun.star.beans.PropertyValue
If Not IsMissing( cName ) Then
oPropertyValue.Name = cName
EndIf
If Not IsMissing( uValue ) Then
oPropertyValue.Value = uValue
EndIf
MakePropertyValue() = oPropertyValue
End Function



Sub ExportToPDF( oDoc, cFilename )
cUrl = ConvertToURL( cFilename + ".pdf" )

oArgs = Array(_
MakePropertyValue( "URL", cURL ),_
MakePropertyValue( "FilterName", "impress_pdf_Export" ),_
)

oController = oDoc.getCurrentController()
oFrame = oController.getFrame()

oDispatcher = createUnoService( "com.sun.star.frame.DispatchHelper" )
oDispatcher.executeDispatch( oFrame, ".uno:ExportTo", "", 0, oArgs() )
End Sub



Sub ExportToSWF( oDoc, cFilename )
cUrl = ConvertToURL( cFilename + ".swf" )

oArgs = Array(_
MakePropertyValue( "URL", cURL ),_
MakePropertyValue( "FilterName", "impress_flash_Export" ),_
)

oController = oDoc.getCurrentController()
oFrame = oController.getFrame()

oDispatcher = createUnoService( "com.sun.star.frame.DispatchHelper" )
oDispatcher.executeDispatch( oFrame, ".uno:ExportTo", "", 0, oArgs() )
End Sub



Sub ExportToJPG( oDoc, cFilename )

Dim sFileUrl As String

sFileUrl = ConvertToURL( cFilename + ".jpg" )
oDrawPage = oDoc.getDrawPages().getByIndex(0)

'creating filter data
Dim aFilterData (7) as new com.sun.star.beans.PropertyValue
'properties valid for all filters
aFilterData(0).Name  = "PixelWidth"        '
aFilterData(0).Value = oDrawPage.Width*(72/2540)	'convert => mm => inches => pixels (72 points per inch)
aFilterData(1).Name  = "PixelHeight"
aFilterData(1).Value = oDrawPage.Height*(72/2540)	'convert => mm => inches => pixels (72 points per inch)

'filter data for the image/jpeg MediaType
aFilterData(2).Name  ="Quality"
aFilterData(2).Value = 90
aFilterData(3).Name  ="ColorMode"
aFilterData(3).Value = 0

'filter data for the image/png MediaType
'aFilterData(2).Name  ="Compression"
'aFilterData(2).Value = 9
'aFilterData(3).Name  ="Interlaced"
'aFilterData(3).Value = 0

'filter data for the image/gif MediaType
'aFilterData(2).Name  ="Translucent"
'aFilterData(2).Value = true
'aFilterData(3).Name  ="Interlaced"
'aFilterData(3).Value = 0

'filter data for the image/bmp MediaType
'aFilterData(2).Name  ="Color"
'aFilterData(2).Value = 7
'aFilterData(3).Name  ="ExportMode"
'aFilterData(3).Value = 0
'aFilterData(4).Name  ="Resolution"
'aFilterData(4).Value = 300
'aFilterData(5).Name  ="RLE_Coding"
'aFilterData(5).Value = true
'aFilterData(6).Name  ="LogicalWidth"
'aFilterData(6).Value = 2000
'aFilterData(7).Name  ="LogicalHeight"
'aFilterData(7).Value = 2000

Dim aArgs (2) as new com.sun.star.beans.PropertyValue

aArgs(0).Name  = "MediaType"
aArgs(0).Value = "image/jpeg"
aArgs(1).Name  = "URL"
aArgs(1).Value = sFileUrl
aArgs(2).Name  = "FilterData"
aArgs(2).Value = aFilterData()

xExporter = createUnoService( "com.sun.star.drawing.GraphicExportFilter" )
xExporter.setSourceDocument( oDrawPage )

xExporter.filter( aArgs() )

End Sub
OOo 3.1.1 on Ms Windows XP
Post Reply