3

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.

Girish
  • 323
  • 3
  • 10
  • There seems an incomplete attempt here https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=74202 – UnDiUdin Sep 13 '17 at 09:43

1 Answers1

7

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;
Jim K
  • 12,824
  • 2
  • 22
  • 51
  • 1
    Thanks @Jim k for providing sample, it worked well for inserting a image in document. how can i insert an image at desired location in the document? – Girish Sep 14 '17 at 07:05
  • Thanks a lot! @Jim k – Girish Sep 14 '17 at 12:00
  • 1
    hi @Jim k, Although i accepted your asnwer but i am having issue as follows: Inserted image always refer the local file, if i remove the local file the link is broken and on opening the odt document i loose the image , that i don't want, how to achieve that? – Girish Sep 15 '17 at 15:10
  • hi @Jim K I get Error "com.sun.star.uno.RuntimeException" when it try to replace text with image in Document Header. – Girish Feb 17 '19 at 15:41
  • The header is likely to be different. Ask a new question about the header and give example code that shows the problem. Also add a link to this post, mentioning that it is a follow-up or related question. – Jim K Feb 18 '19 at 20:03
  • Hello @Jim k i posted a new question https://stackoverflow.com/questions/57809463/how-to-search-text-tags-and-replace-with-image-in-header-footer-table-of-openoff – Girish Sep 05 '19 at 16:30