Display PrintDialog : PrintDialog « GUI Windows Forms « C# / CSharp Tutorial






Display PrintDialog
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;

public class PrintDialogDemo : System.Windows.Forms.Form
{
  private System.Windows.Forms.TextBox txtFile;
  private System.Windows.Forms.Button btnOpenFile;
  private System.Windows.Forms.Button btnSaveFile;

  private System.ComponentModel.Container components = null;
  private System.Windows.Forms.Button btnPageSetup;
  private System.Windows.Forms.Button btnPrint;
  private System.Windows.Forms.Button btnPrintPreview;  
  
  private String strFileName;
  private Font currentFont = null;
  private PrintDocument printDocument = new PrintDocument();
  private StringReader stringReader;
  private OpenFileDialog openFileDialog = new OpenFileDialog();

  public PrintDialogDemo()
  {
    InitializeComponent();
    
    printDocument.PrintPage += new PrintPageEventHandler(pdPrintPage);
    printDocument.BeginPrint += new PrintEventHandler(pdBeginPrint);
    printDocument.EndPrint += new PrintEventHandler(pdEndPrint);
        currentFont= Font;
  }

  private void InitializeComponent()
  {
    this.txtFile = new System.Windows.Forms.TextBox();
    this.btnOpenFile = new System.Windows.Forms.Button();
    this.btnSaveFile = new System.Windows.Forms.Button();
    this.btnPageSetup = new System.Windows.Forms.Button();
    this.btnPrint = new System.Windows.Forms.Button();
    this.btnPrintPreview = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // txtFile
    // 
    this.txtFile.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
      | System.Windows.Forms.AnchorStyles.Left) 
      | System.Windows.Forms.AnchorStyles.Right);
    this.txtFile.Location = new System.Drawing.Point(40, 24);
    this.txtFile.Multiline = true;
    this.txtFile.Name = "txtFile";
    this.txtFile.Size = new System.Drawing.Size(500, 150);
    this.txtFile.TabIndex = 0;
    this.txtFile.Text = "";
    // 
    // btnOpenFile
    // 
    this.btnOpenFile.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
    this.btnOpenFile.Location = new System.Drawing.Point(48, 256);
    this.btnOpenFile.Name = "btnOpenFile";
    this.btnOpenFile.TabIndex = 1;
    this.btnOpenFile.Text = "Open File";
    this.btnOpenFile.Click += new System.EventHandler(this.btnOpenFile_Click);
    // 
    // btnSaveFile
    // 
    this.btnSaveFile.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
    this.btnSaveFile.Location = new System.Drawing.Point(144, 256);
    this.btnSaveFile.Name = "btnSaveFile";
    this.btnSaveFile.TabIndex = 2;
    this.btnSaveFile.Text = "Save File";
    this.btnSaveFile.Click += new System.EventHandler(this.btnSaveFile_Click);
    // 
    // btnPageSetup
    // 
    this.btnPageSetup.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
    this.btnPageSetup.Location = new System.Drawing.Point(240, 344);
    this.btnPageSetup.Name = "btnPageSetup";
    this.btnPageSetup.TabIndex = 8;
    this.btnPageSetup.Text = "Page Setup";
    this.btnPageSetup.Click += new System.EventHandler(this.btnPageSetup_Click);
    // 
    // btnPrint
    // 
    this.btnPrint.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
    this.btnPrint.Location = new System.Drawing.Point(48, 344);
    this.btnPrint.Name = "btnPrint";
    this.btnPrint.TabIndex = 6;
    this.btnPrint.Text = "Print";
    this.btnPrint.Click += new System.EventHandler(this.btnPrint_Click);
    // 
    // btnPrintPreview
    // 
    this.btnPrintPreview.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
    this.btnPrintPreview.Location = new System.Drawing.Point(144, 344);
    this.btnPrintPreview.Name = "btnPrintPreview";
    this.btnPrintPreview.Size = new System.Drawing.Size(80, 23);
    this.btnPrintPreview.TabIndex = 7;
    this.btnPrintPreview.Text = "Print Preview";
    this.btnPrintPreview.Click += new System.EventHandler(this.btnPrintPreview_Click);
    // 
    // PrintDialogDemo
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(592, 398);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                             this.btnPrintPreview,
                                             this.btnPrint,
                                             this.btnPageSetup,
                                             this.btnSaveFile,
                                             this.btnOpenFile,
                                             this.txtFile});
    this.ResumeLayout(false);

  }
  [STAThread]
  static void Main() 
  {
    Application.Run(new PrintDialogDemo());
  }

  private void btnOpenFile_Click(object sender, System.EventArgs e)
  {
    openFileDialog.InitialDirectory = @"c:\";
    openFileDialog.Filter = "Text files (*.txt)|*.txt|" +
            "All files (*.*)|*.*";
    openFileDialog.FilterIndex = 1;              //  1 based index
    
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
      StreamReader reader = new StreamReader(openFileDialog.FileName);
      try
      {
        strFileName = openFileDialog.FileName;
        txtFile.Text = reader.ReadToEnd();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
        return;
      }
      finally
      {
        reader.Close();
      }
    }
  }

  private void btnSaveFile_Click(object sender, System.EventArgs e)
  {
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.InitialDirectory = @"c:\";
    sfd.Filter = "Text files (*.txt)|*.txt|" +
            "All files (*.*)|*.*";
    sfd.FilterIndex = 1;              //  1 based index

    if (strFileName != null)
      sfd.FileName = strFileName;
    else
      sfd.FileName = "*.txt";
    
    if (sfd.ShowDialog() == DialogResult.OK)
    {
      StreamWriter writer = new StreamWriter(strFileName,false);
      try
      {
        strFileName = sfd.FileName;
        writer.Write(txtFile.Text);
      }
      catch(Exception ex)
      {
        MessageBox.Show(ex.Message);
        return;
      }
      finally
      {
        writer.Close();
      }
    }
  }

  private void btnPageSetup_Click(object sender, System.EventArgs e)
  {
    PageSetupDialog psd = new PageSetupDialog();
    psd.Document = printDocument;
    psd.ShowDialog();
  }

  private void btnPrint_Click(object sender, System.EventArgs e)
  {
    PrintDialog pdlg = new PrintDialog();
    pdlg.Document = printDocument;
    
    if (pdlg.ShowDialog() == DialogResult.OK)
    {
      try
      {
        printDocument.Print();
      }
      catch(Exception ex)
      {
        MessageBox.Show("Print error: " + ex.Message);
      }
    }
  }

  private void btnPrintPreview_Click(object sender, System.EventArgs e)
  {
    PrintDialog ppdlg = new PrintDialog();
    ppdlg.Document = printDocument;
    ppdlg.ShowDialog();
  }

  private void pdPrintPage(object sender, PrintPageEventArgs e)
  {
    float linesPerPage = 0;
    float verticalOffset = 0;
    float leftMargin = e.MarginBounds.Left;
    float topMargin = e.MarginBounds.Top;
    int linesPrinted = 0;
    String strLine = null;

    linesPerPage = e.MarginBounds.Height / currentFont.GetHeight(e.Graphics);
    
    while (linesPrinted < linesPerPage &&
        ((strLine = stringReader.ReadLine())!= null ))
    {
      verticalOffset = topMargin + (linesPrinted * currentFont.GetHeight(e.Graphics));
      e.Graphics.DrawString(strLine, currentFont, Brushes.Black, leftMargin, verticalOffset);
      linesPrinted++;
    }
    
    if (strLine != null)
      e.HasMorePages = true;
    else
      e.HasMorePages = false;
      
  }

  private void pdBeginPrint(object sender, PrintEventArgs e)
  {
    stringReader = new StringReader(txtFile.Text);
    currentFont = txtFile.Font;
  }

  private void pdEndPrint(object sender, PrintEventArgs e)
  {
    stringReader.Close();
    MessageBox.Show("Done printing.");
  }
}








23.49.PrintDialog
23.49.1.Display PrintDialogDisplay PrintDialog
23.49.2.Prompt PrintDialogPrompt PrintDialog