Unable to insert multiple images in writer
Posted: Tue Jun 06, 2017 5:06 pm
Hi,
I am trying to run the sample program that I got from https://wiki.openoffice.org/wiki/API/Sa ... csInserter
I have around 10 images in a folder.
I am trying to insert them all in the sample code below
The problem is I am unable to insert all images in single document.
When i was trying to modify the programe, the images either didnt appear in same doc or overlapped each other.
Need help on how to add all images in single document and also the images should be inserted one after another vertically.
So, please help me to do the following:
Open doc
start loop:
insert image1
insert image2
.
.
.
insert imagen
save and close the doc and exit.
Also, the images should appear one after another vertically.
I've been breaking my head for almost 3 hrs now...
Thanks in advance.
I am trying to run the sample program that I got from https://wiki.openoffice.org/wiki/API/Sa ... csInserter
I have around 10 images in a folder.
I am trying to insert them all in the sample code below
The problem is I am unable to insert all images in single document.
When i was trying to modify the programe, the images either didnt appear in same doc or overlapped each other.
Need help on how to add all images in single document and also the images should be inserted one after another vertically.
So, please help me to do the following:
Open doc
start loop:
insert image1
insert image2
.
.
.
insert imagen
save and close the doc and exit.
Also, the images should appear one after another vertically.
I've been breaking my head for almost 3 hrs now...
Thanks in advance.
Code: Select all
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.bridge.UnoUrlResolver;
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.text.TextContentAnchorType;
import com.sun.star.text.XText;
import com.sun.star.text.XTextContent;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;
import com.sun.star.ucb.XFileIdentifierConverter;
import com.sun.star.ucb.XSimpleFileAccess;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
public class GraphicsInserter {
private XComponentContext m_xContext = null;
private XMultiComponentFactory m_xMCF = null;
private XFileIdentifierConverter xFileConverter = null;
private XSimpleFileAccess xSFA = null;
public static void main(String args[]) {
GraphicsInserter graphicExample = new GraphicsInserter();
try {
System.out.println("START");
graphicExample.runDemo();
System.out.println("END");
} catch (java.lang.Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
System.exit(0);
}
}
protected void runDemo() throws java.lang.Exception {
XTextDocument xTextDoc = createTextDocument();
XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class,
xTextDoc);
Object oGraphic = xMSFDoc.createInstance("com.sun.star.text.TextGraphicObject");
System.out.println("2222222");
XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, oGraphic);
XText xText = xTextDoc.getText();
XTextCursor xTextCursor = xText.createTextCursor();
xText.insertTextContent(xTextCursor, xTextContent, true);
XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oGraphic);
Integer vvv = new Integer(0);
int i = 1;
while (i <= 5) {
String m_sGraphicFileURL;
vvv+=new Integer(5000);
java.io.File sourceFile = new java.io.File("D:/images/" + i + ".png");
m_sGraphicFileURL = convertToURL("", sourceFile.getAbsolutePath());
if (!checkFile(m_sGraphicFileURL)) {
System.out.println("Impossible to locate the file " + m_sGraphicFileURL);
}
xPropSet.setPropertyValue("AnchorType", TextContentAnchorType.AT_PAGE);
xPropSet.setPropertyValue("GraphicURL", m_sGraphicFileURL);
xPropSet.setPropertyValue("VertOrientPosition", vvv);
i++;
Thread.sleep(1800);
}
}
private XMultiComponentFactory getMultiComponentFactory() {
try {
if (m_xContext == null && m_xMCF == null) {
System.out.println("ATTEMPTING Connection...");
m_xContext = Bootstrap.createInitialComponentContext(null);
XUnoUrlResolver urlResolver = UnoUrlResolver.create(m_xContext);
Object initialObject = urlResolver
.resolve("uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager");
System.out.println("Connected to a running office ...");
m_xMCF = (XMultiComponentFactory) UnoRuntime.queryInterface(XMultiComponentFactory.class,
initialObject);
}
} catch (Exception e) {
System.out.println("~~~~~~~");
e.printStackTrace();
}
return m_xMCF;
}
private XComponent newDocComponent(String docType) throws java.lang.Exception {
String loadUrl = "private:factory/" + docType;
m_xMCF = this.getMultiComponentFactory();
XPropertySet xProperySet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, m_xMCF);
Object oDefaultContext = xProperySet.getPropertyValue("DefaultContext");
XComponentContext xOfficeComponentContext = (XComponentContext) UnoRuntime
.queryInterface(XComponentContext.class, oDefaultContext);
Object desktop = m_xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", m_xContext);
XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class,
desktop);
PropertyValue[] loadProps = new PropertyValue[0];
return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps);
}
private XTextDocument createTextDocument() {
XTextDocument aTextDocument = null;
try {
XComponent xComponent = newDocComponent("swriter");
aTextDocument = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComponent);
} catch (Exception e) {
e.printStackTrace(System.err);
}
return aTextDocument;
}
private boolean checkFile(String aURL) {
boolean bExists = false;
try {
if(xSFA ==null)
xSFA = (XSimpleFileAccess) UnoRuntime.queryInterface(XSimpleFileAccess.class,
this.getMultiComponentFactory().createInstanceWithContext("com.sun.star.ucb.SimpleFileAccess",
m_xContext));
bExists = xSFA.exists(aURL) && !xSFA.isFolder(aURL);
} catch (com.sun.star.ucb.CommandAbortedException ex) {
ex.printStackTrace();
} catch (com.sun.star.uno.Exception ex) {
ex.printStackTrace();
} catch (java.lang.Exception ex) {
ex.printStackTrace();
}
return bExists;
}
/**
* Converts an URL into a system path using OOo API
*
* @param sURLPath
* @return
*/
private String convertFromURL(String sURLPath) {
String sSystemPath = null;
try {
m_xMCF = getMultiComponentFactory();
xFileConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface(
XFileIdentifierConverter.class,
m_xMCF.createInstanceWithContext("com.sun.star.ucb.FileContentProvider", m_xContext));
sSystemPath = xFileConverter.getSystemPathFromFileURL(sURLPath);
} catch (com.sun.star.uno.Exception e) {
e.printStackTrace(System.err);
} finally {
return sSystemPath;
}
}
/**
* Converts a system path into an URL using OOo API
*
* @param sBase
* @param sSystemPath
* @return
*/
private String convertToURL(String sBase, String sSystemPath) {
String sURL = null;
try {
m_xMCF = getMultiComponentFactory();
xFileConverter = (XFileIdentifierConverter) UnoRuntime.queryInterface(
XFileIdentifierConverter.class,
m_xMCF.createInstanceWithContext("com.sun.star.ucb.FileContentProvider", m_xContext));
sURL = xFileConverter.getFileURLFromSystemPath(sBase, sSystemPath);
} catch (com.sun.star.uno.Exception e) {
e.printStackTrace();
} catch (Exception e) {
System.out.println("sss");
e.printStackTrace();
} finally {
return sURL;
}
}
}