How to add embedded images from extension into Writer doc

I’m working on an extension using a BASIC Macro that adds a preset image into a Writer document, but I can’t find a way to add the image from the extension itself, only from a predetermined location.

Here’s my code so far:

Sub add_image()
    doc = ThisComponent
    text = doc.CurrentSelection.getByIndex(0).End

    image = doc.createInstance("")
    image.GraphicURL = ConvertToURL("C:\images\image.png")
    image.Width = 2015
    image.Height = 705

    cursor = doc.Text.createTextCursorByRange(text)
    doc.Text.insertTextContent(cursor, image, False)

End Sub

I’ve tried relative addressing (images/image.png), using %origin%/images/image.png, and making the GraphicURL a base64 encoded version of the image, but none of these work (instead of the image it says “Read Error”). It would also be possible to use PathSubstitution to find $(user), but I can’t see how to find the extension ID to get the location the extension has been installed to, and am not sure if this would work on all OSes.

Is it possible, or is an absolute URL the only way to add an image?

Wouldn’t it be simpler to create an AutoText with the image and hand over a corresponding .bau file?

I was hoping to make this a self-contained extension, that would add a button to the toolbar to add the image. AutoText would be great, but I think it’s only accessible via Tools > Autotext, and then selecting the AutoText, unless there’s a way to add a specific Autotext to a toolbar?

You type the AutoText shortcut in your text and hit F3. More versatile than a button in the toolbar.
You didn’t describe the purpose of your image; you asked to help you debug a to-be-solution of yours. If you tell us what the image is and what it is for, we could suggest an alternative solution which doesn’t potentially damage document encoding. Any insertion by a macro either results in direct formatting which is no longer controllable by styles or creates potential later problems.

It’s adding a CreativeCommons license image, with a shortcut to the relevant CC license page.

I appreciate that there might be other ways to implement it, but the visibility of having a toolbar button is important to me. Even if I choose another way, I’d still be interested in the original question: whether it’s possible to add an image from an extension.

As a proof of concept, here is a demo AutoText: Licenses.bau.odt (17.3 KB)
After downloading, erase .odt extension to keep only Licenses.bau. Install it in any reachable AutoText directory (see Tools>Options, LibreOffice>Paths).

In a blank document, type CCLF3. You get a CC-BY-SA icon and a hyperlink to licence text, all in a paragraph styled Subtitle.

1 Like

Thanks :slightly_smiling_face: :+1:

With your extension ID, you can find the route where it is installed, then refer to any other file within it.

I’m sorry, just do it with Python.

    ID_EXTENSION = 'mytools.mri'
    SERVICE = '/singletons/'

    CTX = uno.getComponentContext()
    pip = CTX.getValueByName(SERVICE)
    path = pip.getPackageLocation(ID_EXTENSION)

1 Like

Thanks! Will look into Python (LO says I need to have JDK installed to run Python macros, but I guess this won’t be the case when it’s packaged as an .oxt extension?)

No, it’s not true you need JDK for macros Python.

1 Like

Please tell the details. Some years ago, I killed many situations where Java prompt was shown unnecessarily; I don’t know of ~recent complaints about it - except your remark. If you have a reproducible scenario, please file a bug report.

Checking it more closely, it’s asking for JRE, not JDK, but this does still seem like it could be a bug. Each time I start LibreOffice Writer (Windows 10), and choose Tools > Macros > Organise Macros > Python … I get the following error message: “JRE Required” “LibreOffice requires a 64-bit Java runtime environment (JRE) to perform this task. Please install a JRE and restart LibreOffice.”. The Python Macros dialog box then appears with various default Macros (Capitalise, Hello World, InsertText, etc.), with the Create, Edit, Rename, and Delete buttons greyed out, but clicking Run on a selected macro runs it without any problems.

I get the same message for the Beanshell and Javascript options under Tools > Macros > Organise Macros, but not for Basic. Without any Beanshell or Javascript default macros, I haven’t tried testing if the Run button works for those languages. Will file a bug report if this isn’t right.

It definitely isn’t right (as @elmau said, JRE is absolutely not needed for Python; it is needed for BeanShell and JS, because they are implemented using Java, though). And it is silent on my system with JRE disabled (Version: (X86_64) / LibreOffice Community
Build ID: 2c5e46c1980ec5241359fd65d751dc518205e7af
CPU threads: 12; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: ru-RU (ru_RU); UI: en-US
Calc: CL threaded).

The first thing would be to test with all extensions disabled, or maybe in safe mode.

I’ve now tried disabling all extensions, and Safe Mode (still getting the same error both times), so have filed this bug: 156161 – Organise Macros > Python requires JRE

Thanks for all your work on this :+1:

1 Like