PrintDocument for WinForms
PrintPreview
ベストプラクティス > PrintPreview

Some of the best practices to follow while working with the components and controls in the PrintPreview library  such as PrintPreviewControl, PreviewPane, PreviewThumbnailView, PreviewOutlineView, PreviewTextSearchPanel and PrintPreviewDialog are listed below:

Tip 1: Adding a Custom Toolbar Button to PrintPreviewControl

You may want to customize a toolbar in the PrintPreviewControl control by adding a custom button. To add your own toolbar button to any of the built-in toolbars on the PrintPreviewControl, add a PrintPreviewControl to the form, and complete the following steps:

  1. Click once on the PrintPreview control on the form to select it, and navigate to the Properties window.
  2. In the Properties window, locate and expand the Toolbars top-level property by clicking on the plus sign (+) to the left of the item. You should see the list of predefined preview toolbars: File, Navigation, Page, Text, and Zoom.
  3. Select and expand the toolbar, for instance File, that will contain the new item. The expanded list should show the predefined File buttons: Open, PageSetup, Print, Reflow, Save, and a ToolStrip expandable item.
  4. Select and expand the ToolStrip item. The expanded list should contain a single Items collection node.
  5. Select the Items node and click on the ellipses (...) button to the right of the item. The items collection editor dialog box containing the predefined items will be displayed.
  6. Add a new button using the collection dialog box's commands, adjust its properties as needed, note the name of the button, and press OK to close the dialog box and save the changes. Now you should see the newly added button in the designer view.
  7. Select the newly added button in the Properties window (you can do this by either clicking on the button in the designer view, or by selecting the button by its name from the Properties window's drop-down list).

Once you have added the toolbar button, you can customize its actions. You can now add a Click event handler to the button or further customize its properties.

Tip 2: Change the Default Toolbar Button Processing

To change the default processing of a built-in toolbar button on a PrintPreviewControl you need to handle the preview pane's UserAction event.

Add the PrintPreviewControl to the form, and complete the following steps:

  1. Click on the preview pane within that control (the main area showing the pages of the viewed document) in the Visual Studio designer.
  2. This will select the C1PreviewPane within the preview control into the properties window. For example, if your preview control has the name c1PrintPreviewControl1, the selected object's name should become c1PrintPreviewControl1.PreviewPane.
  3. Click the lightning bolt icon in the Properties window to view Events, scroll to the UserAction event item, and double click the item to create an event handler.
  4. You can customize the default processing of a built-in toolbar button in the UserAction event handler.

The handler receives an instance of UserActionEventArgs which contains two interesting properties: PreviewAction and Cancel. PreviewAction is an enum listing all supported user events, such as file open, print, and so on. Testing that property you may find the action you're interested in. The other important property of UserActionEventArgs is Cancel - if you add your own processing to an action and want to omit the standard processing, set UserActionEventArgs.Cancel to True, otherwise standard processing will take place after your event handler returns.

Tip 3: Using Preview Pane's PropertyChanged Event

To monitor interesting preview related properties, use the PropertyChanged event on the C1PreviewPane object within the C1PrintPreviewControl. All of the preview pane's own properties (not inherited from its base control) fire the PropertyChanged event when their values change.

For instance, the following code will add a handler to the PropertyChanged event provided that your form includes the PrintPreviewControl:

C#
コードのコピー
this.c1PrintPreviewControl1.PreviewPane.PropertyChanged += new PropertyChangedEventHandler(PreviewPane_PropertyChanged);

The following property changed event handler will print a debug line each time the current page changes in the preview for whatever reason:

C#
コードのコピー
void PreviewPane_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{   
  if (e.PropertyName == "StartPageIdx")   
    Debug.WriteLine("Current page changed to " + this.c1PrintPreviewControl1.PreviewPane.StartPageIdx.ToString());   
}