Print PageSettings Metrics : 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. 8. Print PageSettings Metrics
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;


public class Form1 : Form {
    private Font mainTextFont = new Font("Times New Roman"14);
    private Font subTextFont = new Font("Times New Roman"12);
    private PageSettings storedPageSettings;

    public Form1() {
        InitializeComponent();
    }

    private void PaintDocument(Graphics g) {
        g.PageUnit = GraphicsUnit.Point;
        g.DrawString("Simple Printing Sample",
                     this.mainTextFont,
                     Brushes.Black,
                     new Rectangle(102018030));
        g.DrawRectangle(Pens.Blue,
                        new Rectangle(new Point(10100)new Size(10050)));
    }

    private void Form1_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        PaintDocument(g);
    }

    private void menuFilePageSetup_Click(object sender, EventArgs e) {
        PageSetupDialog psDlg = new PageSetupDialog();
        if (this.storedPageSettings == null)
            this.storedPageSettings = new PageSettings();
        psDlg.PageSettings = this.storedPageSettings;
        psDlg.ShowDialog();

    }

    private void WriteMetricsToConsole(PrintPageEventArgs ev) {
        Graphics g = ev.Graphics;
        Console.WriteLine("ev.PageSettings.PaperSize: " + ev.PageSettings.PaperSize);
        Console.WriteLine("ev.PageSettings.PrinterResolution: " + ev.PageSettings.PrinterResolution);
        Console.WriteLine("ev.PageSettings.PrinterSettings.LandscapeAngle: " + ev.PageSettings.PrinterSettings.LandscapeAngle);
        Console.WriteLine("ev.PageSettings.Bounds: " + ev.PageSettings.Bounds);
        Console.WriteLine("ev.PageBounds: " + ev.PageBounds);
        Console.WriteLine("ev.PageSettings.Margins: " + ev.PageSettings.Margins);
        Console.WriteLine("ev.MarginBounds: " + ev.MarginBounds);
        Console.WriteLine("Horizontal resolution: " + g.DpiX);
        Console.WriteLine("Vertical resolution: " + g.DpiY);
        g.SetClip(ev.PageBounds);
        Console.WriteLine("g.VisibleClipBounds: " + g.VisibleClipBounds);
        SizeF drawingSurfaceSize = new SizeFg.VisibleClipBounds.Width * g.DpiX / 100,g.VisibleClipBounds.Height * g.DpiY / 100);
        Console.WriteLine("Drawing Surface Size in Pixels: " + drawingSurfaceSize);
    }

    protected void PrintPageEventHandler(Object obj, PrintPageEventArgs ev) {
        WriteMetricsToConsole(ev);
        Graphics g = ev.Graphics;
        PaintDocument(g);
        ev.HasMorePages = false;
    }

    private void menuFilePrint_Click(object sender, EventArgs e) {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);

        if (this.storedPageSettings != null)
            pd.DefaultPageSettings = this.storedPageSettings;
        PrintDialog dlg = new PrintDialog();
        dlg.Document = pd;
        DialogResult result = dlg.ShowDialog();
        if (result == System.Windows.Forms.DialogResult.OK)
            pd.Print();
    }

    private void menuFilePrintPreview_Click(object sender, EventArgs e) {
        PrintDocument pd = new PrintDocument();
        pd.PrintPage += new PrintPageEventHandler(this.PrintPageEventHandler);

        if (this.storedPageSettings != null)
            pd.DefaultPageSettings = this.storedPageSettings;
        PrintPreviewDialog dlg = new PrintPreviewDialog();
        dlg.Document = pd;
        dlg.ShowDialog();
    }
    private void InitializeComponent() {
        this.menuStrip1 = new System.Windows.Forms.MenuStrip();
        this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
        this.menuFilePageSetup = new System.Windows.Forms.ToolStripMenuItem();
        this.menuFilePrintPreview = new System.Windows.Forms.ToolStripMenuItem();
        this.menuFilePrint = new System.Windows.Forms.ToolStripMenuItem();
        this.menuStrip1.SuspendLayout();
        this.SuspendLayout();
        this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem});
        this.menuStrip1.Location = new System.Drawing.Point(00);
        this.menuStrip1.Size = new System.Drawing.Size(29225);
        this.menuStrip1.Text = "menuStrip1";
        this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.menuFilePageSetup,
            this.menuFilePrintPreview,
            this.menuFilePrint});
        this.fileToolStripMenuItem.Text = "File";
        this.menuFilePageSetup.Text = "Page Setup";
        this.menuFilePageSetup.Click += new System.EventHandler(this.menuFilePageSetup_Click);
        this.menuFilePrintPreview.Text = "Print Preview";
        this.menuFilePrintPreview.Click += new System.EventHandler(this.menuFilePrintPreview_Click);
        this.menuFilePrint.Text = "Print";
        this.menuFilePrint.Click += new System.EventHandler(this.menuFilePrint_Click);
        this.AutoScaleDimensions = new System.Drawing.SizeF(8F16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.Window;
        this.ClientSize = new System.Drawing.Size(292268);
        this.Controls.Add(this.menuStrip1);
        this.MainMenuStrip = this.menuStrip1;
        this.Text = "SimplePrintingExample";
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
        this.menuStrip1.ResumeLayout(false);
        this.ResumeLayout(false);
        this.PerformLayout();
    }
    private System.Windows.Forms.MenuStrip menuStrip1;
    private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
    private System.Windows.Forms.ToolStripMenuItem menuFilePageSetup;
    private System.Windows.Forms.ToolStripMenuItem menuFilePrintPreview;
    private System.Windows.Forms.ToolStripMenuItem menuFilePrint;
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
}
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.
ww__w__.___j___a_v_a__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.