程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 調用openoffice Com組件轉換成PDF

調用openoffice Com組件轉換成PDF

編輯:Delphi

//例子 OpenOfficeExportToPDF('file:///C:/SourceFile.odt', 'file:///C:/TargetFile.pdf');   必須為URL格式的路徑

 

function urlconvertor(AFilePath: string): string; //本地路徑轉換成URL相對路徑
begin
  result := 'file:///' + AnsiReplaceStr(AFilePath, '\', '/');
end;

 

procedure OpenOfficeExportToPDF(const ASourceFileURL: string;
  const ATargetFileURL: string);
var
  StarOffice: Variant;
  StarDesktop: Variant;
  StarDocument: Variant;
  FilterParams: Variant;
  ExportParams: Variant;
  ExportObject: Variant;

  function CreateProperty(const AName: AnsiString; AValue: Variant): Variant;
  begin
    Result := StarOffice.Bridge_GetStruct('com.sun.star.beans.PropertyValue');
    Result.Name := AName;
    Result.Value := AValue;
  end;

begin

  StarOffice := CreateOleObject('com.sun.star.ServiceManager');
  StarDesktop := StarOffice.CreateInstance('com.sun.star.frame.Desktop');

  FilterParams := VarArrayCreate([0, 0], varVariant);
  FilterParams[0] := CreateProperty('Hidden', True);

  StarDocument := StarDesktop.LoadComponentFromURL(ASourceFileURL, '_blank', 0,
    FilterParams);

  ExportParams := VarArrayCreate([0, 3], varVariant);
  ExportParams[0] := CreateProperty('Magnification', 2);
  ExportParams[1] := CreateProperty('EncryptFile', True);
  //  ExportParams[2] := CreateProperty('DocumentOpenPassword', AnsiString('StackOverflow'));
  ExportParams[3] := CreateProperty('HideViewerWindowControls', True);

  ExportObject := StarOffice.Bridge_GetValueObject;
  ExportObject.set('[]com.sun.star.beans.PropertyValue', ExportParams);

  FilterParams := VarArrayCreate([0, 1], varVariant);
  FilterParams[0] := CreateProperty('FilterName',
    AnsiString('writer_pdf_Export'));
  FilterParams[1] := CreateProperty('FilterData', ExportObject);

  StarDocument.StoreToURL(ATargetFileURL, FilterParams);

  StarDocument.Close(True);
  StarDesktop.Terminate;

  StarDocument := Unassigned;
  StarDesktop := Unassigned;
  StarOffice := Unassigned;

end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved