Here有一个将OpenOffice文件保存为PDF的示例。我正在使用类似的代码将Calc文件保存为PDF。 (这足以将属性writer_pdf_Export
更改为calc_pdf_Export
。)
不幸的是,代码没有考虑原始文件中定义的打印区域。
我该怎么做?
答案 0 :(得分:2)
扩展我以前的代码(虽然不是那么干净)我会尝试这样的事情(注意我还没有测试过)。第三个参数将导出的范围指定为TRect
变量:
procedure ExportCalcRangeToPDF(const ASourceFileURL, ATargetFileURL: string;
ASheetIndex: Integer; ARange: TRect);
var
CellRange: Variant;
StarOffice: Variant;
StarDesktop: Variant;
StarDocument: Variant;
FilterParams: Variant;
ExportParams: Variant;
ExportObject: Variant;
function CreateProperty(const AName: AnsiString; const 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);
CellRange := StarDocument.Sheets.getByIndex(ASheetIndex).getCellRangeByPosition(
ARange.Left, ARange.Top, ARange.Right, ARange.Bottom);
ExportParams := VarArrayCreate([0, 0], varVariant);
ExportParams[0] := CreateProperty('Selection', CellRange);
ExportObject := StarOffice.Bridge_GetValueObject;
ExportObject.Set('[]com.sun.star.beans.PropertyValue', ExportParams);
FilterParams := VarArrayCreate([0, 1], varVariant);
FilterParams[0] := CreateProperty('FilterName', AnsiString('calc_pdf_Export'));
FilterParams[1] := CreateProperty('FilterData', ExportObject);
StarDocument.StoreToURL(ATargetFileURL, FilterParams);
StarDocument.Close(True);
StarDesktop.Terminate;
StarDocument := Unassigned;
StarDesktop := Unassigned;
StarOffice := Unassigned;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ExportCalcRangeToPDF(
'file:///C:/SourceFile.ods',
'file:///C:/TargetFile.pdf',
0,
Rect(1, 1, 2, 2)
);
end;
答案 1 :(得分:1)
要获取电子表格的打印区域,我会这样做。 (当然可以改进)。
...
Sheet: Variant;
PrintAreas: Variant;
...
...
Sheet := StarDocument.Sheets.getByIndex(0); // get the first sheet
PrintAreas := Sheet.getPrintAreas; // get print areas
CellRange := Sheet.getCellRangeByPosition(PrintAreas[0].StartColumn,
PrintAreas[0].StartRow, PrintAreas[0].EndColumn,
PrintAreas[0].EndRow); // Get range of the first print area
...