如何使用Delphi在OpenOffice文档中插入图像 [英] How to insert image in OpenOffice Document using Delphi

查看:265
本文介绍了如何使用Delphi在OpenOffice文档中插入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用公认的解决方案中提到的方法 如何在odt Open Office文档中进行搜索和替换?/a> Delphi搜索和替换odt文档中的文本

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

现在我的要求是将文字替换为图片. 例如,我的odt文件将具有"SHOW_CHART = ID"标签,我将从数据库中获取给定ID的图表作为图像文件,然后将其替换为"SHOW_CHART = ID".

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".

所以我的问题是如何将图像从文件插入到ODT文档中. 我发现另一个链接在问同样的问题,但使用的是Java. 如何在使用Java打开Openoffice作者文档? 但我不懂Java.

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.

推荐答案

以下代码改编自有关在Pascal上使用OpenOffice的更多信息,请参见 https://www .freepascal.org/〜michael/articles/openoffice1/openoffice.pdf .

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

编辑:

此代码将插入SHOW_CHART=123SHOW_CHART=456作为示例.然后,找到这些字符串并将其替换为相应的图像.

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;

编辑2 :

嵌入式的解释在安德鲁文档的下一部分,代码清单5.26.

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;

这篇关于如何使用Delphi在OpenOffice文档中插入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆