Back to Devexpress

How to: Save and Restore a Document's Watermark in Print Preview

windowsforms-7347-controls-and-libraries-printing-exporting-examples-miscellaneous-how-to-save-and-restore-a-documents-watermark-in-print-preview.md

latest4.7 KB
Original Source

How to: Save and Restore a Document's Watermark in Print Preview

  • Nov 15, 2023
  • 5 minutes to read

This tutorial demonstrates how to save and load a document’s watermark, which is specified in Print Preview, to/from an XML file or memory stream.

To save and load a document’s watermark, do the following.

  1. In Visual Studio, create a new Windows Forms Application.

  2. From the DX.25.2: Data & Analytics Toolbox tab, drop a GridControl onto a form, and bind it to a data source. A report document will be generated based on it later in code.

  3. To add a print preview to your application, switch to the application’s main form in Visual Studio, and press CTRL+ALT+X to run the Toolbox. Expand the DX.25.2: Reporting category and drop the DocumentViewer control onto the form.

  4. Click the smart tag of the control, and in the invoked actions list, select a toolbar that matches the user interface style of the rest of your application. In this tutorial, the ribbon toolbar is preferred over the minimal bar interface.

  5. To assign a document source to the control, click its smart tag again. In the drop-down menu of the DocumentViewer.DocumentSource property, expand the Standard Sources category and select PrintingSystem.

  6. Click the Printing System’s smart tag and click ellipsis button for the PrintingSystem.Links property. In the invoked dialog, add a new PrintableComponentLink instance. Set its PrintableComponentLinkBase.Component property to gridControl1.

  7. Next, from the DX.25.2: Common Controls Toolbox tab, drop the SimpleButton and RadioGroup controls onto the form.

  8. Now, let’s declare two storages (corresponding to a file and stream), and write two private methods ( SaveWatermark and RestoreWatermark ), which save/restore a PrintingSystem‘s watermark (the PrintingSystemBase.Watermarks property value) to/from these storages, using the Watermark.SaveToXml and Watermark.SaveToStream methods, correspondingly.

  9. Now, assign an appropriate storage to each RadioGroup’s item.

  10. In the form’s Load event handler, specify options for the default watermark, and save them to all available storages.

  11. Now, handle the Click event of the simpleButton1 , to call the RestoreWatermark method, create a report document (by calling the PrintableComponentLink.CreateDocument method) and handle the FormClosed event of the PrintingSystem.PreviewFormEx.

  12. Lastly, write the FormClosed event handler for the Preview form, where the watermark is saved to the specified storage.

Run the application. Choose a storage using the radio buttons, and click Show Preview. Then, in the invoked Print Preview form, access and specify the document’s watermark options (by clicking the Watermark toolbar button, or by choosing Watermark… entry in the Background menu).

In the invoked Watermark dialog, customize the default watermark, and click OK. Now, close the Print Preview form, and click the Show Preview button again, to see that the changes being made to the watermark are still in effect.

See Also

How to: Add a Watermark to a Document

How to: Add Different Watermarks to Document Pages