How to Search text tags and replace with Image in Header/Footer/Table of Openoffice Document using Delphi

▼魔方 西西 提交于 2019-12-05 11:47:32

Headers, tables and so on have their own text object, so the text object of the main document will not work. Instead, get the text object and cursor from Found.

Also, remove . from the regular expression to match multiple digits instead of multiple of anything. And the brackets must be literal.

Here is working Basic code.

Sub ReplaceTextTagsWithImage
    Document = ThisComponent
    Bitmaps = Document.createInstance("com.sun.star.drawing.BitmapTable")
    ImageFile = "C:/google_wht.gif"
    SearchDescriptor = Document.createSearchDescriptor()
    SearchDescriptor.setSearchString("\[CHART=[0-9]*\]")
    SearchDescriptor.SearchRegularExpression = True
    Found = Document.findFirst(SearchDescriptor)
    Do While Not IsNull(Found)
        TextCursor = Found.getText().createTextCursor()
        TextCursor.gotoRange(Found, False)
        Graphic = Document.createInstance("com.sun.star.text.GraphicObject")
        gurl = "file:///" & ImageFile
        gname = sNumber & "_Image"
        if Not Bitmaps.hasbyname(gname) Then
            Bitmaps.insertByName(gname, gurl)
        End If
        Graphic.GraphicURL = Bitmaps.getByName(gname)
        Graphic.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
        Graphic.Width = 6000
        Graphic.Height = 8000
        TextCursor.getText().insertTextContent(TextCursor, Graphic, False)
        Found = ThisComponent.findNext( Found.End, SearchDescriptor)
    Loop
End Sub
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!