2017-09-13 104 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