Unable to insert multiple images in writer

Java, C++, C#, Delphi, ??? - Using the UNO bridges

Unable to insert multiple images in writer

Postby freelancer.vijaya » 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: :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   Expand viewCollapse view

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
freelancer.vijaya
 
Posts: 2
Joined: Tue Jun 06, 2017 4:54 pm

Re: Unable to insert multiple images in writer

Postby RoryOF » Tue Jun 06, 2017 5:30 pm

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.6 on Xubuntu 18.04 (mostly 64 bit version) and infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 27646
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Unable to insert multiple images in writer

Postby freelancer.vijaya » Tue Jun 06, 2017 6:10 pm

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
freelancer.vijaya
 
Posts: 2
Joined: Tue Jun 06, 2017 4:54 pm

Re: Unable to insert multiple images in writer

Postby Zizi64 » Tue Jun 06, 2017 6:17 pm

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; LO4.4.7, LO6.1.1 on Win7x64Prof.
PortableApps, winPenPack: LO3.3.0-LO6.1.2 and AOO4.1.5
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.
User avatar
Zizi64
Volunteer
 
Posts: 7442
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary


Return to External Programs

Who is online

Users browsing this forum: No registered users and 2 guests