主页 > 编程资料 > C# >
发布时间:2015-09-26 作者:网络 阅读:238次

文章标题是翻译出的中文标题,英文原文标题为:Master Everyday Printing in .NET

文章来自Fawcette Technical Publications(www.ftponline.com)

文章详细地讲述了 System.Drawing.Printing  命名空间中的 20个类 7 个枚举以及 3个委托的用法。

本文将先贴出英文原文,最后再贴出完整源代码,希望对大家有所帮助!

英文原文:

Listing 1 C#

Create and Dispose.

The PrintDocument object exposes several events. BeginPrint is fired before any pages are actually printed, giving you the opportunity to allocate objects or open files. EndPrint occurs after the last page has been printed. Don’t forget to bind the events to your PrintDocument object.

 

// At the class level

private Font bodyFont;

private Font headerFont;

private StreamReader data;

private PrintDocument doc;

private void MainForm_Load(object

   sender, System.EventArgs e) {

   doc = new PrintDocument();

   // shows up in Print Manager

   doc.DocumentName = “Contact List”;

   doc.BeginPrint += new

       PrintEventHandler(doc_BeginPrint);

   doc.EndPrint += new

       PrintEventHandler(doc_EndPrint);

}

private void doc_BeginPrint(object

   sender, PrintEventArgs pv) {

   data = new

       StreamReader("contacts.csv");

   Font bodyFont = new Font(“Arial”,

       12);

   Font headerFont = new Font(“Arial”,

       24);

}

private void doc_EndPrint(object

   sender, PrintEventArgs pv) {

   data.Close();

   bodyFont.Dispose();

   headerFont.Dispose();

}

 

Listing 2 C#

Inherit From PrintDocument.

Derive a new class from PrintDocument so you can encapsulate all your printing functionality in a single place, enhancing code reuse.

 

public class CustomPrintDocument : PrintDocument {

   private StreamReader dataToPrint;

   public CustomPrintDocument(StreamReader data) : base()

   {

       dataToPrint = data;

   }

   protected override void OnBeginPrint(PrintEventArgs

       ev) {

       base.OnBeginPrint(ev) ;

   }

   protected override void OnEndPrint(PrintEventArgs ev)

   {

       base.OnEndPrint(ev);

   }

   protected override void

       OnQueryPageSettings(QueryPageSettingsEventArgs ev)

   {

       base.OnQueryPageSettings(ev);

   }

   protected override void OnPrintPage(PrintPageEventArgs

       ev) {

       base.OnPrintPage(ev);

       ev.Graphics.DrawString("this is a test", new

          Font("Arial", 24), Brushes.Black, 100, 100);

       ev.HasMorePages = false;

   }

}

 

Listing 3 C#

Retrieve the Margins With P/Invoke.

The .NET Framework doesn’t provide a way to retrieve the hard margins of a printer, so you need to use P/Invoke and call the Win32 GetDeviceCaps function. The method in this class takes a device handle (hDc), then populates the class members with the information you need.

 

[DllImport("gdi32.dll")]

private static extern Int16 GetDeviceCaps([In] [MarshalAs

   (UnmanagedType.U4)] int hDc, [In] [MarshalAs

   (UnmanagedType.U2)] Int16 funct);

private float _leftMargin = 0;

private float _topMargin = 0;

private float _rightMargin = 0;

private float _bottomMargin = 0;

const short HORZSIZE      = 4;

const short VERTSIZE      = 6;

const short HORZRES       = 8;

const short VERTRES       = 10;

const short PHYSICALOFFSETX = 112;

const short PHYSICALOFFSETY = 113;

public marginInfo(int deviceHandle) {

   float offx = Convert.ToSingle(

       GetDeviceCaps(deviceHandle, PHYSICALOFFSETX));

   float offy = Convert.ToSingle(

       GetDeviceCaps(deviceHandle, PHYSICALOFFSETY));

   float resx = Convert.ToSingle(

       GetDeviceCaps(deviceHandle, HORZRES));

   float resy = Convert.ToSingle(

       GetDeviceCaps(deviceHandle, VERTRES));

   float hsz = Convert.ToSingle(

       GetDeviceCaps(deviceHandle, HORZSIZE))/25.4f;

   float vsz = Convert.ToSingle(

       GetDeviceCaps(deviceHandle,VERTSIZE))/25.4f;

   float ppix = resx/hsz;

   float ppiy = resy/vsz;

   _leftMargin  = (offx/ppix) * 100.0f;

   _topMargin   = (offy/ppix) * 100.0f;

   _bottomMargin  = _topMargin + (vsz * 100.0f);

   _rightMargin  = _leftMargin + (hsz * 100.0f);

}

 

Listing 4 C#

Lay Out the Report.

The basic format of any report must include at least a header, body, and footer section. Use Rectangle objects to lay out your reports easily. You can increase the complexity of those reports by adding more Rectangle objects.

 

// create the header

int headerHeight =

   hf.GetHeight(ev.Graphics);

RectangleF header = new

   RectangleF(leftMargin, topMargin,

       pageWidth, headerHeight);

// create the footer

int bodyFontHeight =

   bodyFont.GetHeight(ev.Graphics);

RectangleF footer = new 

   RectangleF(leftMargin, body.Bottom,

       pageWidth, bodyFontHeight);

// create the body section

RectangleF body = new

   RectangleF(leftMargin, header.Bottom,

       pageWidth, pageHeight –

       bodyFontHeight);

 

Listing 5 C#

PrintPage Provides the Key to Printing.

The PrintPage event is the primary event you’ll use when printing using the System.Drawing.Printing objects. This event fires once per page until you set the value of ev.HasMorePages to false. The code used to retrieve the hard margins makes up for any shortcomings in the .NET Framework.

 

private void doc_PrintPage(object sender,

   System.Drawing.Printing.PrintPageEventArgs ev) {

   _currentPage++;

   String headerText = "Northwinds Customer Contacts";

   IntPtr hDc = ev.Graphics.GetHdc();

   ev.Graphics.ReleaseHdc(hDc);

   marginInfo mi = new marginInfo(hDc.ToInt32());

   // take the hard margins into account…

   float leftMargin = ev.MarginBounds.Left - mi.Left;

   float rightMargin = ev.MarginBounds.Right;

   float topMargin = ev.MarginBounds.Top - mi.Left;

   float bottomMargin = ev.MarginBounds.Bottom;

   float pageHeight = bottomMargin - topMargin;

   float pageWidth = rightMargin - leftMargin;

   float headerHeight =

   headerFont.GetHeight(ev.Graphics);

   float footerHeight = bodyFont.GetHeight(ev.Graphics);

   // report header

   RectangleF ReportheaderR = new RectangleF(leftMargin,

       topMargin, pageWidth, headerHeight);

   // report body

   RectangleF bodyR = new RectangleF(leftMargin,

       ReportheaderR.Bottom, pageWidth, pageHeight –

       ReportheaderR.Height - footerHeight);           

   // report footer

   RectangleF ReportfooterR = new RectangleF(leftMargin,

       bodyR.Bottom, pageWidth, footerHeight * 2);

   // results of using the Split function on the text

   String[] el;

   // a line of text from our file

   string text = "";

   // print the header once per page

   centerText(ev.Graphics, headerText, headerFont,

       defaultBrush, ReportheaderR);

   // the header is equal to 2 normal lines

   int currentLine = 2;

   // how many lines can we fit on a page?             

   int linesPerPage = Convert.ToInt32(bodyR.Height /

       bodyFont.GetHeight(ev.Graphics)) - 1;

   float bodyFontHeight =

       bodyFont.GetHeight(ev.Graphics);

   float currentY;

   // Print each line of the file.

   while(currentLine < linesPerPage &&

   ((text=data.ReadLine()) != null)) {

       el = text.Split(',');

       currentY = getCurrentY(currentLine, topMargin,

          bodyFontHeight);

       ev.Graphics.DrawString(el[0], bodyFont,

          defaultBrush, bodyR.Left, currentY);

       currentLine++;

       currentY = getCurrentY(currentLine, topMargin,

          bodyFontHeight);

       ev.Graphics.DrawString(el[1],

          bodyFont, defaultBrush, bodyR.Left + 20,

          currentY);

       currentLine++;

       currentY = getCurrentY(currentLine, topMargin,

          bodyFontHeight);

       ev.Graphics.DrawString("Phone: " + el[2],

          bodyFont, defaultBrush, bodyR.Left + 20,

          currentY);

       currentLine++;

       currentY = getCurrentY(currentLine, topMargin,

          bodyFontHeight);

       ev.Graphics.DrawString("Fax: " + el[3],

          bodyFont,defaultBrush, bodyR.Left + 20,

          currentY);

       currentLine++;

       currentY = getCurrentY(currentLine, topMargin,

          bodyFontHeight);

       ev.Graphics.DrawLine(Pens.Black, leftMargin,

          currentY, ev.MarginBounds.Right, currentY);

   }

   // page number

   centerText(ev.Graphics, "Page " +

       currentPage.ToString(), bodyFont,

       defaultBrush, ReportfooterR);

   if (text != null) {

       ev.HasMorePages = true;

   } else {

       // no more pages to print

       ev.HasMorePages = false;

   }

}

private float getCurrentY(int currentLine, float

   topMargin, float fontHeight) {

   return topMargin + (currentLine * fontHeight);

}

private void centerText(Graphics g,

   string t, Font f, Brush b, RectangleF

   rect) {

   StringFormat sf = new StringFormat();

   sf.Alignment =

       StringAlignment.Center;

   g.DrawString(t, f, b, rect, sf);

}

 

 




完整源代码:

using System;
using System.IO;
using System.Drawing;
using System.Drawing.Printing;
using System.ComponentModel;
using System.Windows.Forms;

namespace printTest {
    public class PrintForm : System.Windows.Forms.Form
 {

        // private variables
        private PrintDocument _PDoc;
        private StreamReader _Data;
        private int _currentPage = 0;
        // these will be created in BeginPrint and
        // destroyed in EndPrint.
        private Font _headerFont;
        private Font _bodyFont;
        private Brush _defaultBrush = Brushes.Black;
        private Pen _defaultPen = new Pen(Brushes.Black, .25f);

        private System.Windows.Forms.Button PreviewButton;
        private System.Windows.Forms.Button PrintButton;
        private System.Windows.Forms.Label StatusLabel;

        ///


        /// Required designer variable.
        ///

        private System.ComponentModel.Container components = null;

  public PrintForm()
  {
   InitializeComponent();
  }

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  ///


  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  ///

  private void InitializeComponent()
  {
            this.PreviewButton = new System.Windows.Forms.Button();
            this.PrintButton = new System.Windows.Forms.Button();
            this.StatusLabel = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // PreviewButton
            //
            this.PreviewButton.Location = new System.Drawing.Point(108, 40);
            this.PreviewButton.Name = "PreviewButton";
            this.PreviewButton.TabIndex = 1;
            this.PreviewButton.Text = "Preview";
            this.PreviewButton.Click += new System.EventHandler(this.PreviewButton_Click);
            //
            // PrintButton
            //
            this.PrintButton.Location = new System.Drawing.Point(188, 40);
            this.PrintButton.Name = "PrintButton";
            this.PrintButton.TabIndex = 2;
            this.PrintButton.Text = "Print";
            this.PrintButton.Click += new System.EventHandler(this.PrintButton_Click);
            //
            // StatusLabel
            //
            this.StatusLabel.Location = new System.Drawing.Point(9, 8);
            this.StatusLabel.Name = "StatusLabel";
            this.StatusLabel.Size = new System.Drawing.Size(352, 23);
            this.StatusLabel.TabIndex = 3;
            this.StatusLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            //
            // PrintForm
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(370, 71);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.StatusLabel,
                                                                          this.PrintButton,
                                                                          this.PreviewButton});
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "PrintForm";
            this.Text = "Print Demo";
            this.Load += new System.EventHandler(this.PrintForm_Load);
            this.ResumeLayout(false);

        }
  #endregion

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

        private void PreviewButton_Click(object sender, System.EventArgs e)
  {
            PrintPreviewDialog previewDialog = new PrintPreviewDialog();           

            // display a pagesetup dialog
            PageSetupDialog pageSetup = new PageSetupDialog();
            pageSetup.Document = _PDoc;
            DialogResult Rc = pageSetup.ShowDialog();
            if (Rc == DialogResult.Cancel)
   {
                return;
            }           

            // display the preview dialog
            previewDialog.Document = _PDoc;
            previewDialog.PrintPreviewControl.Zoom = 1.0;
            previewDialog.WindowState = FormWindowState.Maximized;
            previewDialog.ShowInTaskbar = true;
            previewDialog.ShowDialog();
        }

        private void _PDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)
  {

            _currentPage++;

            String headerText = "Northwinds Customer Contacts";

            marginInfo mi;

            float leftMargin = 0f;
            float rightMargin = 0f;
            float topMargin = 0f;
            float bottomMargin = 0f;
            float pageHeight = 0f;
            float pageWidth = 0f;

            // retrieve the hard printer margins
            IntPtr hDc = ev.Graphics.GetHdc();
            try {
                mi = new marginInfo(hDc.ToInt32());   
            } finally {
                ev.Graphics.ReleaseHdc(hDc);
            }
           
            ev.Graphics.PageUnit = GraphicsUnit.Inch;
            ev.Graphics.PageScale = .01f;
            ev.Graphics.TranslateTransform(-mi.Left, -mi.Top);

            // retrieve the margins
            leftMargin = ev.MarginBounds.Left - mi.Left;
            rightMargin = ev.MarginBounds.Right;
            topMargin = ev.MarginBounds.Top - mi.Top;
            bottomMargin = ev.MarginBounds.Bottom;
            pageHeight = bottomMargin - topMargin;
            pageWidth = rightMargin - leftMargin;

            // used to define the sections of the document
            float headerHeight = _headerFont.GetHeight(ev.Graphics);
            float footerHeight = _bodyFont.GetHeight(ev.Graphics);           

            // report header
            RectangleF ReportheaderR = new RectangleF(leftMargin, topMargin, pageWidth, headerHeight);
            // report body
            RectangleF bodyR = new RectangleF(leftMargin, ReportheaderR.Bottom, pageWidth, pageHeight - ReportheaderR.Height - footerHeight);
            // report footer
            RectangleF ReportfooterR = new RectangleF(leftMargin, bodyR.Bottom, pageWidth, footerHeight * 2);

           
            // uncomment these lines to draw borders around the rectangles
            /*
            drawRect(ev.Graphics, _defaultPen, leftMargin, topMargin, pageWidth, headerHeight);
            drawRect(ev.Graphics, _defaultPen, leftMargin, ReportheaderR.Bottom, pageWidth, pageHeight - ReportheaderR.Height - footerHeight);
            drawRect(ev.Graphics, _defaultPen, leftMargin, bodyR.Bottom, pageWidth, ReportfooterR.Height);                                   
           
            centerText(ev.Graphics, "Header section", _headerFont, _defaultBrush, ReportheaderR);
            centerText(ev.Graphics, "Body section", _headerFont, _defaultBrush, bodyR);
            centerText(ev.Graphics, "Footer section", _headerFont, _defaultBrush, ReportfooterR);          
            return;
            */

            // results of using the Split function on the text
            String[] data;
            // a line of text from our file
            string text = "";

            // print the header once per page
            centerText(ev.Graphics, headerText, _headerFont, _defaultBrush, ReportheaderR);

            // how many lines can we fit on a page?             
            float bodyFontHeight = footerHeight;
            int linesPerPage = Convert.ToInt32(bodyR.Height / bodyFontHeight) - 1;

            // the header = 2 lines
            int currentLine = 2;
            // currentY is what we'll use for the Top (Y) coordinate that
            // DrawString needs.
            float currentY = 0;

            // Print each line of the file.  This could be easily modified to
            // deal with data from a DataTable
            while(currentLine + 1 < linesPerPage && ((text=_Data.ReadLine()) != null))
   {
               
                data = text.Split(',');

                // retrieve the current top position
                currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
                // DrawString will take a Rectangle argument, but NOT a RectangleF arg,
                // so we need to provide the X and Y arguments.
                ev.Graphics.DrawString(data[0], _bodyFont, _defaultBrush,  bodyR.Left, currentY);
                currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
                ev.Graphics.DrawString(data[1], _bodyFont, _defaultBrush, bodyR.Left + 25, currentY);
                currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
                ev.Graphics.DrawString("Phone: " + data[2], _bodyFont, _defaultBrush, bodyR.Left + 25, currentY);
                currentY = getCurrentY(currentLine++, bodyR.Top, bodyFontHeight);
                ev.Graphics.DrawString("Fax: " + data[3], _bodyFont,_defaultBrush, bodyR.Left + 25, currentY);               
                //currentY = getCurrentY(currentLine++, topMargin, bodyFontHeight);
                //ev.Graphics.DrawLine(_defaultPen, leftMargin, currentY, pageWidth, currentY);
                currentLine++;
            }

            // page number
            centerText(ev.Graphics, "Page " + _currentPage.ToString(), _bodyFont, _defaultBrush, ReportfooterR);

            // Do we have more pages to print?
            if (text != null) {
                ev.HasMorePages = true;
            } else {
                ev.HasMorePages = false;
            }
        }

        private float getCurrentY(int currentLine, float topMargin, float fontHeight)
  {
            return topMargin + (currentLine * fontHeight);
        }

        // my wrapper for DrawRectangle
        private void drawRect(Graphics g, Pen p, float left, float top, float width, float height)
  {
            g.DrawRectangle(_defaultPen, left, top, width, height);
        }

        // the StringFormat class has a lot of cool features
        private void centerText(Graphics g, string t, Font f, Brush b, RectangleF rect)
  {
            StringFormat sf = new StringFormat();
            // center horizontally
            sf.Alignment = StringAlignment.Center;
            // center vertically
            sf.LineAlignment = StringAlignment.Center;
            g.DrawString(t, f, b, rect, sf);
        }

        // used for debugging
        private void dumpRectF(RectangleF rect)
  {
            System.Diagnostics.Debug.WriteLine(rect.ToString());
        }

        private void _PDoc_BeginPrint(object sender, PrintEventArgs pv)
  {
            // make sure we reset this before printing
            _currentPage = 0;

            // the file should be in the same folder as the EXE
            _Data = new StreamReader("contacts.csv");
            _headerFont = new Font("Arial", 24, FontStyle.Underline, GraphicsUnit.World);
            _bodyFont = new Font("Arial", 12, GraphicsUnit.World);
        }

        private void _PDoc_EndPrint(object sender, PrintEventArgs pv)
  {
            _Data.Close();
            _headerFont.Dispose();
            _bodyFont.Dispose();
        }

        private void _PDoc_QueryPageSettings(object sender, QueryPageSettingsEventArgs ev)
  {
           
        }

        private void PrintForm_Load(object sender, System.EventArgs e)
  {           
            _PDoc = new PrintDocument();
            // this is what will show up in the print manager
            _PDoc.DocumentName = "printTest -- Northwind Contact List";
            // make sure at least one printer is installed
            if (_PDoc.PrinterSettings.PrinterName == "") {
                StatusLabel.Text = "At least one (1) printer must be installed to continue.";
                PrintButton.Enabled = false;
                PreviewButton.Enabled = false;
            } else {
                StatusLabel.Text = "You have " + PrinterSettings.InstalledPrinters.Count + " printer(s) installed.";
                // bind the events
                _PDoc.PrintPage += new PrintPageEventHandler(_PDoc_PrintPage);
                _PDoc.BeginPrint += new PrintEventHandler(_PDoc_BeginPrint);
                _PDoc.EndPrint += new PrintEventHandler(_PDoc_EndPrint);
                _PDoc.QueryPageSettings += new QueryPageSettingsEventHandler(_PDoc_QueryPageSettings);
            }
        }

        private void PrintButton_Click(object sender, System.EventArgs e)
  {
            PrintDialog pd = new PrintDialog();
            pd.Document = _PDoc;
            DialogResult Rc = pd.ShowDialog();

            if (Rc == DialogResult.OK) {
                _PDoc.Print();
            }
        }
 }
}

<> <>
关键字词: