| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 
 |  
package fr.sewatech.sewatoool.pdf;
 
import java.io.File;
import java.net.MalformedURLException;
 
import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.io.IOException;
import com.sun.star.lang.IllegalArgumentException;
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 PdfConverter {
 
	public static void main(String[] args) {
		try {
			new PdfConverter().convert("D:\\Temp\\tmp.doc");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.exit(0);
		}
	}
 
	private XComponentContext xContext;
	private Object component;
	private Object desktop;
 
	/**
         * Convertit un fichier en PDF
         * 
         * @param filename
         * @throws Exception
         */
	public void convert(String filename) throws Exception {
 
		startOpenOffice();
		String unoUrl = loadDocument(filename);
		generatePdf(unoUrl);
	}
 
	private void generatePdf(String unoUrl) throws IOException {
		XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
				XStorable.class, component);
 
		// Set properties for conversions
		PropertyValue[] convertProperties = new PropertyValue[2];
 
		convertProperties[0] = new PropertyValue();
		convertProperties[0].Name = "Overwrite";
		convertProperties[0].Value = true;
 
		convertProperties[1] = new PropertyValue();
		convertProperties[1].Name = "FilterName";
		convertProperties[1].Value = "writer_pdf_Export";
 
		xStorable.storeToURL(unoUrl.substring(0, unoUrl.lastIndexOf('.')) + ".pdf", convertProperties);
	}
 
	/**
         * Chargement d'un fichier OpenOffice
         * 
         * @param filename
         * @return
         * @throws MalformedURLException
         * @throws IOException
         * @throws IllegalArgumentException
         */
	private String loadDocument(String filename)
			throws MalformedURLException, IOException, IllegalArgumentException {
		XComponentLoader loader = (XComponentLoader) UnoRuntime.queryInterface(
				XComponentLoader.class, desktop);
 
		String unoUrl = formatUnoUrl(filename);
 
		// Value=true => pas d'interface graphique
		PropertyValue[] loadProperties = new PropertyValue[1];
		loadProperties[0] = new PropertyValue();
		loadProperties[0].Name = "Hidden";
		loadProperties[0].Value = true;
 
		component = loader.loadComponentFromURL(unoUrl, "_blank", 0,
				loadProperties);
		return unoUrl;
	}
 
	/**
         * Démarrage d'OpenOffice
         * 
         * @return Instance d'OOo avec contexte
         * @throws Exception
         */
	private Object startOpenOffice() throws Exception {
		xContext = Bootstrap.bootstrap();
		XMultiComponentFactory xMCF = xContext.getServiceManager();
 
		desktop = xMCF.createInstanceWithContext(
				"com.sun.star.frame.Desktop", xContext);
		return desktop;
	}
 
	/**
         * Formatte un chemin traditionnel en chemin compatible UNO 
         * 
         * @param filename Chemin du fichier, au format traditionnel
         * @return Chemin du fichier, au format UNO
         * @throws MalformedURLException 
         */
	private String formatUnoUrl(String filename)
			throws MalformedURLException {
		String unoUrl = ExternalUriReferenceTranslator.create(xContext)
				.translateToInternal(new File(filename).toURL().toExternalForm());
		return unoUrl;
	}
 
} | 
Partager