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

How convert Powerpoint slides to jpeg using openoffice api?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Viggo
Guest





PostPosted: Sun May 09, 2004 11:50 pm    Post subject: How convert Powerpoint slides to jpeg using openoffice api? Reply with quote

Problem:
I need to convert powerpoint slides to jpeg (or pdf) using the open office api. I need to do this in the background, openoffice must not open.
I have seen code snippets for this, but not an entire code sample that I can use, and since I'm new to openoffice and it's api's, I would really need some help!

I have tried to create a sample, but it's not complete:

import java.io.File;

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.io.IOException;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class PowerpointConverter {

private XMultiComponentFactory mxRemoteServiceManager = null;
private XComponentContext mxRemoteContext = null;
private String unoUrl =
"uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";

public void convert(String url) {
try {
System.out.println("URL: " + url);
XComponent xDoc = loadDocument(url);
storeDocument(xDoc,url);
} catch( IOException ioe) {
ioe.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

private XComponent loadDocument(String loadUrl) throws Exception {

// get the remote service manager
mxRemoteServiceManager = this.getRemoteServiceManager(loadUrl);
// retrieve the Desktop object, we need its XComponentLoader
Object desktop = mxRemoteServiceManager.createInstanceWithContext(
"com.sun.star.frame.Desktop", mxRemoteContext);
XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface(
XComponentLoader.class, desktop);

// Composing the URL by replacing all backslashs
File file = new File(loadUrl);
String stringUrl = "file://"
+ file.getAbsolutePath().replace( '\\', '/' );

// Loading the wanted document
XComponent xDoc =
DocumentConverter.xcomponentloader.loadComponentFromURL(
stringUrl, "_blank", 0, new PropertyValue[0] );

return xDoc;
}

private void storeDocument(XComponent xDoc, String storeUrl) throws IOException {

XStorable xStorable = (XStorable)UnoRuntime.queryInterface(XStorable.class, xDoc);
PropertyValue[] storeProps = new PropertyValue[1];
storeProps[0] = new PropertyValue();
storeProps[0].Name = "FilterName";
storeProps[0].Value = "jpg_Export";

xStorable.storeAsURL(storeUrl, storeProps);
}

private XMultiComponentFactory getRemoteServiceManager(String unoUrl) throws java.lang.Exception {
if (mxRemoteContext == null) {
// First step: create local component context, get local servicemanager and
// ask it to create a UnoUrlResolver object with an XUnoUrlResolver interface
XComponentContext xLocalContext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext(null);

XMultiComponentFactory xLocalServiceManager = xLocalContext.getServiceManager();

Object urlResolver = xLocalServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xLocalContext );
// query XUnoUrlResolver interface from urlResolver object
XUnoUrlResolver xUnoUrlResolver = (XUnoUrlResolver) UnoRuntime.queryInterface(
XUnoUrlResolver.class, urlResolver );

// Second step: use xUrlResolver interface to import the remote StarOffice.ServiceManager,
// retrieve its property DefaultContext and get the remote servicemanager
Object initialObject = xUnoUrlResolver.resolve( unoUrl );
XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
XPropertySet.class, initialObject);
Object context = xPropertySet.getPropertyValue("DefaultContext");
mxRemoteContext = (XComponentContext)UnoRuntime.queryInterface(
XComponentContext.class, context);
}
return mxRemoteContext.getServiceManager();
}

public static void main(String[] args) {

if(1 != args.length) {
System.out.println("Usage: PowerpointConverter <path to file>");
System.out.println("Example: PowerpointConverter c:\\data\\projects\\openoffice\\convertsource\\test.ppt");
} else {
PowerpointConverter converter = new PowerpointConverter();
converter.convert(args[0]);
}
}
}


Best Regards
Viggo
Code:
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Mon May 10, 2004 7:21 am    Post subject: Reply with quote

Here is a list of many past document conversion examples...
http://www.oooforum.org/forum/viewtopic.php?t=4998
The technique primarily consists of...
1. Open a document from format X (possibly requiring import filter X)
2. Save document in format Y (probably requiring export filter Y)
3. Close document.

In step 1, of course, you can open the document hidden so that it does not appear on screen. If you do this, then be sure to close the document (step 3).

Here is a program ready made, in Basic, that takes a presentation and splits it into sepeerate files. I call it Slide Splitter, and you can get it at OOoMacros.org.

Code:

Sub Main
   ' 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 = "/home/danny/Desktop/SlideSplitter/SlideDocumentToSplit.sxi"
   cImpressDocToSplit = "C:\Documents and Settings\dbrewer\Desktop\SlideSplitter\SlideDocumentToSplit.sxi"

   SplitSlides( cImpressDocToSplit )
End Sub

Sub SplitSlides( cImpressDocToSplit )
   
   ' Open the document to find out how many pages it has.
   oDoc = StarDesktop.LoadComponentFromURL( ConvertToURL( cImpressDocToSplit ), "_blank", 0, Array() )
   
   nNumPages = oDoc.getDrawPages().getCount()
   
   ' Now that we know how many pages it has, close it.
   oDoc.close( True )
   
   ' 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 = StarDesktop.LoadComponentFromURL( ConvertToURL( cImpressDocToSplit ), "_blank", 0, Array() )
      
      ' 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 PDF.
      oDoc.storeToUrl( ConvertToURL( cNewName + ".pdf" ), _
         Array( MakePropertyValue( "FilterName", "impress_pdf_Export" ) ) )

      ' Save it as a Flash.
      oDoc.storeToUrl( ConvertToURL( cNewName + ".swf" ), _
         Array( MakePropertyValue( "FilterName", "impress_flash_Export" ) ) )

      ' Save it as a JPEG.
      oDoc.storeToUrl( ConvertToURL( cNewName + ".jpeg" ), _
         Array( MakePropertyValue( "FilterName", "impress_jpg_Export" ) ) )
      
      ' Close the document without saving it.
      oDoc.close( True )
   Next
   
End Sub



' 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
   oPropertyValue = createUnoStruct( "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



The above program takes an impress document (or powerpoint) and splits it into multiple presentation documents, one slide per document. In addition, each separated slide is exported into the following formats....
PDF
Flash (swf)
Jpeg

You can either use this code, if it is a one-time job. Or you can use the same techniques in a Java program.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
varungupta
General User
General User


Joined: 18 Aug 2004
Posts: 7

PostPosted: Wed Aug 18, 2004 12:41 pm    Post subject: Re: How convert Powerpoint slides to jpeg using openoffice a Reply with quote

Viggo wrote:
Problem:
I need to convert powerpoint slides to jpeg (or pdf) using the open office api. I need to do this in the background, openoffice must not open.
I have seen code snippets for this, but not an entire code sample that I can use, and since I'm new to openoffice and it's api's, I would really need some help!

I have tried to create a sample, but it's not complete:

import java.io.File;

import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.bridge.XUnoUrlResolver;

Best Regards
Viggo
Code:


Hi Viggo,

Can u tell me where have u seen some code snippets for converting Presentation slides to JPEG ?

I also want to write a Java Program which can convert PPT slides into JPEG in the background (without opening OpenOffice) ??

Moreover i dont want to use Macros

Thanx in advance

Varun
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Aug 19, 2004 7:22 am    Post subject: Reply with quote

The code earlier in this thread converts Impress to JPEG. On the loadComponentFromURL, just load a PPT instead of an Impress (SXI). Because TypeDetection mechanism can auto recognize PPT and open it in Impress, you don't even need to pass an import filter property to loadComponentFromURL. Just give loadComponentFromURL the pathname of a PPT file instead of an SXI file.

If you want the files opened in the background, then be sure to do two things...
1. Be sure to pass the Hideen property with a value of True to loadComponentFromURL.
2. Be SURE to close the document when done with it. Since the document was opened invisible (Hidden=True), the user cannot see it on the screen, and therefore cannot close it. So your code must close it.

The code can be written in Java. The API is the same no matter what programming language. In Java, before every single method call, you will have to do an UnoRuntime.queryInterface to obtain the correct interface which has the method you are about to call.

See this article....
Impress: Export slides into separate documents
http://www.oooforum.org/forum/viewtopic.php?t=7796
This article is similar to the above. If you do rewrite the example in Java, please consider publishing it publicly. Perhaps by appending it to the article I just linked to.



Other misc. related articles...
http://www.oooforum.org/forum/viewtopic.php?p=32470#32470
http://www.oooforum.org/forum/viewtopic.php?p=23018#23018
http://www.oooforum.org/forum/viewtopic.php?t=2664
http://www.oooforum.org/forum/viewtopic.php?p=9819#9819
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
prakash
General User
General User


Joined: 27 May 2004
Posts: 32

PostPosted: Thu Aug 19, 2004 8:52 pm    Post subject: Reply with quote

Same thing can be done in three steps :
1 get XComponentLoader
2 load document
3. use storeToURL or storeAsURL ( this will store PPT as jpeg and html as well)
use filter ( impress_html_Export )

But if you want convert a particular slide in to jpeg.. earlier approach is better

Can we use XComponentLoader object to get ServiceFactory ( using some query interface) ... which can be used to create Graphic filter.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Fri Aug 20, 2004 5:26 am    Post subject: Reply with quote

A Service may support multiple Interfaces. Once you have any interface, you can query to any other interface on the same service.

Think of the Service as an "object". Think of the Interfaces to be similar to what Interfaces are in Java. The UnoRuntime.queryInterface should be thought of as a type cast from one interface to another interface. Do not replace queryInteface with an ordinary typecast. UnoRuntime.queryInterface might return a different underlying concrete object. After all a "Service" may actually be a small group of related objects. But think of a Service abstractly as one object with multiple interfaces.

ServiceFoo has interfaces XApple, XOrange, and XBannana.

XOrange xOrange = (XOrange) ....something to get ServiceFoo....
XApple xApple = (XApple) UnoRuntime.queryInterface( XApple.class, xOrange );

The first line got the ServiceFoo object (actually service not object). It got the XOrange interface to the Service. The second line does a typecast to obtain the XApple interface to the ServiceFoo service.

In Basic or Python, this whole queryInterface business is unnecessary.

oServiceFoo = ....something to get ServiceFoo....

I would not need seperate variables to hold typed interfaces. I could call any method of any interface directly from the oServiceFoo variable.

oServiceFoo.someAppleMethod( ... )
oServiceFoo.someOrangeMethod( ... )
oServiceFoo.someBannanaMethod( .... )

no need to queryInterface. The Basic or Python will figure out which interface needs to be obtained in order to call the method.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
varungupta
General User
General User


Joined: 18 Aug 2004
Posts: 7

PostPosted: Sat Aug 21, 2004 2:01 am    Post subject: Reply with quote

prakash wrote:
Same thing can be done in three steps :
1 get XComponentLoader
2 load document
3. use storeToURL or storeAsURL ( this will store PPT as jpeg and html as well)
use filter ( impress_html_Export )

But if you want convert a particular slide in to jpeg.. earlier approach is better

Can we use XComponentLoader object to get ServiceFactory ( using some query interface) ... which can be used to create Graphic filter.


Hi Prakash

I tried that but it gives this exception :

com.sun.star.io.IOException:
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at com.sun.star.lib.uno.protocols.urp.Unmarshal.readThrowable(Unmarshal.java:315)
at com.sun.star.lib.uno.protocols.urp.Unmarshal.readAny(Unmarshal.java:157)
at com.sun.star.lib.uno.protocols.urp.Unmarshal.readObject(Unmarshal.java:404)
at com.sun.star.lib.uno.protocols.urp.urp.readReply(urp.java:192)
at com.sun.star.lib.uno.protocols.urp.urp.readMessage(urp.java:308)
at com.sun.star.lib.uno.protocols.urp.urp.readMessage(urp.java:607)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge$MessageDispatcher.invoke(java_remote_bridge.java:184)
at com.sun.star.lib.uno.bridges.java_remote.java_remote_bridge$MessageDispatcher.run(java_remote_bridge.java:175)

See if u can help me get over this problem.
Moreover i tried the DocumentConverter Example in the SDK. I used it to convert doc to pdf by using the filter "writer_pdf_Export". But it also gives the same exception.

Help me !! Sad

thanx in advance

Varun
Back to top
View user's profile Send private message
varungupta
General User
General User


Joined: 18 Aug 2004
Posts: 7

PostPosted: Sat Aug 21, 2004 10:25 am    Post subject: Reply with quote

hi guys

Good news.
Got the application to convert from PPT to JPEG in Java Working.

The exception that i told before can be avoided if we use
storeToUrl instead of storeAsUrl

Bye

Varun
Back to top
View user's profile Send private message
prakash
General User
General User


Joined: 27 May 2004
Posts: 32

PostPosted: Thu Aug 26, 2004 1:55 am    Post subject: Reply with quote

i am unable to get slide title from the PPt document. I couldnt find any
information in Developers guide on this..
we can get a draw page by index... but what about getting draw page name
is there name property set somwhere to acees this info?
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Aug 26, 2004 9:16 am    Post subject: Reply with quote

prakash wrote:
i am unable to get slide title from the PPt document. I couldnt find any
information in Developers guide on this..
we can get a draw page by index... but what about getting draw page name
is there name property set somwhere to acees this info?


Once a PPT has been properly converted into Impress, you should be able to access the slid titles like this.....
http://www.oooforum.org/forum/viewtopic.php?p=46191#46191
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
varungupta
General User
General User


Joined: 18 Aug 2004
Posts: 7

PostPosted: Sun Aug 29, 2004 10:09 pm    Post subject: Increasing thw quality of the JPEG image Reply with quote

Hi,

I m able to convert a PPT into JPEG, but how to increase the quality of the converted image.

I have seen that when i do manually from OpenOffice, it asks to enter a number that signify the quality of the JPEG image.

How can i increase the quality of the JPEG image in Java while converting.

Thanx in advance

Varun
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Mon Aug 30, 2004 8:10 am    Post subject: Reply with quote

I don't have an answer. This thread might help.
http://www.oooforum.org/forum/viewtopic.php?t=11838
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
othmanelmoulat
General User
General User


Joined: 06 Mar 2005
Posts: 29

PostPosted: Sun Mar 06, 2005 2:27 pm    Post subject: Reply with quote

Hi guys,

I'm new to open office and i'm trying to write a java program that would convert PTT slides into jpeg images.

Is there any java code to do this without having open office installed ?(MS PPT).
I want to try the java code posted by OP but the com.sun.star stuff is not recognized : do i need first to install OO?

It would be nice if varungupta would have the courtesy to post the java code to share with the community.

please help !
thanks.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Sat Mar 12, 2005 4:18 pm    Post subject: Reply with quote

The code would require OpenOffice.org to be installed.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
b6s
Newbie
Newbie


Joined: 26 Nov 2005
Posts: 1

PostPosted: Sat Nov 26, 2005 11:48 pm    Post subject: Reply with quote

DannyB wrote:
The code would require OpenOffice.org to be installed.


Excuse me, is it possible to use URE instead of OO.o itself to run this macro?
Back to top
View user's profile Send private message
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
Goto page 1, 2  Next
Page 1 of 2

 
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