2013-09-25 54 views
2

我正在使用OpenOffice Writer,並希望在程序控制下打印。但是,根據我生成的文檔類型,我不希望打印到默認打印機,而是將某些文檔導向某些打印機。如何選擇要使用OpenOffice oleautomation進行打印的打印機?

使用Bernard Marcelly's OOoTools庫,德爾福,做OLE自動化,談論到OpenOffice 4.0,下面的代碼工作打印到當前的打印機,

procedure TMyOODocClass.Print; 
var 
    docObj : variant; // Current OOo Document, implements IXPrintable 
    printProps : variant; 
begin 
    docObj := GetMyActiveDocument; // method not shown, pretty standard stuff. 
    try 
    // empty array, I think this is where I would fill in PrinterName? 
    printProps := VarArrayCreate([0, -1], varVariant); 
    docObj.print(printProps); 
    except 
    on E:EOleException do 
    begin 
     raise Exception.Create('OpenOffice Document Print failed. '+E.Message); 
    end; 
    end; 
end; 

我無法找到OpenOffice的文檔Writer文檔打印方式或支持,我想我應該定義一些屬性的屬性,像這樣:

printProps := VarArrayCreate([0, 1], varVariant); 
printProps[0] := MakePropertyValue('PrinterName', 'PrinterNameHere') ; 

問題A部分,是有中所有屬性的打印徹底HTML在線參考,和所有其他類似的文檔方法接受?而B部分,是什麼性質或技術來設置上述。我相信OO中的Document對象實現了一個名爲IXPrintable的接口,所以我想知道如何找到IXPrintable的所有方法,以及該方法中的Print方法接受哪些參數或屬性。

更新繼意見建議,我嘗試使用了一個名爲「名稱」屬性,像這樣:

procedure TMyOODocClass.PrintTo(PrinterName:String); 
var 
    docObj : variant; // Current OOo Document, implements IXPrintable 
    printProps : variant; 
begin 
    docObj := GetMyActiveDocument; // method not shown, pretty standard stuff. 
    try 
    if PrinterName='' then 
     printProps := dummyArray 
    else 
    begin 
     printProps := VarArrayCreate([0, 1], varVariant); 
     printProps[0] := MakePropertyValue('Name',PrinterName); 
    end; 
    docObj.print(printProps); 
    except 
    on E:EOleException do 
    begin 
     raise EOOoError.Create('OpenOffice Document Print failed. '+E.Message); 
    end; 
    end; 
end; 

以上不工作,所以必須有東西丟失或錯誤。我也嘗試調用docObj.SetPrinter,但是我得到一個參數類型不匹配錯誤。

+1

也許['這example'(http://wiki.openoffice.org/wiki/API/Samples/Java/Office/DocumentHandling#DocumentPrinter)可能會有所幫助。 – TLama

+0

謝謝,這個例子很有用,但我很難將它翻譯成Delphi-sprache。 :-) –

回答

3

好吧我得到它的工作,問題是我沒有正確創建屬性值。另外我愚蠢地認爲你將參數傳遞給Print時,你所要做的是調用SetPrinter,將屬性Name設置爲打印機名稱,然後調用Print,仍然沒有參數。由TLama鏈接的網址清楚地表明瞭這一點,但我最初錯過了它,我想我需要更多的咖啡。

另外它似乎Unicode VarType 258(varUString)值不是特別的OLE自動化友好的,所以我明確在下面的代碼中使用AnsiString

uses 
     ComObj, 
     Classes, 
     SysUtils, 
     Dialogs, 
     Controls, 
     Windows, 
     oOoConstants, 
     OOoTools, 
     DB, 
     Variants, 
     StdCtrls, 
     Forms; 

procedure TMyOODocClass.PrintTo(PrinterName:AnsiString); 
var 
    docObj : variant; // Current OOo Document, implements IXPrintable 
    emptyProps, printProps: variant; 
    propName:AnsiString; 
begin 
    docObj := GetMyActiveDocument; // method not shown, pretty standard stuff. 
    try 
    emptyProps := dummyArray; 
    if PrinterName <> '' then 
    begin 
     propName := 'Name'; 
     printProps := createProperties([propName,PrinterName] ); // OOTools helper 
     docObj.SetPrinter(printProps); 
    end; 
    docObj.print(emptyProps); 
    except 
    on E:EOleException do 
    begin 
     raise EOOoError.Create('OpenOffice Document Print failed. '+E.Message); 
    end; 
    end; 
end; 

編譯並運行了一個完整的演示是到位桶這裏delphi_openoffice_demo01

+0

是的,ANSI字符串用於屬性名稱是正確的。無論如何,你可以使用該助手的'MakePropertyValue'函數來創建一個單獨的屬性(該包裝看起來是用於Delphi的ANSI版本,但是對於某些函數參數,可能會明確使用'AnsiString')。 – TLama

+0

'createProperties'調用似乎工作正常,並且有一些安全檢查內容。 –

+0

'createProperties'只是因爲它需要'Variant'數組作爲輸入,它必須是'[AnsiString,Variant]'類型的對。對於這種情況,它會創建不必要的變量數組,因爲對於['setPrinter'](http://www.openoffice.org/api/docs/common/ref/com/sun/star/view/XPrintable.html#setPrinter)方法調用你只使用一個參數。並且爲了使一個參數是來自該包裝器的'MakePropertyValue'功能,或者例如我的['CreateProperty'](http://stackoverflow.com/a/7819626/960757)嵌套函數。它們是相同的(假設包裝是ANSI Delphi)。 – TLama

相關問題