Print Preview Dialog for printing text file : Print Preview Dialog « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » Print Preview Dialog 
23.50.2.Print Preview Dialog for printing text file
Print Preview Dialog for printing text file
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 PrintPreviewDialog : 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= Font;
  private PrintDocument printDocument = new PrintDocument();
  private StringReader stringReader;
  private OpenFileDialog openFileDialog = new OpenFileDialog();

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

  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(4024);
    this.txtFile.Multiline = true;
    this.txtFile.Name = "txtFile";
    this.txtFile.Size = new System.Drawing.Size(500150);
    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(48256);
    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(144256);
    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(240344);
    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(48344);
    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(144344);
    this.btnPrintPreview.Name = "btnPrintPreview";
    this.btnPrintPreview.Size = new System.Drawing.Size(8023);
    this.btnPrintPreview.TabIndex = 7;
    this.btnPrintPreview.Text = "Print Preview";
    this.btnPrintPreview.Click += new System.EventHandler(this.btnPrintPreview_Click);
    // 
    // PrintPreviewDialog
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(513);
    this.ClientSize = new System.Drawing.Size(592398);
    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 PrintPreviewDialog());
  }

  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)
  {
    PrintPreviewDialog ppdlg = new PrintPreviewDialog();
    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.50.Print Preview Dialog
23.50.1.Show PrintPreview Dialog before print out a documentShow PrintPreview Dialog before print out a document
23.50.2.Print Preview Dialog for printing text filePrint Preview Dialog for printing text file
23.50.3.Output the PrinterSettings information
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.