Hello @EvilOverlord,
To embed an image into your LibreOffice document, you could use the Basic macro EmbedImage()
provided in the code section below.
To find all occurrences of a specified string pattern such as “###” within the current Writer document, and replace all the found occurrences with a previously embedded image, you could use the Basic method Writer_replaceAll_With_Bitmap()
presented also in the code section below:
The embedded image will be inserted into your normal text flow using Anchor Type = AS_CHARACTER, so that the image will become part of the text flow.
Example call:
EmbedImage( "file:///home/user/Desktop/my_image.png", "My_Embedded_Image" )
Writer_replaceAll_With_Bitmap( "###", "My_Embedded_Image" )
Code:
Function EmbedImage( strImageFileURL As String, strImageName as String, Optional oDocument ) As String
REM Embed an image into a LibreOffice document*.
REM <strImageFileURL> : URL or Full Path to the Image file to be embedded.
REM Will cause an Error if the URL is invalid, or if the URL points to an unsupported image file.
REM Pass "" here, in order to Delete the embedded bitmap named <strImageName>.
REM <strImageName> : Name to uniquely identify this image in the BitmapTable; usually the filename without extension.
REM <oDocument> : [OPTIONAL] The document to embed the image in. [DEFAULT]= The current document.
REM *For Base, this argument is required and must be a FormDocument in Design Mode ( not the DatabaseDocument ).
REM Returns the internal image URL to the embedded image ( vnd.sun.star.graphicObject ).
If IsMissing( oDocument ) Or IsEmpty( oDocument ) Or IsNull( oDocument ) Then oDocument = ThisComponent
Dim oBitmapTable As Object
oBitmapTable = oDocument.createInstance( "com.sun.star.drawing.BitmapTable" )
If Not IsNull( oBitmapTable ) Then
If oBitmapTable.hasByName( strImageName ) Then
If strImageFileURL = "" Then
oBitmapTable.removeByName( strImageName )
Else
oBitmapTable.replaceByName( strImageName, strImageFileURL )
End If
Else
oBitmapTable.insertByName( strImageName, strImageFileURL )
End If
EmbedImage = oBitmapTable.getByName( strImageName )
End If
End Function
Sub Writer_replaceAll_With_Bitmap( strSearchPattern As String, strBitmapName As String )
REM Finds all occurrences of the specified string pattern in the current Writer document,
REM and replaces the found occurences with a previously embedded bitmap image.
REM <strSearchPattern> : String to be found/replaced within the current Writer document. Supports RegExp.
REM <strBitmapName> : Name of a bitmap image embedded in the current Writer document.
REM See: https://ask.libreoffice.org/t/replace-text-with-image/30514
Dim oDocument As Object : oDocument = ThisComponent
Dim oBitmapTable As Object
oBitmapTable = oDocument.createInstance( "com.sun.star.drawing.BitmapTable" )
If oBitmapTable.hasByName( strBitmapName ) Then
Dim sGraphicURL As String : sGraphicURL = oBitmapTable.getByName( strBitmapName )
Dim oSearchDescriptor As Object : oSearchDescriptor = oDocument.createSearchDescriptor()
oSearchDescriptor.setSearchString( strSearchPattern )
oSearchDescriptor.SearchRegularExpression = True REM Set to <False> for normal search.
Dim oSearchResults As Object : oSearchResults = oDocument.findAll( oSearchDescriptor )
Dim oFound As Object
Dim i As Integer
For i = 0 To oSearchResults.getCount() - 1
oFound = oSearchResults.getByIndex( i )
Dim oGraphic As Object
oGraphic = oDocument.createInstance( "com.sun.star.text.TextGraphicObject" )
oGraphic.GraphicURL = sGraphicURL
oGraphic.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
oFound.Text.insertTextContent( oFound, oGraphic, True)
Next i
End If
End Sub
With Regards, lib