HowTo: Set the Margins and Landscape PageSettings for a PrintDocument
ID: W3298006
Background
This document describes how to use the .NET Framework PageSettings object to control the Margins and Landscape settings of any PrintDocument derived class.
Applies to :
- PrintDocument
- PrintForm
- PrintAdapters: PrintListView, PrintRichTextBox, PrintTreeView and PrintDataTable
- PrintChainManager
Adjusting properties of the PageSettings Object
Margin widths are controlled by the Margins property of the PageSettings object, and page orientation is controlled by the Landscape setting.
In the printing lifecycle of a PrintDocument there are two opportunities to set the PageSettings
- set the PrintDocument.DefaultPageSettings properties to control the default margin size etc of all pages printed by the PrintDocument, or
- set e.PageSettings.Margins in the PrintDocument.QueryPageSettings event to set each printed page's settings individually
Setting the default size for all pages in the form Load event via the DefaultPageSettings property:
VB.NET Sample, showing setting the Margins and Landscape for a PrintForm PrintDocument:
Private Sub frmSimpleForm_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Dim newMargins As System.Drawing.Printing.Margins newMargins = New System.Drawing.Printing.Margins(20, 20, 20, 20) Me.PrintForm1.DefaultPageSettings.Margins = newMargins Me.PrintForm1.DefaultPageSettings.Landscape = True End Sub
Setting each page's Margin size individually via the QueryPageSettingsEventArgs.PageSettings property in the QueryPageSettings event:
VB.NET Sample, showing setting the Margins and Landscape for a PrintForm PrintDocument in the QueryPageSettings event:
Private Sub PrintForm1_QueryPageSettings(ByVal sender As Object, _ ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles PrintForm1.QueryPageSettings Dim newMargins As System.Drawing.Printing.Margins newMargins = New System.Drawing.Printing.Margins(20, 20, 20, 20) e.PageSettings.Margins = newMargins e.PageSettings.Landscape = True End Sub
Last updated
04-Aug-2005
|