Unable to insert multiple images in writer

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
freelancer.vijaya
Posts: 2
Joined: Tue Jun 06, 2017 4:54 pm

Unable to insert multiple images in writer

Post by freelancer.vijaya »

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: :roll: :roll:


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... :crazy: :crazy: :crazy:


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;
		}
	}

}

OO beginner OpenOffice 3.1 on Windows
User avatar
RoryOF
Moderator
Posts: 34802
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Unable to insert multiple images in writer

Post by RoryOF »

Analyse where the insertion cursor is and figure out how to move it about. Change AnchorType for the graphics to another value (you need to understand what these are and what they do). One cannot just take code and expect it to work exactly for your requirements - you must understand what it is doing.

You have already asked this question on the OO dev list.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
freelancer.vijaya
Posts: 2
Joined: Tue Jun 06, 2017 4:54 pm

Re: Unable to insert multiple images in writer

Post by freelancer.vijaya »

Hi Rory,

I have tried modifying the anchor as advised by you. However, the images do overlap, as each image has the same anchor settings.

Also anchoring is to specify if the image should stick to paragraph or page or frame and so on....

My requirement is i write some text and some images to the doc. It can be anywhere in the doc. Start of the doc or in between text (not wrapped) or at the end of doc. All is fine till I have only one image in the whole doc. However, If I need to write multiple images, continuously as i specified in my above post. The images overlap each other.

In fact I tried inserting (ctrl+c on image file and ctrl+v) the image manually in the writer. Images are overlapping even if I do it that way. I even tried the insert image through menu manually in the writer. The images are still getting overlapped.

The only way to allign them vertically is to insert all the images and dragging using mouse pointer.
In that case only the vertical alignment changes.

Now, my problem is I do the image insertion through java. In java. I was wondering if I could compute the image height and thus set the image position.
OO beginner OpenOffice 3.1 on Windows
User avatar
Zizi64
Volunteer
Posts: 11426
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Unable to insert multiple images in writer

Post by Zizi64 »

Now, my problem is I do the image insertion through java. In java. I was wondering if I could compute the image height and thus set the image position.
Try to anchor the pictures to the page. In this case you can compute (apriximately) the positions of the picturtes (in one page) based on the size of the pictures.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
Post Reply