The print preview application. : Print PrintDocument « GUI Windows Forms « C# / CSharp Tutorial

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. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » GUI Windows Forms » Print PrintDocument 
22. 51. 6. The print preview application.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Button button1;
    private System.ComponentModel.Container components = null;
    private System.Drawing.Printing.PrintDocument ThePrintDocument = null;
    private System.IO.StringReader myStringReader = null;

    public Form1() {
        ThePrintDocument = new System.Drawing.Printing.PrintDocument();
        this.button1 = new System.Windows.Forms.Button();
        this.button2 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.button1.Location = new System.Drawing.Point(112352);
        this.button1.Text = "&Preview";
        this.button1.Click += new System.EventHandler(this.button1_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(512397);
        this.Controls.AddRange(new System.Windows.Forms.Control[] { this.button1 });
        this.ResumeLayout(false);
    }

    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    protected void PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs ev) {
        float linesPerPage = 0;
        float yPosition = 0;
        int count = 0;
        float leftMargin = ev.MarginBounds.Left;
        float topMargin = ev.MarginBounds.Top;
        string line = null;
        Font printFont = this.Font;
        SolidBrush myBrush = new SolidBrush(Color.Black);

        linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
        while (count < linesPerPage && ((line = myStringReader.ReadLine()) != null)) {
            yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
            ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
            count++;
        }
        if (line != null)
            ev.HasMorePages = true;
        else
            ev.HasMorePages = false;

        myBrush.Dispose();
    }

    private void button1_Click(object sender, System.EventArgs e) {
        ThePrintDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(PrintPage);
        string strText = "STRING";
        myStringReader = new System.IO.StringReader(strText);
        PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
        printPreviewDialog1.Document = this.ThePrintDocument;
        printPreviewDialog1.ShowDialog();
    }
}
22. 51. Print PrintDocument
22. 51. 1. Basic Printing
22. 51. 2. Print BMP imagePrint BMP image
22. 51. 3. Multi Page Print
22. 51. 4. Subclass PrintDocument
22. 51. 5. Print a paragraph
22. 51. 6. The print preview application.
22. 51. 7. Print With Margins
22. 51. 8. Print PageSettings Metrics
22. 51. 9. PrintDocument: DocumentName, PrintPage, Print
22. 51. 10. The print process application.
w___w___w_.___java_2__s___.___c___o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.