2005-6-23 [email protected]
技術原理
通過在TppReport的BeforePrint事件中對TPrinterDevice的PageSetting屬性和PageList屬性進行賦值
為何要如此處理?
根據分析ReportBuilder源碼,我們得到如下的調用順序TppViewer -> TppProducer -> TppPrinterDevice -> TppPageRequest -> TppPublisher,其中 TppPageRequest 封裝了打印頁范圍信息,而TppPrinterDevice 負責將指定的頁發送到PrinterCanvas
實現步驟
1、在調用單元聲明一個類私有變量,用於保存打印頁范圍
type
...
private
sPageRange: string;
...
end;
2、在調用單元聲明一個類私有過程,用於處理TppReport.BeforePrint事件
procedure TfrmMain.ppReportBeforePrint(Sender: TObject);
begin
if Sender is TppReport then
if (Sender as TppReport).PrinterDevice <> nil then
begin
(Sender as TppReport).PrinterDevice.PageSetting := psPageList;
ppTextToPageList(sPageRange, (Sender as TppReport).PrinterDevice.PageList, True);
end;
end;
3、在打印之前設置打印頁范圍,將TppReport.BeforePrint引導到自定義過程
sPageRange := '3-5';
(ppViewer1.Report as TppReport).BeforePrint := ppReportBeforePrint;
(ppViewer1.Report as TppReport).ShowPrintDialog := False;
ppViewer1.Print;
注意事項
1,如果找不到TppReport類別,在接口引用單元添加ppReport單元
2,如果找不到psPageList類別,在接口引用單元添加ppTypes單元
3,如果找不到ppTextToPageList函數,在接口引用單元添加ppUtils單元
4,sPageRange可以定義三種類型的頁范圍
A:起止頁:'3-10'//連接線分隔
B:分隔頁:'3,5,7'//逗號分隔
C:單獨頁:'7'