Replace text placeholder with image using Java

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sumit7325
Posts: 6
Joined: Tue Apr 07, 2020 3:28 pm

Replace text placeholder with image using Java

Post by sumit7325 »

Hi,
I want to replace a text with an image, I can insert the image but the position of the image is wrong and the text also is not replacing, below is sample code and attached the DOCX file for reference, text place holder can be located at anywhere, header, footer, etc.

Code: Select all

        String templateUrl = "file:///Users/Desktop/Test.docx";
        String signatureImg = "file:///Users/Desktop/logo.png";
        XTextRange xTextRange = null;
        XTextViewCursorSupplier supTextViewCursor = null;
        XController xController = null;
        Object obj = null;
        XComponent xComp = null;
        XComponentContext xContext = bootstrapContext();
        XMultiComponentFactory xRemoteServiceManager  = xContext.getServiceManager();

XDesktop xDesktop = createInstanceMCF(XDesktop.class, "com.sun.star.frame.Desktop", xContext, xRemoteServiceManager);

            XComponentLoader xComponentLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, xDesktop);

            PropertyValue[] loadProps = new PropertyValue[1];
            loadProps[0] = new PropertyValue();
            loadProps[0].Name = "Hidden";
            loadProps[0].Value = true;

            xComp = xComponentLoader.loadComponentFromURL(templateUrl, "_blank", 0, loadProps);
            XTextDocument xTextDoc = (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, xComp);

            xController = xTextDoc.getCurrentController();
            supTextViewCursor = (XTextViewCursorSupplier) UnoRuntime.queryInterface(XTextViewCursorSupplier.class, xController);
            XTextViewCursor curTextView = supTextViewCursor.getViewCursor();

            // Get page cursor and assigns XTextViewCursor cursor to page
            XPageCursor curPage = (XPageCursor) UnoRuntime.queryInterface(XPageCursor.class, curTextView);
            System.out.println("The current page number is " + curPage.getPage());

            // Querying for the interface XMultiServiceFactory on the XTextDocument
            XMultiServiceFactory xMSFDoc = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, xTextDoc);

            // Get XText
            XText xText = xTextDoc.getText();

            XReplaceDescriptor xReplaceDescr = null;
            XSearchDescriptor xSearchDescr = null;
            XReplaceable xReplaceable = null;
            XSearchable xSearchable = null;
            XStorable xStorable = null;
            xReplaceable = (XReplaceable) UnoRuntime.queryInterface(XReplaceable.class, xTextDoc);
            xReplaceDescr = xReplaceable.createReplaceDescriptor();
            xSearchable = (XSearchable) UnoRuntime.queryInterface(XSearchable.class, xTextDoc);
            xSearchDescr = xSearchable.createSearchDescriptor();
            xSearchDescr.setSearchString("${logo}");
            Point point = null;
            // Search String and get its first occurrence and get  XInterface
            XInterface xInterface = (XInterface) xSearchable.findFirst(xSearchDescr);
            System.out.println("XInterface=" + xInterface);
            if (xInterface != null) {
                System.out.println("XInterface-->" + xInterface.toString());
                xTextRange = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface(
                        com.sun.star.text.XTextRange.class, xInterface);
                System.out.println("xTextRange-->" + xTextRange.getString());
                curTextView.gotoRange(xTextRange, false);

                //get cursor position
                point = curTextView.getPosition();
                System.out.println("Point X=" + point.X + " Y=" + point.Y);
            }

//    (If I create xTextCursor with xInterface then code throws an error cursor and doc position is not matching)     XTextCursor xTextCursor = (com.sun.star.text.XTextCursor) UnoRuntime.queryInterface(com.sun.star.text.XTextCursor.class, xInterface);

            XTextCursor xTextCursor   = xTextDoc.getText().createTextCursor();;
            Object graphicObjShape = null;
            graphicObjShape = xMSFDoc.createInstance("com.sun.star.drawing.GraphicObjectShape");

            // Customizing graphic shape position and size
            XShape shapeSettings = (XShape) UnoRuntime.queryInterface(XShape.class, graphicObjShape);
            shapeSettings.setSize(new Size(600, 600));
            //shapeSettings.setPosition(point);

            // Creating bitmap ccontainer service
            XNameContainer bitmapContainer = UnoRuntime.queryInterface(XNameContainer.class,
                    xMSFDoc.createInstance("com.sun.star.drawing.BitmapTable"));
            // Inserting test image to the container
            bitmapContainer.insertByName("logo", signatureImg);
            // Querying for the interface XPropertySet on the graphic object
            XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, graphicObjShape);

            System.out.println("Test img url is" + bitmapContainer.getByName("logo"));

            // Setting Signature image internal URL to graphic shape property
            xPropSet.setPropertyValue("GraphicURL", bitmapContainer.getByName("logo"));

            // Convert graphic shape to the text content item
            XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface(XTextContent.class, graphicObjShape);

            // Embed image into the document text with replacement
            System.out.println("Inserting image...");
            xText.insertTextContent(xTextCursor, xTextContent, true);
Any help is appreciated.
Attachments
Test.docx
(31.25 KiB) Downloaded 178 times
NeoOffice 2.2.3 with MacOS 10.4
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Replace text placeholder with image using Java

Post by Villeroy »

This forum is about Apache OpenOffice, LibreOffice, Neo Office. Whatever you try to do with a docx file, you have to do that with Microsoft products because these file formats are designed to be handled by Microsoft products exclusively. Even if LibreOffice is fairly compatible with that stuff, there can not be any API compatibility because compatibility on API level would mean that someone has written a clone of MS Word which won't happen as long as MS Word is availlable for free or for little money.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply