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. &quot;/tmp/hello_world.ods&quot;
    private String source_File = &quot;/home/tobias/test.odt&quot;;
    // The file that is the target of a conversion, e.g. &quot;/tmp/hello_world&quot;
    private String target_File = &quot;/tmp/test.pdf&quot;;
    // Format of the target_File. This snipped allows &quot;pdf&quot;, &quot;doc&quot;, &quot;xls&quot;
    private String conversion_Format = &quot;pdf&quot;;

    // Commandline parameter can equal these value
    public final static String CONVERT = &quot;-convert&quot;;
    
    public final static String DOC_EXPORT = &quot;MS Word 97&quot;;
    public final static String XLS_EXPORT = &quot;MS Excel 97&quot;;
    
    // 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 = &quot;Overwrite&quot;;
        documentProperties[0].Value = new Boolean(true);
        
        documentProperties[1] = new PropertyValue();
        documentProperties[1].Name = &quot;FilterName&quot;;
        
        // Conversion starts
        if (conversion_Format.equals(&quot;pdf&quot;)) {
            String applicationName = getApplicationName(openDocument);
            
            // Select the right filter 
            String myPDFExportFilter = null;
            if (applicationName.equals(&quot;com.sun.star.text.TextDocument&quot;))
                myPDFExportFilter = &quot;writer_pdf_Export&quot;;
            else if (applicationName.equals(&quot;com.sun.star.sheet.SpreadsheetDocument&quot;))
                myPDFExportFilter = &quot;calc_pdf_Export&quot;;
            else if (applicationName.equals(&quot;com.sun.star.presentation.PresentationDocument&quot;))
                myPDFExportFilter = &quot;impress_pdf_Export&quot;;
            else if (applicationName.equals(&quot;com.sun.star.drawing.DrawingDocument&quot;))
                myPDFExportFilter = &quot;draw_pdf_Export&quot;;
            else
                System.out.println(&quot;Unknown PDF export filter. Will crash soon.&quot;);
                
            // 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(&quot;Can&apos;t convert document!&quot;);
                System.exit(-5);
            }
        }
        else if (conversion_Format.equals(&quot;doc&quot;)) {
            // 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(&quot;Can&apos;t convert document. Got Error: &quot; + e);
                System.exit(-5);
            }
        }
        else if (conversion_Format.equals(&quot;xls&quot;)) {
            // 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(&quot;Can&apos;t convert document. Got error: &quot; + e);
                System.exit(-5);
            }
        }
        else{
            // Convert to unknown format
            System.err.println(&quot;Cannot convert to this format: &quot;
                    + conversion_Format);
            System.exit(-5);
        }
        // Close program
        System.exit(0);
    }

    /**
     * Get the application name of a document. E.g. for a writer document
     * &quot;com.sun.star.text.TextDocument&quot;
     * 
     * @param openDocument
     * @return
     */
    private String getApplicationName(XComponent openDocument)
    {
        XModuleManager xMM = null;
        try
        {
            xMM = (XModuleManager)UnoRuntime.queryInterface(
                    XModuleManager.class,
                    xRemoteServiceManager.createInstanceWithContext(
                            &quot;com.sun.star.frame.ModuleManager&quot;,
                            xRemoteContext));
        }
        catch (Exception e)
        {
            System.out.println(&quot;Cannot get Module Manager of started &quot; +
                    &quot;OpenOffice: &quot; + e.getLocalizedMessage());
        }
        String sOOoApp = null;
        try{
            // Getting the application name of the document,
            // e.g. &quot;com.sun.star.text.TextDocument&quot; for writer
            sOOoApp = xMM.identify(openDocument);
        }
        catch(Exception e)
        {
            System.out.println(&quot;Got the Module Manager but cannot determine &quot; +
                    &quot;the application name: &quot; + 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]

Reply via email to