Print preview using the PageLayoutControl


PurposeThis sample demonstrates changing the printer and printer settings and applying these changes to the PageLayoutControl and its print preview. The sample uses the .NET OpenFileDialog, PageSetupDialog, PrintPreviewDialog, and PrintDialog to load a map document into the PageLayoutControl, change the printer settings, display a print preview, and print the IPageLayoutControl::Page, respectively. The terminology used is as follows:

▪ Print page—A page that is displayed in the PrintPreviewDialog or printed to a physical printer
▪ Device boundaries—The boundaries of the page the map document is printed to
▪ Visible boundaries—The area of the map document that fits on the print page
▪ Graphics device—Encapsulates a graphical device interface (GDI+) drawing surface, provides methods for drawing objects to the display device, is associated with a specific device context

After loading a map document into the PageLayoutControl using the OpenFileDialog, the user can select a printer and change the page settings using the PageSetupDialog. The PrintPreviewDialog displays the IPageLayoutControl::Page as print pages dependent upon the selected printer and the page settings. The key functionality is implemented in the document_PrintPage event handler, which is called when the PrintPreviewDialog appears.

First, the IPage::PageToPrinterMapping is set with the selected index from the combo box. The combo box allows the user to specify the way the IPageLayoutControl::Page is mapped onto the print pages. There are three different options:

▪ esriPageMappingTile—Tiles up the map document and prints every tile onto a separate print page
▪ esriPageMappingCrop—Prints the first tile only; there will be one print page only
▪ esriPageMappingScale—Scales the map document onto a single print page

Second, the device boundaries (devBounds) of the printer, as well as the visible boundaries (visBounds) of the map document, are obtained using the selected printer properties.

The IPageLayoutControl::Page is printed into the PrintPreviewDialog using the Output method of the PageLayoutControl::ActiveView. Apart from the graphics device's resolution (dpi), device boundaries (devBounds), visible boundaries (visBounds), and CancelTracker, the method gets passed a handle to the graphics device the map document is printed to. This graphics device handle is obtained by calling GetHdc on the graphics object that gets passed within the method's argument structure e. Calls to the GetHdc and ReleaseHdc methods must appear in pairs. If ReleaseHdc is not called, the application will fail with an exception.

If the map document is to be mapped to more than one page, the HasMorePages property of the document_PrintPage's argument e is set to true. This ensures that document_PrintPage will be called again. This will be repeated until the last page is printed to the PrintPreviewDialog. The document can then be printed using the PrintDialog.

Development licensing Deployment licensing
Engine Developer Kit Engine Runtime
ArcView
ArcEditor
ArcInfo

How to use

See Using the samples for help on compiling, setting up the debugger, and running the sample (either an exe or dll).

  1. Browse and select a map document to load into the PageLayoutControl.
  2. Select a printer and change the page settings in the Page Setup dialog box.
  3. Preview the print layout of the map in the Print Preview dialog box.
  4. Print the document using the Print dialog box.

AssemblyInfo.cs Assembly information
PrintPreview.cs Form file
Download the C# files
AssemblyInfo.vb Assembly information
PrintPreview.vb Form file
Download the VB.NET files

Download the files for all languages

See Also:

PageLayoutControl class
IPageLayoutControl interface