import java.io.Serializable;
public class FindReplace implements Serializable{
String find;
String replace;
public FindReplace() {
// TODO Auto-generated constructor stub
}
public FindReplace(String bookmark, String fieldName) {
this.find = bookmark;
this.replace = fieldName;
}
public String getFind() {
return find;
}
public void setFind(String find) {
this.find = find;
}
public String getReplace() {
return replace;
}
public void setReplace(String replace) {
this.replace = replace;
}
}
package com.documentProcessing;
import java.io.Serializable;
import java.util.List;
import com.sun.star.frame.XDesktop;
import com.sun.star.text.XTextDocument;
/**
* Class created to help the document manipulation.
*
* @author Marco Silva
*
*/
public class OpenOffice implements Serializable {
// Open Office Controllers
public XDesktop xDesktop;
public XTextDocument xTextDocument;
// Process Document
public String libraryDocumentName;
public String documentName;
public List findReplaceList;
public String getDocumentName() {
return documentName;
}
public void setDocumentName(String documentName) {
this.documentName = documentName;
}
public List getFindReplaceList() {
return findReplaceList;
}
public void setFindReplaceList(List findReplaceList) {
this.findReplaceList = findReplaceList;
}
public String getLibraryDocumentName() {
return libraryDocumentName;
}
public void setLibraryDocumentName(String libraryDocumentName) {
this.libraryDocumentName = libraryDocumentName;
}
public XDesktop getXDesktop() {
return xDesktop;
}
public void setXDesktop(XDesktop desktop) {
xDesktop = desktop;
}
public XTextDocument getXTextDocument() {
return xTextDocument;
}
public void setXTextDocument(XTextDocument textDocument) {
xTextDocument = textDocument;
}
public void closeDocument() {
try {
if (xTextDocument != null) xTextDocument.dispose();
if (xDesktop != null) xDesktop.terminate();
} catch (Exception e) {}
}
}
package com.documentProcessing;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.ejb.Stateless;
import ooo.connector.BootstrapSocketConnector;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XDesktop;
import com.sun.star.frame.XStorable;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.text.XTextDocument;
import com.sun.star.text.XTextRange;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.XInterface;
import com.sun.star.util.XSearchDescriptor;
import com.sun.star.util.XSearchable;
public class OpenOfficeManager {
OpenOffice oofice;
/**
* Create the OpenOffice Document Controller
*
* @return OpenOffice
*/
public void openOfficeDocument(String docLibrary, String docName,
List
oofice = new OpenOffice();
XMultiComponentFactory xMCF = null;
try {
// TODO: Make sure the location of the open office is correct
// String sOffice = System.getProperty( "os.name" ).startsWith(
// "Windows" ) ? "C:\\Program Files\\OpenOffice.org 2.0\\program" :
// "//home//eanilkumar//openoffice//usr//lib//openoffice//program";
String sOffice = "/opt/openoffice.org2.3/program";
// Bootstrap open office with the location of the OpenOffice
// executable file
/*
* URL[] jarList = new URL[] { new URL("file://" + sOffice) };
* URLClassLoader loader = new URLClassLoader(jarList);
*/
XComponentContext xContext = BootstrapSocketConnector
.bootstrap(sOffice);
// Get the remote office service manager
xMCF = xContext.getServiceManager();
if (xMCF != null) {
Object oDesktop = xMCF.createInstanceWithContext(
"com.sun.star.frame.Desktop", xContext);
oofice.xDesktop = (XDesktop) UnoRuntime.queryInterface(
XDesktop.class, oDesktop);
} else
System.out
.println("Can't create a desktop. No connection, no remote office servicemanager available!");
// Update documents names
oofice.documentName = docName;
oofice.libraryDocumentName = docLibrary;
oofice.findReplaceList = findReplace;
openTextdocument();
} catch (Exception e) {
System.out.println("Exception in Document opening");
e.printStackTrace();
}
}
/**
*
*/
public void closeDocument() {
oofice.closeDocument();
}
/**
* Open the document library
*
* @param OpenOffice
* oofice
*/
public void openTextdocument() {
try {
XComponent xComponent = openDocument(oofice.xDesktop,
oofice.libraryDocumentName + oofice.documentName);
oofice.xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, xComponent);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
/**
* @param XDesktop
* xDesktop - Open Office Desktop, used to open the document
* @param sURL -
* location of the document
*
* @return XComponent - the component - internal open office document
* controller
*/
protected XComponent openDocument(XDesktop xDesktop, String sURL) {
XComponent xComponent = null;
XComponentLoader xComponentLoader = null;
// Open the document in hidden mode, will not display the document
PropertyValue xArgs[] = new PropertyValue[1];
xArgs[0] = new PropertyValue();
xArgs[0].Name = "Hidden";
xArgs[0].Value = new Boolean(true);
try {
xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(
XComponentLoader.class, xDesktop);
// xComponent =
// xComponentLoader.loadComponentFromURL("private:factory/scalc",
// "_blank", 0, xArgs);
xComponent = xComponentLoader.loadComponentFromURL(sURL, "_blank",
0, xArgs);
// xComponent = xComponentLoader.loadComponentFromURL("", "_blank",
// 0, xArgs);
} catch (Exception e) {
e.printStackTrace();
}
return xComponent;
}
/**
* Save the document as PDF
*
* @param OpenOffice
* oofice
*/
public void savePDFDocument() {
PropertyValue xValues[] = new PropertyValue[2];
xValues[0] = new PropertyValue();
xValues[0].Name = "CompressMode";
xValues[0].Value = "0";
xValues[1] = new PropertyValue();
xValues[1].Name = "FilterName";
xValues[1].Value = "writer_pdf_Export";
XStorable xStorable = (XStorable) UnoRuntime.queryInterface(
XStorable.class, oofice.xTextDocument);
try {
System.out.println("Pdf Dpcument to save path"
+ oofice.libraryDocumentName + oofice.documentName);
xStorable.storeToURL(oofice.libraryDocumentName + "tmp//"
+ oofice.documentName + ".pdf", xValues);
} catch (Exception e) {
e.printStackTrace();
}
}
public String GetDocumentText() {
return oofice.xTextDocument.getText().getString();
}
/**
* Find all specific string in the document and replace for another string.
* Used in merge database information in the document
*
* @param OpenOffice
* oofice
*/
public void replaceDocument(Map record) {
if (oofice.findReplaceList == null)
return;
Iterator ite = oofice.findReplaceList.iterator();
while (ite.hasNext()) {
// Map findReplace = (Map) ite.next();
FindReplace findReplace = (FindReplace) ite.next();
// String value = (String) record.get((String)
// findReplace.get("library_field_field"));
// String bookmark = (String)
// findReplace.get("library_field_bookmark");
String value = (String) record.get((String) findReplace
.getReplace());
String bookmark = (String) findReplace.getFind();
if ((value != null) && (bookmark != null))
replaceDocumentText(oofice.xTextDocument, bookmark, value);
}
}
/**
* @param XTextDocument
* xTextDocument - Open Office Document
* @param String
* find - string to find in the document
* @param String
* replace - string to replace
*/
protected void replaceDocumentText(XTextDocument xTextDocument,
String find, String replace) {
XInterface xSearchInterface = null;
XTextRange xSearchTextRange = null;
try {
do {
// the findfirst returns a XInterface
xSearchInterface = (XInterface) FindFirst(xTextDocument, find);
if (xSearchInterface != null) {
// get the TextRange form the XInterface
xSearchTextRange = (XTextRange) UnoRuntime.queryInterface(
XTextRange.class, xSearchInterface);
xSearchTextRange.setString(replace);
}
} while (xSearchInterface != null);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param XTextDocument
* xTextDocument - Open Office Document
* @param sSearchString -
* string to search
*
* @return XInterface - position where the string are
*/
protected XInterface FindFirst(XTextDocument xTextDocument,
String sSearchString) {
XSearchDescriptor xSearchDescriptor = null;
XSearchable xSearchable = null;
XInterface xSearchInterface = null;
try {
xSearchable = (XSearchable) UnoRuntime.queryInterface(
XSearchable.class, xTextDocument);
xSearchDescriptor = (XSearchDescriptor) xSearchable
.createSearchDescriptor();
xSearchDescriptor.setSearchString(sSearchString);
XPropertySet xPropertySet = null;
xPropertySet = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xSearchDescriptor);
xPropertySet.setPropertyValue("SearchRegularExpression",
new Boolean(true));
xSearchInterface = (XInterface) xSearchable
.findFirst(xSearchDescriptor);
} catch (Exception e) {
e.printStackTrace();
}
return xSearchInterface;
}
public static void main(String[] args) {
}
public void processDocument(String filePath, String fileName,
List
OpenOfficeManager openOfficeManager = new OpenOfficeManager();
Map
record.put("firstname", "Marco");
record.put("lastname", "Silva");
record.put("coordinator", "WILLIS HALE");
List
FindReplace opt1 = new FindReplace("<
FindReplace opt2 = new FindReplace("<
FindReplace opt3 = new FindReplace("<
fr.add(opt1);
fr.add(opt2);
fr.add(opt3);
openOfficeManager.openOfficeDocument("file:///home//eanilkumar//",
"Test.doc", fr);
openOfficeManager.replaceDocument(record1);
openOfficeManager.savePDFDocument();
System.out.println(openOfficeManager.GetDocumentText());
openOfficeManager.closeDocument();
}
}
No comments:
Post a Comment