OO-Snippets: Convert Documents

Commons

Keywordsconvert, conversion, pdf
LanguageJava
ApplicationOffice
AuthorsTobias Krais (www.design-to-use.de) (initial)
Supported Versions2.0.x  1.1.x  
Supported OSAll  
QuestionHow to convert a document in an other format?
Answer

Code-Snippet-Listing (snippet-source)

import com.sun.star.lang.XComponent;
import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XStorable;
import com.sun.star.io.IOException;
import com.sun.star.uno.UnoRuntime;

/**
 * This method converts documents to other formats.
 * See http://www.openoffice.org/files/documents/25/3042/filter_list_ooo_2_0.html
 * 
 * @param targetFilename Name of the file, which is the target of the export.
 * @param conversionFilter Contains the conversion filter (see link above).
 */
public void convert(String targetFilename, String conversionFilter)
{
	// How to get the XComponent, see ../Office/Office.OpenDocumentFromURL.snip
	XStorable xStorable = (XStorable)
	UnoRuntime.queryInterface(XStorable.class, this.xComponent);

	// Set properties for conversions
	PropertyValue[] conversionProperties = new PropertyValue[2];

	conversionProperties[0] = new PropertyValue();
	conversionProperties[0].Name = "Overwrite";
	conversionProperties[0].Value = new Boolean(true);

	conversionProperties[1] = new PropertyValue();
	conversionProperties[1].Name = "FilterName";
	conversionProperties[1].Value = conversionFilter;

	// Convert
	try {
		// See ../Office/Office.CreateUNOCompatibleURL.snip for method createUNOFileURL(targetFilename);
		xStorable.storeToURL(createUNOFileURL(targetFilename),
				conversionProperties);
	} catch (IOException e) {
		e.printStackTrace();
	}
}

Changelog

DateUserModification
2006-12-11tobiaskraisLinked other methods from the snippet base and thus simplified the method

and