About the Print preview using the PageLayoutControl Sample
[C#]
PrintPreview.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Drawing.Printing;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS;
namespace PrintPreview
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
//buttons for open file, print preview, print dialog, page setup
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.ComboBox comboBox1;
//declare the dialogs for print preview, print dialog, page setup
internal PrintPreviewDialog printPreviewDialog1;
internal PrintDialog printDialog1;
internal PageSetupDialog pageSetupDialog1;
//declare a PrintDocument object named document, to be displayed in the print preview
private System.Drawing.Printing.PrintDocument document = new System.Drawing.Printing.PrintDocument();
//cancel tracker which is passed to the output function when printing to the print preview
private ITrackCancel m_TrackCancel = new CancelTrackerClass();
private ESRI.ArcGIS.Controls.AxPageLayoutControl axPageLayoutControl1;
private ESRI.ArcGIS.Controls.AxLicenseControl axLicenseControl1;
//the page that is currently printed to the print preview
private short m_CurrentPrintPage;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
//Release COM objects
ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown();
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.axPageLayoutControl1 = new ESRI.ArcGIS.Controls.AxPageLayoutControl();
this.axLicenseControl1 = new ESRI.ArcGIS.Controls.AxLicenseControl();
((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 24);
this.button1.TabIndex = 1;
this.button1.Text = "Open File";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(112, 8);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(88, 24);
this.button2.TabIndex = 4;
this.button2.Text = "Page Setup";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(216, 8);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(88, 24);
this.button3.TabIndex = 5;
this.button3.Text = "Print Preview";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Location = new System.Drawing.Point(320, 8);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(88, 24);
this.button4.TabIndex = 6;
this.button4.Text = "Print";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(424, 8);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(144, 21);
this.comboBox1.TabIndex = 8;
//
// axPageLayoutControl1
//
this.axPageLayoutControl1.Location = new System.Drawing.Point(0, 48);
this.axPageLayoutControl1.Name = "axPageLayoutControl1";
this.axPageLayoutControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axPageLayoutControl1.OcxState")));
this.axPageLayoutControl1.Size = new System.Drawing.Size(680, 520);
this.axPageLayoutControl1.TabIndex = 9;
//
// axLicenseControl1
//
this.axLicenseControl1.Enabled = true;
this.axLicenseControl1.Location = new System.Drawing.Point(512, 88);
this.axLicenseControl1.Name = "axLicenseControl1";
this.axLicenseControl1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axLicenseControl1.OcxState")));
this.axLicenseControl1.Size = new System.Drawing.Size(32, 32);
this.axLicenseControl1.TabIndex = 10;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 566);
this.Controls.Add(this.axLicenseControl1);
this.Controls.Add(this.axPageLayoutControl1);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Print Preview / Print dialog Sample";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.axPageLayoutControl1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.axLicenseControl1)).EndInit();
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
if (!RuntimeManager.Bind(ProductCode.Engine))
{
if (!RuntimeManager.Bind(ProductCode.Desktop))
{
MessageBox.Show("Unable to bind to ArcGIS runtime. Application will be shut down.");
return;
}
}
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
InitializePrintPreviewDialog(); //initialize the print preview dialog
printDialog1 = new PrintDialog(); //create a print dialog object
InitializePageSetupDialog(); //initialize the page setup dialog
comboBox1.Items.Add("esriPageMappingTile");
comboBox1.Items.Add("esriPageMappingCrop");
comboBox1.Items.Add("esriPageMappingScale");
comboBox1.SelectedIndex= 0;
}
private void InitializePrintPreviewDialog()
{
// create a new PrintPreviewDialog using constructor
printPreviewDialog1 = new PrintPreviewDialog();
//set the size, location, name and the minimum size the dialog can be resized to
printPreviewDialog1.ClientSize = new System.Drawing.Size(800, 600);
printPreviewDialog1.Location = new System.Drawing.Point(29, 29);
printPreviewDialog1.Name = "PrintPreviewDialog1";
printPreviewDialog1.MinimumSize = new System.Drawing.Size(375, 250);
//set UseAntiAlias to true to allow the operating system to smooth fonts
printPreviewDialog1.UseAntiAlias = true;
//associate the event-handling method with the document's PrintPage event
this.document.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(document_PrintPage);
}
private void InitializePageSetupDialog()
{
//create a new PageSetupDialog using constructor
pageSetupDialog1 = new PageSetupDialog();
//initialize the dialog's PrinterSettings property to hold user defined printer settings
pageSetupDialog1.PageSettings = new System.Drawing.Printing.PageSettings();
//initialize dialog's PrinterSettings property to hold user set printer settings
pageSetupDialog1.PrinterSettings = new System.Drawing.Printing.PrinterSettings();
//do not show the network in the printer dialog
pageSetupDialog1.ShowNetwork = false;
}
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream;
//create an open file dialog
OpenFileDialog openFileDialog1 = new OpenFileDialog();
//set the file extension filter, filter index and restore directory flag
openFileDialog1.Filter = "template files (*.mxt)|*.mxt|mxd files (*.mxd)|*.mxd" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
//display open file dialog and check if user clicked ok button
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
//check if a file was selected
if((myStream = openFileDialog1.OpenFile())!= null)
{
//get the selected filename and path
string fileName = openFileDialog1.FileName;
//check if selected file is mxd file
if (axPageLayoutControl1.CheckMxFile(fileName))
{
//load the mxd file into PageLayout control
axPageLayoutControl1.LoadMxFile(fileName, "");
}
myStream.Close();
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
//Show the page setup dialog storing the result.
DialogResult result = pageSetupDialog1.ShowDialog();
//set the printer settings of the preview document to the selected printer settings
document.PrinterSettings = pageSetupDialog1.PrinterSettings;
//set the page settings of the preview document to the selected page settings
document.DefaultPageSettings = pageSetupDialog1.PageSettings;
//due to a bug in PageSetupDialog the PaperSize has to be set explicitly by iterating through the
//available PaperSizes in the PageSetupDialog finding the selected PaperSize
int i;
IEnumerator paperSizes = pageSetupDialog1.PrinterSettings.PaperSizes.GetEnumerator();
paperSizes.Reset();
for(i = 0; i<pageSetupDialog1.PrinterSettings.PaperSizes.Count; ++i)
{
paperSizes.MoveNext();
if(((PaperSize)paperSizes.Current).Kind == document.DefaultPageSettings.PaperSize.Kind)
{
document.DefaultPageSettings.PaperSize = ((PaperSize)paperSizes.Current);
}
}
/////////////////////////////////////////////////////////////
///initialize the current printer from the printer settings selected
///in the page setup dialog
/////////////////////////////////////////////////////////////
IPaper paper;
paper = new PaperClass(); //create a paper object
IPrinter printer;
printer = new EmfPrinterClass(); //create a printer object
//in this case an EMF printer, alternatively a PS printer could be used
//initialize the paper with the DEVMODE and DEVNAMES structures from the windows GDI
//these structures specify information about the initialization and environment of a printer as well as
//driver, device, and output port names for a printer
paper.Attach(pageSetupDialog1.PrinterSettings.GetHdevmode(pageSetupDialog1.PageSettings).ToInt32(), pageSetupDialog1.PrinterSettings.GetHdevnames().ToInt32());
//pass the paper to the emf printer
printer.Paper = paper;
//set the page layout control's printer to the currently selected printer
axPageLayoutControl1.Printer = printer;
}
private void button3_Click(object sender, System.EventArgs e)
{
//initialize the currently printed page number
m_CurrentPrintPage = 0;
//check if a document is loaded into PageLayout control
if(axPageLayoutControl1.DocumentFilename==null) return;
//set the name of the print preview document to the name of the mxd doc
document.DocumentName = axPageLayoutControl1.DocumentFilename;
//set the PrintPreviewDialog.Document property to the PrintDocument object selected by the user
printPreviewDialog1.Document = document;
//show the dialog - this triggers the document's PrintPage event
printPreviewDialog1.ShowDialog();
}
private void button4_Click(object sender, System.EventArgs e)
{
//allow the user to choose the page range to be printed
printDialog1.AllowSomePages = true;
//show the help button.
printDialog1.ShowHelp = true;
//set the Document property to the PrintDocument for which the PrintPage Event
//has been handled. To display the dialog, either this property or the
//PrinterSettings property must be set
printDialog1.Document = document;
//show the print dialog and wait for user input
DialogResult result = printDialog1.ShowDialog();
// If the result is OK then print the document.
if (result==DialogResult.OK) document.Print();
}
private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//this code will be called when the PrintPreviewDialog.Show method is called
//set the PageToPrinterMapping property of the Page. This specifies how the page
//is mapped onto the printer page. By default the page will be tiled
//get the selected mapping option
string sPageToPrinterMapping = (string)this.comboBox1.SelectedItem;
if(sPageToPrinterMapping == null)
//if no selection has been made the default is tiling
axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile;
else if (sPageToPrinterMapping.Equals("esriPageMappingTile"))
axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile;
else if(sPageToPrinterMapping.Equals("esriPageMappingCrop"))
axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingCrop;
else if(sPageToPrinterMapping.Equals("esriPageMappingScale"))
axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingScale;
else
axPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile;
//get the resolution of the graphics device used by the print preview (including the graphics device)
short dpi = (short)e.Graphics.DpiX;
//envelope for the device boundaries
IEnvelope devBounds = new EnvelopeClass();
//get page
IPage page = axPageLayoutControl1.Page;
//the number of printer pages the page will be printed on
short printPageCount;
printPageCount = axPageLayoutControl1.get_PrinterPageCount(0);
m_CurrentPrintPage++;
//the currently selected printer
IPrinter printer = axPageLayoutControl1.Printer;
//get the device bounds of the currently selected printer
page.GetDeviceBounds(printer, m_CurrentPrintPage, 0, dpi, devBounds);
//structure for the device boundaries
tagRECT deviceRect;
//Returns the coordinates of lower, left and upper, right corners
double xmin,ymin,xmax,ymax;
devBounds.QueryCoords(out xmin, out ymin, out xmax, out ymax);
//initialize the structure for the device boundaries
deviceRect.bottom = (int) ymax;
deviceRect.left = (int) xmin;
deviceRect.top = (int) ymin;
deviceRect.right = (int) xmax;
//determine the visible bounds of the currently printed page
IEnvelope visBounds = new EnvelopeClass();
page.GetPageBounds(printer, m_CurrentPrintPage, 0, visBounds);
//get a handle to the graphics device that the print preview will be drawn to
IntPtr hdc = e.Graphics.GetHdc();
//print the page to the graphics device using the specified boundaries
axPageLayoutControl1.ActiveView.Output(hdc.ToInt32(), dpi, ref deviceRect, visBounds, m_TrackCancel);
//release the graphics device handle
e.Graphics.ReleaseHdc(hdc);
//check if further pages have to be printed
if( m_CurrentPrintPage < printPageCount)
e.HasMorePages = true; //document_PrintPage event will be called again
else
e.HasMorePages = false;
}
}
}
[Visual Basic .NET]
PrintPreview.vb
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Output
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS
Public Class Form1
Inherits System.Windows.Forms.Form
<STAThread()> _
Shared Sub Main()
'Load runtime
If Not RuntimeManager.Bind(ProductCode.Engine) Then
If Not RuntimeManager.Bind(ProductCode.Desktop) Then
MessageBox.Show("Unable to bind to ArcGIS runtime. Application will be shut down.")
System.Environment.Exit(1) ' Force exit or other indication in the application
End If
End If
Application.Run(New Form1())
End Sub
'Declare a PrintDocument object named document.
Private WithEvents document As New System.Drawing.Printing.PrintDocument
Private m_CurrentPrintPage As Short 'the page number of the currently printed page
Private m_TrackCancel As ITrackCancel 'a cancel tracker for the printing process
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
'Release COM objects
ESRI.ArcGIS.ADF.COMSupport.AOUninitialize.Shutdown()
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents PrintDialog1 As System.Windows.Forms.PrintDialog
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
Friend WithEvents PageSetupDialog1 As System.Windows.Forms.PageSetupDialog
Friend WithEvents AxPageLayoutControl1 As ESRI.ArcGIS.Controls.AxPageLayoutControl
Friend WithEvents AxLicenseControl1 As ESRI.ArcGIS.Controls.AxLicenseControl
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.PrintDialog1 = New System.Windows.Forms.PrintDialog
Me.Button4 = New System.Windows.Forms.Button
Me.ComboBox1 = New System.Windows.Forms.ComboBox
Me.PageSetupDialog1 = New System.Windows.Forms.PageSetupDialog
Me.AxPageLayoutControl1 = New ESRI.ArcGIS.Controls.AxPageLayoutControl
Me.AxLicenseControl1 = New ESRI.ArcGIS.Controls.AxLicenseControl
CType(Me.AxPageLayoutControl1, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.AxLicenseControl1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(16, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(96, 32)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Open File"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(128, 8)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(96, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Page Setup"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(240, 8)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(96, 32)
Me.Button3.TabIndex = 3
Me.Button3.Text = "Print Preview"
'
'PrintPreviewDialog1
'
Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
Me.PrintPreviewDialog1.Enabled = True
Me.PrintPreviewDialog1.Icon = CType(resources.GetObject("PrintPreviewDialog1.Icon"), System.Drawing.Icon)
Me.PrintPreviewDialog1.Location = New System.Drawing.Point(154, 203)
Me.PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250)
Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
Me.PrintPreviewDialog1.Visible = False
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(352, 8)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(96, 32)
Me.Button4.TabIndex = 7
Me.Button4.Text = "Print"
'
'ComboBox1
'
Me.ComboBox1.Location = New System.Drawing.Point(464, 16)
Me.ComboBox1.Name = "ComboBox1"
Me.ComboBox1.Size = New System.Drawing.Size(144, 21)
Me.ComboBox1.TabIndex = 8
'
'AxPageLayoutControl1
'
Me.AxPageLayoutControl1.Location = New System.Drawing.Point(0, 48)
Me.AxPageLayoutControl1.Name = "AxPageLayoutControl1"
Me.AxPageLayoutControl1.OcxState = CType(resources.GetObject("AxPageLayoutControl1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxPageLayoutControl1.Size = New System.Drawing.Size(624, 448)
Me.AxPageLayoutControl1.TabIndex = 9
'
'AxLicenseControl1
'
Me.AxLicenseControl1.Enabled = True
Me.AxLicenseControl1.Location = New System.Drawing.Point(440, 112)
Me.AxLicenseControl1.Name = "AxLicenseControl1"
Me.AxLicenseControl1.OcxState = CType(resources.GetObject("AxLicenseControl1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxLicenseControl1.Size = New System.Drawing.Size(32, 32)
Me.AxLicenseControl1.TabIndex = 10
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 494)
Me.Controls.Add(Me.AxLicenseControl1)
Me.Controls.Add(Me.AxPageLayoutControl1)
Me.Controls.Add(Me.ComboBox1)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Print Preview / Print dialog Sample"
CType(Me.AxPageLayoutControl1, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.AxLicenseControl1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myStream As System.IO.Stream
'create a new open file dialog object
Dim openFileDialog1 As New OpenFileDialog
'initialize the filter, filter index and restore directory flag
openFileDialog1.Filter = "template files (*.mxt)|*.mxt|mxd files (*.mxd)|*.mxd"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
'display the dialog and wait for user input
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
Dim fileName As String
fileName = openFileDialog1.FileName 'get the selected file
If AxPageLayoutControl1.CheckMxFile(fileName) Then
'load the file in the page layout control
AxPageLayoutControl1.LoadMxFile(fileName, "")
End If
myStream.Close()
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
PageSetupDialog1.Document = document
'Show the page setup dialog storing the result.
Dim result As DialogResult
result = PageSetupDialog1.ShowDialog()
'set the printer settings of the preview document to the selected printer settings
document.PrinterSettings = PageSetupDialog1.PrinterSettings
'set the page settings of the preview document to the selected page settings
document.DefaultPageSettings = PageSetupDialog1.PageSettings
'due to a bug in PageSetupDialog the PaperSize has to be set explicitly by iterating through the
'available PaperSizes in the PageSetupDialog finding the selected PaperSize
Dim i As Integer
For i = 0 To PageSetupDialog1.PrinterSettings.PaperSizes.Count - 1
If PageSetupDialog1.PrinterSettings.PaperSizes.Item(i).Kind = document.DefaultPageSettings().PaperSize().Kind Then
document.DefaultPageSettings().PaperSize() = PageSetupDialog1.PrinterSettings.PaperSizes.Item(i)
End If
Next i
'------------------------------------------------------------
'initialize the current printer from the printer settings selected
'in the page setup dialog
'-------------------------------------------------------------
Dim paper As IPaper
paper = New PaperClass 'create a paper object
Dim printer As IPrinter
printer = New EmfPrinterClass 'create a printer object
'in this case an EMF printer, alternatively a PS printer could be used
'initialize the paper with the DEVMODE and DEVNAMES structures from the windows GDI
'these structures specify information about the initialization and environment of a printer as well as
'driver, device, and output port names for a printer
paper.Attach(PageSetupDialog1.PrinterSettings.GetHdevmode(PageSetupDialog1.PageSettings).ToInt32(), PageSetupDialog1.PrinterSettings.GetHdevnames().ToInt32())
'pass the paper to the emf printer
printer.Paper = paper
'set the page layout control's printer to the currently selected printer
AxPageLayoutControl1.Printer = printer
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'initialize the currently printed page number
m_CurrentPrintPage = 0
If AxPageLayoutControl1.DocumentFilename Is Nothing Then Exit Sub
'set the print document's name to that if the document in the PageLayoutControl
document.DocumentName = AxPageLayoutControl1.DocumentFilename
'set the Document property to the PrintDocument object selected by the user
PrintPreviewDialog1.Document = document
'show the dialog - this triggers the document's PrintPage event
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'allow the user to choose the page range to be printed
PrintDialog1.AllowSomePages = True
'show the help button.
PrintDialog1.ShowHelp = True
'set the Document property to the PrintDocument for which the PrintPage Event
'has been handled. To display the dialog, either this property or the
'PrinterSettings property must be set
PrintDialog1.Document = document
'display the print dialog and wait for user input
Dim result As DialogResult = PrintDialog1.ShowDialog()
'if the result is OK then print the document.
If (result = Windows.Forms.DialogResult.OK) Then document.Print()
End Sub
Private Sub InitializePrintPreviewDialog()
'set the size, location, name and the minimum size the dialog can be resized to
PrintPreviewDialog1.ClientSize() = New System.Drawing.Size(800, 600)
PrintPreviewDialog1.Location() = New System.Drawing.Point(29, 29)
PrintPreviewDialog1.Name() = "PrintPreviewDialog1"
PrintPreviewDialog1.MinimumSize = New System.Drawing.Size(375, 250)
'set UseAntiAlias to true to allow the operating system to smooth fonts
PrintPreviewDialog1.UseAntiAlias = True
End Sub
Private Sub InitializePageSetupDialog()
'initialize the dialog's PrinterSettings property to hold user defined printer settings.
PageSetupDialog1.PageSettings = New System.Drawing.Printing.PageSettings
'Initialize dialog's PrinterSettings property to hold user set printer settings
PageSetupDialog1.PrinterSettings = New System.Drawing.Printing.PrinterSettings
'Do not show the network in the printer dialog
PageSetupDialog1.ShowNetwork = False
End Sub
Private Sub document_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles document.PrintPage
'this code will be called when the PrintPreviewDialog.Show method is called
'set the PageToPrinterMapping property of the Page. This specifies how the page
'is mapped onto the printer page.
Dim sPageToPrinterMapping As String
sPageToPrinterMapping = Me.ComboBox1.SelectedItem 'get the selected mapping option
If sPageToPrinterMapping Is Nothing Then 'if no selection has been made the default is tiling
AxPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile
Else
If sPageToPrinterMapping.Equals("esriPageMappingTile") = True Then
AxPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile
ElseIf sPageToPrinterMapping.Equals("esriPageMappingCrop") = True Then
AxPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingCrop
ElseIf sPageToPrinterMapping.Equals("esriPageMappingScale") = True Then
AxPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingScale
Else
AxPageLayoutControl1.Page.PageToPrinterMapping = esriPageToPrinterMapping.esriPageMappingTile
End If
End If
Dim deviceRect As ESRI.ArcGIS.esriSystem.tagRECT 'structure for the device boundaries
Dim dpi As Short
dpi = e.Graphics.DpiX 'get the resolution of the graphics device used by the print preview
'e holds print preview arguments, including the graphics device
Dim devBounds As IEnvelope 'envelope for the device boundaries
devBounds = New EnvelopeClass
Dim page As IPage 'the page of the page layout control
page = AxPageLayoutControl1.Page
Dim printer As IPrinter 'the currently selected printer
printer = AxPageLayoutControl1.Printer
Dim printPageCount As Short 'the number of printer pages the page will be printed on
printPageCount = AxPageLayoutControl1.get_PrinterPageCount(0) '.Page.PrinterPageCount(printer, 0, printPageCount)
m_CurrentPrintPage += 1
'get the device bounds of the currently selected printer
page.GetDeviceBounds(printer, m_CurrentPrintPage, 0, dpi, devBounds)
'Returns the coordinates of lower, left and upper, right corners
Dim xmin, ymin, xmax, ymax As Double
devBounds.QueryCoords(xmin, ymin, xmax, ymax)
'initialize the structure for the device boundaries
deviceRect.bottom = ymax
deviceRect.left = xmin
deviceRect.top = ymin
deviceRect.right = xmax
'determine the visible bounds of the currently printed page
Dim visBounds As IEnvelope
visBounds = New EnvelopeClass
page.GetPageBounds(printer, m_CurrentPrintPage, 0, visBounds)
'get a handle to the graphics device
'this is the device the print preview will be drawn to
Dim hdc As IntPtr = e.Graphics.GetHdc()
'print the page of the page layout control to the graphics device using the specified boundaries
AxPageLayoutControl1.ActiveView.Output(hdc.ToInt32(), dpi, deviceRect, visBounds, m_TrackCancel)
'release the graphics device handle
e.Graphics.ReleaseHdc(hdc)
'check if further pages have to be printed
If m_CurrentPrintPage < printPageCount Then
e.HasMorePages = True 'document_PrintPage event will be called again
Else
e.HasMorePages = False
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitializePrintPreviewDialog()
InitializePageSetupDialog()
ComboBox1.Items.Add("esriPageMappingTile")
ComboBox1.Items.Add("esriPageMappingCrop")
ComboBox1.Items.Add("esriPageMappingScale")
ComboBox1.SelectedIndex = 0
End Sub
End Class