Gir*_*sh 3 delphi openoffice-writer
我正在使用在odt Open Office文档中如何搜索和替换的公认解决方案中提到的方法 ? 使用Delphi搜索和替换odt文档中的文本
现在我的要求是用图像替换文本。例如,我的odt文件将具有“ SHOW_CHART = ID”标签,我将从数据库中获取给定ID的图表作为图像文件,然后将其替换为“ SHOW_CHART = ID”。
所以我的问题是如何将图像从文件插入到ODT文档中。我发现另一个链接问同样的问题,但使用Java。 如何使用Java将图像插入OpenOffice编写器文档中? 但我不懂Java。
下面的代码改编自Andrew Pitonyak的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);
Run Code Online (Sandbox Code Playgroud)
有关在Pascal上使用OpenOffice的更多信息,请访问https://www.freepascal.org/~michael/articles/openoffice1/openoffice.pdf。
编辑:
此代码插入SHOW_CHART=123并SHOW_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;
Run Code Online (Sandbox Code Playgroud)
编辑2:
在Andrew文档的下一部分,清单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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
621 次 |
| 最近记录: |