How to insert image in OpenOffice Document using Delphi

1.4k views Asked by At

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

now my requirement is replace text with image. for example my odt file will have tags as "SHOW_CHART=ID", i will retrieve chart from DB for given ID as an image file and then replace it with "SHOW_CHART=ID".

So my question is how to insert image from a file to ODT document. I found another link asking same question but using java. How to insert an image in to an openoffice writer document with java? but i don't know java.

1

There are 1 answers

6
Jim K On BEST ANSWER

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;