1 2 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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
package fr.iomeda.ooffice;
// JDK API
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.util.Enumeration;
import java.lang.Runtime;
// Servlet API
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletOutputStream;
// UNO API
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.view.XPrintable;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.util.XReplaceDescriptor;
import com.sun.star.util.XSearchDescriptor;
import com.sun.star.util.XReplaceable;
import com.sun.star.text.*;
public class PrintDocument {
/** Specifies the host for the office server.
*/
private String stringHost = "localhost";
/** Specifies the port for the office server.
*/
private String stringPort = "9200";
/** Cette méthode a pour unique but d'imprimer dans pdf le document.
* @param doc
* @return Le chemin du fichier de l'impression.
*/
private String printDocumentInPdf(String modeleFileName) {
// Converting the document to the favoured type
String psPathFile = "/tmp/print/out.ps";
String pdfPathFile = "/tmp/print/out.pdf";
try {
// Composing the URL
String stringUrl = "file://" + modelDir + "modele.sxw";
/* Bootstraps a component context with the jurt base components
registered. Component context to be granted to a component for running.
Arbitrary values can be retrieved from the context. */
XComponentContext xcomponentcontext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );
/* Gets the service manager instance to be used (or null). This method has
been added for convenience, because the service manager is a often used
object. */
XMultiComponentFactory xmulticomponentfactory =
xcomponentcontext.getServiceManager();
/* Creates an instance of the component UnoUrlResolver which
supports the services specified by the factory. */
Object objectUrlResolver =
xmulticomponentfactory.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xcomponentcontext );
// Create a new url resolver
XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class,
objectUrlResolver );
// Resolves an object that is specified as follow:
// uno:<connection description>;<protocol description>;<initial object name>
Object objectInitial = xurlresolver.resolve(
"uno:socket,host=" + stringHost + ",port=" + stringPort + ";urp;StarOffice.ServiceManager" );
// Create a service manager from the initial object
xmulticomponentfactory = ( XMultiComponentFactory )
UnoRuntime.queryInterface( XMultiComponentFactory.class, objectInitial );
// Query for the XPropertySet interface.
XPropertySet xpropertysetMultiComponentFactory = ( XPropertySet )
UnoRuntime.queryInterface( XPropertySet.class, xmulticomponentfactory );
// Get the default context from the office server.
Object objectDefaultContext =
xpropertysetMultiComponentFactory.getPropertyValue( "DefaultContext" );
// Query for the interface XComponentContext.
xcomponentcontext = ( XComponentContext ) UnoRuntime.queryInterface(
XComponentContext.class, objectDefaultContext );
/* A desktop environment contains tasks with one or more
frames in which components can be loaded. Desktop is the
environment for components which can instanciate within
frames. */
XComponentLoader xcomponentloader = ( XComponentLoader )
UnoRuntime.queryInterface( XComponentLoader.class,
xmulticomponentfactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", xcomponentcontext ) );
// Preparing properties for loading the document
PropertyValue propertyvalue[] = new PropertyValue[ 1 ];
// Setting the flag for hidding the open document
propertyvalue[ 0 ] = new PropertyValue();
propertyvalue[ 0 ].Name = "Hidden";
propertyvalue[ 0 ].Value = new Boolean(true);
// Loading the wanted document
Object objectDocumentToStore =
xcomponentloader.loadComponentFromURL(
stringUrl, "_blank", 0, propertyvalue );
if(objectDocumentToStore == null) {
System.out.println("objectDocumentToStore est null");
System.out.println("stringUrl:" + stringUrl);
}
// Querying for the interface XTextDocument on the xcomponent
XTextDocument xtextdocument = ( XTextDocument ) UnoRuntime.queryInterface(
XTextDocument.class, objectDocumentToStore );
// Getting the text
XText xtext = xtextdocument.getText();
// Getting the cursor on the document
XTextCursor xtextcursor = xtext.createTextCursor();
com.sun.star.util.XReplaceable xReplaceable;
xReplaceable = ( com.sun.star.util.XReplaceable ) UnoRuntime.queryInterface( com.sun.star.util.XReplaceable.class, xtextdocument );
// Variable dépendant du propriétaire du document.
replace(xReplaceable,"<u:prenom>","Jérôme");
replace(xReplaceable,"<u:nom>","Saint-Yves");
replace(xReplaceable,"<u:titre>","Mr");
int x = 0;
for(x = 0;x < User.NB_SUBTITLE;x++) {
replace(xReplaceable,"<u:sous-titre" + String.valueOf(x+1) + ">",u.getSubtitle(x));
}
// Date de création.
replace(xReplaceable,"<d:cree_le>", "01/07/1978");
XPrintable xprintable = ( XPrintable ) UnoRuntime.queryInterface( XPrintable.class,
objectDocumentToStore );
if(xprintable == null) {
System.out.println("xprintable est null");
}
propertyvalue = new PropertyValue[ 1 ];
propertyvalue[ 0 ] = new PropertyValue();
propertyvalue[ 0 ].Name = "FileName";
propertyvalue[ 0 ].Value = psPathFile;
xprintable.print(propertyvalue);
// Getting the method dispose() for closing the document
XComponent xcomponent =
( XComponent ) UnoRuntime.queryInterface( XComponent.class,xprintable );
xcomponent.dispose();
String cmd[] = new String[3];
cmd[0] = "ps2pdf";
cmd[1] = psPathFile;
cmd[2] = pdfPathFile;
Runtime.getRuntime().exec(cmd).waitFor();
}
catch( Exception exception ) {
exception.printStackTrace();
return( "" );
}
// Returning the name of the converted file
return( pdfPathFile );
}
private void replace(XReplaceable xReplaceable,String search,String replace) {
com.sun.star.util.XReplaceDescriptor xReplaceDescriptor;
// You need a descriptor to set properies for Replace
xReplaceDescriptor = ( com.sun.star.util.XReplaceDescriptor) xReplaceable.createReplaceDescriptor();
xReplaceDescriptor.setSearchString(search);
xReplaceDescriptor.setReplaceString(replace);
// Replace all words
xReplaceable.replaceAll( xReplaceDescriptor );
}
} |
Partager