VCL Chart Print Functionality
- 5 minutes to read
The Chart control relies on the ExpressPrinting System to print content and export it to PDF. To be able to print chart content in your application, you need to add a TdxComponentPrinter component and create a report link for the Chart control.
Print and Export Chart APIs
You can use the following main API members to print and export chart content when a report link connects a Chart control and a component printer:
Report Link API Member[1] | Component Printer API Member[1] | Description |
---|---|---|
Preview | Preview | Invoke the Print Preview dialog for the source Chart control. |
PageSetup | PageSetup | Invoke the Page Setup dialog. |
Invoke the Print dialog or print chart content as is without user interaction. | ||
PrintEx | PrintEx | Like Print , these methods print chart content but accept additional parameters, such as the number of copies. |
ExportToPDF | ExportToPDF | Invoke the PDF Export Options dialog or export chart content to a PDF document without user interaction. |
Tip
Refer to TdxChartControlReportLink and TdxComponentPrinter class descriptions for detailed information on all available options.
Create a Report Link at Design Time
Double-click the TdxComponentPrinter to invoke the Report Links editor dialog.
This dialog allows you to manage all report links in your application. Click the Add… button to invoke the Add Link dialog.
The Available Source(s) box lists all controls for which you can create a report link. Select the target Chart control and click the OK button to create a report link (a TdxChartControlReportLink class instance) for the control in the TdxComponentPrinter component.
Close the Report Links dialog. Now you can use report link and component printer APIs to print Chart control content and invoke the Print Preview dialog for the control.
Code Example: Print Content
The following code example creates a report link for an existing Chart control, prints its content without user interaction, and deletes the created report link:
var
AReportLink: TBasedxReportLink;
begin
// Creates a Chart control report link
AReportLink := dxComponentPrinter1.AddEmptyLinkEx(TdxChartControlReportLink, dxChartControl1);
try
AReportLink.Print(False); // Prints chart content without user interaction
finally
dxComponentPrinter1.DeleteLink(dxComponentPrinter1.LinkCount - 1); // Deletes the report link after export
end;
end;
Code Example: Export to PDF
The following code example creates a report link for an existing Chart control, configures report link settings, exports control content to a password-protected PDF document with a digital signature without user interaction, and deletes the created report link:
uses
// ...
dxX509Certificate, dxPSPDFExportCore;
// ...
var
AExportSettings: TdxPSPDFReportExportOptions;
AReportLink: TBasedxReportLink;
ACertificate: TdxX509Certificate;
begin
// Creates a Chart control report link
AReportLink := dxComponentPrinter1.AddEmptyLinkEx(TdxChartControlReportLink, dxChartControl1);
AExportSettings := TdxPSPDFReportExportOptions.Create; // Creates default export settings
// Loads an X.509 certificate required to create a digital signature
ACertificate := TdxX509Certificate.Create('123.pfx', '123456');
try
// Configures PDF export settings
AExportSettings.Author := 'DevExpress VCL';
AExportSettings.Keywords := 'PDF, Chart, VCL, DevExpress';
AExportSettings.CompressStreams := True;
// Enables password protection
AExportSettings.SecurityOptions.UserPassword := '123';
AExportSettings.SecurityOptions.Enabled := True;
// Adds a digital signature to the exported document
AExportSettings.SignatureOptions.Reason := 'Approved';
AExportSettings.SignatureOptions.Certificate := ACertificate;
AExportSettings.SignatureOptions.Enabled := True;
// Exports chart content to a PDF file in the application directory without user interaction
AReportLink.ExportToPDF('output.pdf', False, ASettings);
finally
dxComponentPrinter1.DeleteLink(dxComponentPrinter1.LinkCount - 1); // Deletes the report link after export
AExportSettings.Free; // Destroys the created export settings instance to free up reserved memory
ACertificate.Free; // Releases the X.509 certificate used to create a digital signature
end;
end;
Related Compiled Demo
To see the Chart print functionality in action, run the Chart Control demo in the VCL Demo Center installed with compiled VCL DevExpress demos. Click the Print or Print Preview button to invoke the corresponding dialog.
Tip
You can find full source code for the installed compiled Chart control demo in the following folder:
%PUBLIC%\Documents\DevExpress VCL Demos\MegaDemos\Product Demos\ExpressChart
-
You can use corresponding API members of the TdxComponentPrinter and TdxChartControlReportLink components interchangeably if the report link for the source Chart control is set as current.