Hi Tom, > http://codesnippets.services.openoffice.org/Office/Office.ConvertDocuments.snip > > Maybe you can rethink and brake the snippet into small logical parts and > link them together like i described it in my reply to "Java Snippet > Suggestion: how to convert documents - please review"
now I did so. It is still an application, but split. This is the last for today. Thank you for integrating the others so fast! Greetings, Tobias
<?xml version="1.0"?> <!-- $RCSfile: $ last change: $Revision: $ $Author: $ $Date: $ (c)2003 by the copyright holders listed with the author-tags. If no explicit copyright holder is mentioned with a certain author, the author him-/herself is the copyright holder. All rights reserved. Public Documentation License Notice: The contents of this Documentation are subject to the Public Documentation License Version 1.0 (the "License"); you may only use this Documentation if you comply with the terms of this License. A copy of the License is available at http://www.openoffice.org/licenses/PDL.html The Original Documentation can be found in the CVS archives of openoffice.org at the place specified by RCSfile: in this header. The Initial Writer(s) of the Original Documentation are listed with the author-tags below. The Contributor(s) are listed with the author-tags below without the marker for being an initial author. All Rights Reserved. --> <snippet language="Java" application="Office"> <keywords> <keyword>convert</keyword> <keyword>conversion</keyword> <keyword>pdf</keyword> </keywords> <authors> <author id="tobiaskrais" initial="false" email="[EMAIL PROTECTED]" copyright="Feel free to use and distribute it">Tobias Krais</author> </authors> <question heading="Convert Documents">How to convert documents? </question> <answer> <p>This small application starts an OpenOffice, loads a file and converts it.</p> <p>If you are only interested in the conversion part, see method</p> <p>private void convertDocument().</p> <listing>package de.twc.oocom.snippets; import java.net.MalformedURLException; import com.sun.star.beans.PropertyValue; import com.sun.star.comp.helper.Bootstrap; import com.sun.star.comp.helper.BootstrapException; import com.sun.star.frame.XComponentLoader; import com.sun.star.frame.XModuleManager; 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; import com.sun.star.uri.ExternalUriReferenceTranslator; public class ConvertDocument { public ConvertDocument(){ // Empty Constructor for OOComNG } // The file that should be processed, e.g. "/tmp/hello_world.ods" private String source_File = "/home/tobias/test.odt"; // The file that is the target of a conversion, e.g. "/tmp/hello_world" private String target_File = "/tmp/test.pdf"; // Format of the target_File. This snipped allows "pdf", "doc", "xls" private String conversion_Format = "pdf"; // Commandline parameter can equal these value public final static String CONVERT = "-convert"; public final static String DOC_EXPORT = "MS Word 97"; public final static String XLS_EXPORT = "MS Excel 97"; // OpenOffice Desktop Object desktop = null; XComponentContext xRemoteContext = null; XMultiComponentFactory xRemoteServiceManager = null; /** * Main method. For commandline start values see method * setCommandlineArgs(args). * * @param args */ public static void main(String[] args){ ConvertDocument myOOComNG = new ConvertDocument(); myOOComNG.convertDocument(); } /** * This method converts documents to other formats. */ private void convertDocument() { // Getting a running OpenOffice see: {%internal ../Office/Office.BootstrapOpenOffice.snip} desktop = bootstrapOpenOffice(); // Open document see: {%internal ../Office/Office.OpenDocumentFromURL.snip} XComponent openDocument = openDocument(source_file); XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, openDocument); // Set properties for conversions documentProperties = new PropertyValue[2]; documentProperties[0] = new PropertyValue(); documentProperties[0].Name = "Overwrite"; documentProperties[0].Value = new Boolean(true); documentProperties[1] = new PropertyValue(); documentProperties[1].Name = "FilterName"; // Conversion starts if (conversion_Format.equals("pdf")) { String applicationName = getApplicationName(openDocument); // Select the right filter String myPDFExportFilter = null; if (applicationName.equals("com.sun.star.text.TextDocument")) myPDFExportFilter = "writer_pdf_Export"; else if (applicationName.equals("com.sun.star.sheet.SpreadsheetDocument")) myPDFExportFilter = "calc_pdf_Export"; else if (applicationName.equals("com.sun.star.presentation.PresentationDocument")) myPDFExportFilter = "impress_pdf_Export"; else if (applicationName.equals("com.sun.star.drawing.DrawingDocument")) myPDFExportFilter = "draw_pdf_Export"; else System.out.println("Unknown PDF export filter. Will crash soon."); // Convert to pdf try { documentProperties[1].Value = myPDFExportFilter; // For createUNOFileURL refer to: {%internal ../Office/Office.CreateUNOCompatibleURL.snip} xStorable.storeToURL(createUNOFileURL(target_File), documentProperties); } catch (IOException e) { System.err.println("Can't convert document!"); System.exit(-5); } } else if (conversion_Format.equals("doc")) { // Convert to MS Word try { documentProperties[1].Value = DOC_EXPORT; // For createUNOFileURL refer to: {%internal ../Office/Office.CreateUNOCompatibleURL.snip} xStorable.storeToURL(createUNOFileURL(target_File), documentProperties); } catch (IOException e) { System.err.println("Can't convert document. Got Error: " + e); System.exit(-5); } } else if (conversion_Format.equals("xls")) { // Convert to MS Excel try { documentProperties[1].Value = XLS_EXPORT; // For createUNOFileURL refer to: {%internal ../Office/Office.CreateUNOCompatibleURL.snip} xStorable.storeToURL(createUNOFileURL(target_File), documentProperties); } catch (IOException e) { System.err.println("Can't convert document. Got error: " + e); System.exit(-5); } } else{ // Convert to unknown format System.err.println("Cannot convert to this format: " + conversion_Format); System.exit(-5); } // Close program System.exit(0); } /** * Get the application name of a document. E.g. for a writer document * "com.sun.star.text.TextDocument" * * @param openDocument * @return */ private String getApplicationName(XComponent openDocument) { XModuleManager xMM = null; try { xMM = (XModuleManager)UnoRuntime.queryInterface( XModuleManager.class, xRemoteServiceManager.createInstanceWithContext( "com.sun.star.frame.ModuleManager", xRemoteContext)); } catch (Exception e) { System.out.println("Cannot get Module Manager of started " + "OpenOffice: " + e.getLocalizedMessage()); } String sOOoApp = null; try{ // Getting the application name of the document, // e.g. "com.sun.star.text.TextDocument" for writer sOOoApp = xMM.identify(openDocument); } catch(Exception e) { System.out.println("Got the Module Manager but cannot determine " + "the application name: " + e.getLocalizedMessage()); } return sOOoApp; } }</listing> </answer> <versions> <version number="2.0.x" status="tested"/> <version number="1.1.x" status="untested"/> </versions> <operating-systems> <operating-system name="All"/> </operating-systems> <changelog> <change author-id="tobiaskrais" date="2006-03-27">Other snippets are linked in and thus the code is shortened.</change> </changelog> </snippet>
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
