Skip to main content
A newer version of this page is available. .

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

  • 5 minutes to read

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

Tip

A complete sample project is available in the DevExpress Code Examples database at http://www.devexpress.com/example=E1909.

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.19.1: 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.19.1: Reporting category and drop the DocumentViewer control onto the form.

    HowTo - ShowPreviewOnForm2.png

  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.

    document-viewer-create-ribbon-toolbar

  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.

    document-viewer-select-document-source-standard-printing-system

  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.

    printing-system-links-printable-component-link-assign-grid-control

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

    Select the radioGroup1, and in the Properties window, expand its RadioGroup.Properties, and click the ellipsis button for the RepositoryItemRadioGroup.Items property.

    HowTo_SaveRestoreWatermarks2

    In the invoked dialog, create three items with the following RadioGroupItem.Description property values.

    • Save/Load from Registry

    • Save/Load from XML

    • Save/Load from Stream

    For the radioGroup1, you may also wish to set the RepositoryItemRadioGroup.Columns property to 3, to better position its items within the form.

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

    
    using System;
    using System.IO;
    using System.Windows.Forms;
    using DevExpress.XtraPrinting;
    using DevExpress.XtraPrinting.Drawing;
    // ...
    
    private enum WatermarkStorage { Registry, XML, Stream };
    private string registryPath = @"HKEY_CURRENT_USER\Software\MyCompany\MyTool\";
    private string xmlFile = "test.xml";
    private MemoryStream stream = new MemoryStream();
    
    private void SaveWatermark(PrintableComponentLink pcl, WatermarkStorage storage) {
        switch (storage) {
            case WatermarkStorage.Registry: {
                    pcl.PrintingSystem.Watermark.SaveToRegistry(registryPath);
                    break;
                }
            case WatermarkStorage.XML: {
                    pcl.PrintingSystem.Watermark.SaveToXml(xmlFile);
                    break;
                }
            case WatermarkStorage.Stream: {
                    pcl.PrintingSystem.Watermark.SaveToStream(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    break;
                }
        }
    }
    
    private void RestoreWatermark(PrintableComponentLink pcl, WatermarkStorage storage) {
        switch (storage) {
            case WatermarkStorage.Registry: {
                    pcl.PrintingSystem.Watermark.RestoreFromRegistry(registryPath);
                    break;
                }
            case WatermarkStorage.XML: {
                    if (File.Exists(xmlFile)) {
                        pcl.PrintingSystem.Watermark.RestoreFromXml(xmlFile);
                    }
                    break;
                }
            case WatermarkStorage.Stream: {
                    pcl.PrintingSystem.Watermark.RestoreFromStream(stream);
                    stream.Seek(0, SeekOrigin.Begin);
                    break;
                }
        }
    }
    
  9. Now, assign an appropriate storage to each RadioGroup’s item.

    
    private WatermarkStorage GetStorage() {
        WatermarkStorage storage = WatermarkStorage.Registry;
    
        switch (radioGroup1.SelectedIndex) {
            case 0: {
                    storage = WatermarkStorage.Registry;
                    break;
                }
            case 1: {
                    storage = WatermarkStorage.XML;
                    break;
                }
            case 2: {
                    storage = WatermarkStorage.Stream;
                    break;
                }
        }
    
        return storage;
    }
    
  10. In the form’s Load event handler, specify options for the default watermark, and save them to all available storages.

    
    private void Form1_Load(object sender, EventArgs e) {
        this.productsTableAdapter.Fill(this.nwindDataSet.Products);
    
        radioGroup1.SelectedIndex = 0;
    
        Watermark wm = printableComponentLink1.PrintingSystem.Watermark;
        wm.Text = "Change the Watermark,\r\nthen close and re-open\r\nthe form.";
        wm.ShowBehind = false;
        wm.TextDirection = DirectionMode.Horizontal;
    
        SaveWatermark(printableComponentLink1, WatermarkStorage.Registry);
        SaveWatermark(printableComponentLink1, WatermarkStorage.Stream);
        SaveWatermark(printableComponentLink1, WatermarkStorage.XML);
    }
    
  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.

    Finally, call the Link.ShowPreview method.

    
    private void btnShowPreview_Click(object sender, EventArgs e) {
        RestoreWatermark(printableComponentLink1, GetStorage());
    
        printableComponentLink1.CreateDocument();
    
        printableComponentLink1.PrintingSystem.PreviewFormEx.FormClosed +=
            new FormClosedEventHandler(PreviewFormEx_FormClosed);
    
        printableComponentLink1.ShowPreview();
    }
    
  12. Lastly, write the FormClosed event handler for the Preview form, where the watermark is saved to the specified storage.

    
    void PreviewFormEx_FormClosed(object sender, FormClosedEventArgs e) {
        SaveWatermark(printableComponentLink1, GetStorage());
    }
    

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 PrintPreviewBar.Watermark toolbar button, or by choosing Watermark… entry in the Background menu).

Watermarks_SaveRestore

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