简体   繁体   English

如何使用Delphi和com界面更新ODT文档中的“目录”

[英]How to update the “table of contents” in an ODT document with Delphi and the com interface

at first to the background what I try to achieve. 首先要了解我要实现的背景。 I am basically creating a report and depending on the user selecting export it to different formats (odt, doc & pdf). 我基本上是在创建报告,并根据选择的用户将其导出为不同的格式(odt,doc和pdf)。

So my approach is that I generate the whole document in the open document format odt (Which is very neat after you worked your way through the specification and XML), then I use the openoffice com interfaces to open the document programmatically and save it in word or in pdf. 所以我的方法是,我以odt的开放文档格式生成整个文档(在您遍历规范和XML之后这很整齐),然后我使用openoffice com界面以编程方式打开文档并将其保存为Word或pdf。

This works perfect so far but I have the problem that the table of content doesn't get updated. 到目前为止,这是完美的,但是我的问题是目录没有更新。

It doesn't matter so much in the DOC format, because the user can do it manually afterwards, but in PDF the user doesn't get this option. DOC格式无关紧要,因为用户之后可以手动进行操作,但是在PDF中,用户没有此选项。

I recorded the macro of the TOC update and tried to use it but somehow it doesn't work. 我记录了TOC更新的宏并尝试使用它,但是由于某种原因它不起作用。 I doesn't give me an error message but it just doesn't fire.. Below is the Makro: 我没有给我错误消息,但没有触发。.下面是万客隆:

sub Main

  dim document   as object
  dim dispatcher as object
  document   = ThisComponent.CurrentController.Frame
  dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
  dispatcher.executeDispatch(document, ".uno:UpdateCurIndex", "", 0, Array())

end sub

Basically I use this to create it: 基本上,我用它来创建它:

oDispatcher := fOpenOffice.createInstance('com.sun.star.frame.DispatchHelper');

oDispatcher.executeDispatch(fDocument.CurrentController.Frame
                          , '.uno:UpdateCurIndex', '', 0
                          , VarArrayCreate([0, 0], varVariant));

with those variants: 这些变体:

fOpenOffice := CreateOleObject('com.sun.star.ServiceManager');
wProperties := VarArrayCreate([0, 0], varVariant);
wProperties[0] := MakePropertyValue('Hidden', True);
fDocument := fDesktop.loadComponentFromURL('file:///' + FileName
               , '_blank', 0, wProperties);`

Is there anything I forgot in the process? 我在此过程中忘记了什么? I haven't listed the whole source code which is pretty standard and works perfect. 我没有列出完整的源代码,这是相当标准的并且可以正常工作。 It is just those two lines with the "oDispatcher" which don't do the job. 只是带有“ oDispatcher”的这两行不起作用。

Have you tried a autoupdate in a event ? 您是否尝试过在事件中进行自动更新?

sub OnOpenDocumentUpdateAllDocumentIndexes
oIndexes = ThisComponent.getDocumentIndexes()

for i = 0 to oIndexes.getCount () - 1
 oIndexes (i).update
next i
end sub 

Ok, now I found out about the problem and come up with an work around! 好的,现在我发现了问题所在,并提出了解决方案! 1. The update of the Table of Contents only works when the documnet is opened NOT hidden! 1.目录的更新仅在打开documnet而不隐藏时才起作用! So I had to change my code to: 所以我不得不将代码更改为:

wProperties[0] := MakePropertyValue('Hidden', False);
  1. I used a very simple and efficient work around by adding a global Makro to OpenOffice which will automatically execute my Makro when a document is opened. 我通过在OpenOffice中添加一个全局Makro来进行非常简单而有效的工作,当打开文档时,它将自动执行我的Makro。 All the Makro is doing is to look the text "Content", move one line down and update the selected Table of content. Makro所做的全部工作就是查找文本“ Content”,向下移动一行并更新选定的目录。 But this only works when openOffice ist started visible to he user. 但这仅在openOffice ist开始对用户可见时有效。 Otherwise it doesn't work. 否则它将无法正常工作。 The script for updating it is below: 用于更新它的脚本如下:

     sub UpdateTOC dim document as object dim dispatcher as object document = ThisComponent.CurrentController.Frame sub UpdateTOC dim document as object dim dispatcher as object document = ThisComponent.CurrentController.Frame dispatcher = createUnoService("com.sun.star.frame.DispatchHelper") dim args1(18) as new com.sun.star.beans.PropertyValue args1(0).Name = "SearchItem.StyleFamily" args1(0).Value = 2 args1(1).Name = "SearchItem.CellType" args1(1).Value = 0 args1(2).Name = "SearchItem.RowDirection" args1(2).Value = true args1(3).Name = "SearchItem.AllTables" args1(3).Value = false args1(4).Name = "SearchItem.Backward" args1(4).Value = false args1(5).Name = "SearchItem.Pattern" args1(5).Value = false args1(6).Name = "SearchItem.Content" args1(6).Value = false args1(7).Name = "SearchItem.AsianOptions" args1(7).Value = false args1(8).Name = "SearchItem.AlgorithmType" args1(8).Value = 0 args1(9).Name = "SearchItem.SearchFlags" args1(9).Value = 65536 args1(10).Name = "SearchItem.SearchString" args1(10).Value = "Contents" args1(11).Name = "SearchItem.ReplaceString" args1(11).Value = "" args1(12).Name = "SearchItem.Locale" args1(12).Value = 255 args1(13).Name = "SearchItem.ChangedChars" args1(13).Value = 2 args1(14).Name = "SearchItem.DeletedChars" args1(14).Value = 2 args1(15).Name = "SearchItem.InsertedChars" args1(15).Value = 2 args1(16).Name = "SearchItem.TransliterateFlags" args1(16).Value = 1024 args1(17).Name = "SearchItem.Command" args1(17).Value = 0 args1(18).Name = "Quiet" args1(18).Value = true dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1()) dim args2(1) as new com.sun.star.beans.PropertyValue args2(0).Name = "Count" args2(0).Value = 1 args2(1).Name = "Select" args2(1).Value = false dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args2()) dispatcher.executeDispatch(document, ".uno:UpdateCurIndex", "", 0, Array()) end sub 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM