2017-09-13 38 views
2

我使用搜索的 How to Search and Replace in odt Open Office document? 接受的解决方案中提到的方法在OpenOffice文档中插入图像,并用Delphi如何用Delphi

现在我的要求ODT文档中替换文本与图像替换文本。 例如,我的odt文件将标记为“SHOW_CHART = ID”,我将从数据库检索图表作为图像文件的给定ID,然后将其替换为“SHOW_CHART = ID”。

所以我的问题是如何从一个文件插入图像到ODT文件。 我发现另一个链接提出相同的问题,但使用java。 How to insert an image in to an openoffice writer document with java? 但我不知道java。

+0

似乎这里一个不完整的企图https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=74202 – LaBracca

回答

4

下面的代码改编自Andrew Pitonyak's Macro Document的代码5.24。

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); 

使用OpenOffice的帕斯卡的更多信息,在https://www.freepascal.org/~michael/articles/openoffice1/openoffice.pdf

EDIT

此代码插入SHOW_CHART=123SHOW_CHART=456,例如,然后它找到这些字符串并用相应的图像替换它们。

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。

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; 
+1

感谢提供样品@Jim K,它的工作以及用于插入图像在文档中。我如何在文档中的所需位置插入图像? – Girish

+1

查看编辑答案。 –

+0

非常感谢! @Jim k – Girish