How to insert image in OpenOffice Document using Delphi

前端 未结 1 1811
粉色の甜心
粉色の甜心 2021-01-20 14:37

I am using approach mentioned in accepted solution of How to Search and Replace in odt Open Office document? for search and replace text in odt document using Delphi

相关标签:
1条回答
  • 2021-01-20 15:25

    The following code was adapted from Listing 5.24 of Andrew Pitonyak's Macro Document.

    ServiceManager := CreateOleObject('com.sun.star.ServiceManager');
    Desktop := ServiceManager.createInstance('com.sun.star.frame.Desktop');
    NoParams := VarArrayCreate([0, -1], varVariant);
    Document := Desktop.loadComponentFromURL('private:factory/swriter', '_blank', 0, NoParams);
    Txt := Document.getText;
    TextCursor := Txt.createTextCursor;
    {TextCursor.setString('Hello, World!');}
    Graphic := Document.createInstance('com.sun.star.text.GraphicObject');
    Graphic.GraphicURL := 'file:///C:/path/to/my_image.jpg';
    Graphic.AnchorType := 1; {com.sun.star.text.TextContentAnchorType.AS_CHARACTER;}
    Graphic.Width := 6000;
    Graphic.Height := 8000;
    Txt.insertTextContent(TextCursor, Graphic, False);
    

    More information on using OpenOffice with Pascal is at https://www.freepascal.org/~michael/articles/openoffice1/openoffice.pdf.

    EDIT:

    This code inserts SHOW_CHART=123 and SHOW_CHART=456 as an example. Then it finds these strings and replaces them with the corresponding image.

    Txt.insertString(TextCursor, 'SHOW_CHART=123' + #10, False);
    Txt.insertString(TextCursor, 'SHOW_CHART=456' + #10, False);
    SearchDescriptor := Document.createSearchDescriptor;
    SearchDescriptor.setSearchString('SHOW_CHART=[0-9]+');
    SearchDescriptor.SearchRegularExpression := True;
    Found := Document.findFirst(SearchDescriptor);
    While Not (VarIsNull(Found) or VarIsEmpty(Found) or VarIsType(Found,varUnknown)) do
    begin
        IdNumber := copy(String(Found.getString), Length('SHOW_CHART=') + 1);
        Found.setString('');
        Graphic := Document.createInstance('com.sun.star.text.GraphicObject');
        If IdNumber = '123' Then
            Graphic.GraphicURL := 'file:///C:/path/to/my_image123.jpg'
        Else
            Graphic.GraphicURL := 'file:///C:/path/to/my_image456.jpg';
        Graphic.AnchorType := 1; {com.sun.star.text.TextContentAnchorType.AS_CHARACTER;}
        Graphic.Width := 6000;
        Graphic.Height := 8000;
        TextCursor.gotoRange(Found, False);
        Txt.insertTextContent(TextCursor, Graphic, False);
        Found := Document.findNext(Found.getEnd, SearchDescriptor);
    end;
    

    EDIT 2:

    Embedding is explained in the next section of Andrew's document, Listing 5.26.

    Bitmaps := Document.createInstance('com.sun.star.drawing.BitmapTable');
    While...
        If IdNumber = '123' Then begin
            Bitmaps.insertByName('123Jpg', 'file:///C:/OurDocs/test_img123.jpg');
            Graphic.GraphicURL := Bitmaps.getByName('123Jpg');
        end Else begin
            Bitmaps.insertByName('456Jpg', 'file:///C:/OurDocs/test_img456.jpg');
            Graphic.GraphicURL := Bitmaps.getByName('456Jpg');
        end;
    
    0 讨论(0)
提交回复
热议问题