Ask Your Question
0

Replace text with image

asked 2018-01-26 15:57:26 +0200

Evil Overlord gravatar image

updated 2018-01-26 15:57:43 +0200

I'd like to replace specified text (e.g., ###) with a specific image. I haven't found how to do this, and would appreciate advice.

As a subsidiary issue, the image will recur many times in the document. Is there a way to incorporate it that will take less space - e.g. includes only one copy, but links to it, so that visually the image is present everywhere?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2018-02-09 12:33:36 +0200

librebel gravatar image

Hello @Evil Overlord,

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/en/question/144163/replace-text-with-image/    
    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

edit flag offensive delete link more

Comments

Thanks. This looks a little more intensive than I had anticipated, but seems a viable solution, so I'll test it out.

Evil Overlord gravatar imageEvil Overlord ( 2018-03-27 10:36:14 +0200 )edit

So, I'd either use fixed images and search strings, or use a dialog box to get inputs and then execute the calls, correct?

Evil Overlord gravatar imageEvil Overlord ( 2018-03-27 15:09:13 +0200 )edit

Got the following error in the embed image function. (Maybe because I'm on Windows, and not using the right file path?): BASIC runtime error. An exception occurred Type: com.sun.star.container.NoSuchElementException Message: .

Evil Overlord gravatar imageEvil Overlord ( 2018-03-27 15:18:18 +0200 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2018-01-26 15:57:26 +0200

Seen: 418 times

Last updated: Feb 09 '18