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.

1

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

6 Comments

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?
Thanks a lot! @Jim k
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?
hi @Jim K I get Error "com.sun.star.uno.RuntimeException" when it try to replace text with image in Document Header.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.