http://chs.gotdotnet.com/quickstart/winforms/doc/WinFormsPrinting.aspx

解决方案 »

  1.   

    默认PrintDocument的尺寸单位是什么?
    能否改单位?
      

  2.   

    把每页要打的内容放到PrintPage事件中去
      

  3.   

    关于PrintDocument的尺寸单位请参考:
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpguide/html/_gdiplus_types_of_coordinate_systems_about.htm
    ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemDrawingGraphicsUnitClassTopic.htm
      

  4.   

    还有不是在printDocument的print方法中打印所有的
    而是打印每页的。
    例程:
      
        using System;
        using System.ComponentModel;
        using System.Windows.Forms;
        using System.Drawing;
        using System.Drawing.Printing;
        using System.IO;    // <doc>
        // <desc>
        //     TextFilePrintDocument 将流输出到打印机
        //
        //     注意:为了避免在关闭文件时出问题,
        //     如果发生异常,此类仅使用一个流
        //     并将它留给调用方,以打开该文件
        //     进行打印
        //
        // </desc>
        // </doc>
        public class TextPrintDocument : PrintDocument {        private Font printFont             = null;
            private StringReader streamToPrint = null;
            private string overflowText        = null;        public TextPrintDocument(StringReader streamToPrint, Font printFont) : base ()  {
                this.streamToPrint = streamToPrint ;
                this.printFont = printFont;
            }        //重写 OnBeginPrint 以设置将要使用的字体
            protected override void OnBeginPrint(PrintEventArgs ev) {
                base.OnBeginPrint(ev) ;            overflowText = null;
            }        //重写 OnPrintPage 以为文档提供打印逻辑
            protected override void OnPrintPage(PrintPageEventArgs ev) {            base.OnPrintPage(ev) ;            float lpp = 0 ;
                float yPos =  0 ;
                int count = 0 ;
                float leftMargin = ev.MarginBounds.Left;
                float topMargin = ev.MarginBounds.Top;
                String line=null;            //算出每页的行数
                //在事件上使用 MarginBounds 以达到此目的
                lpp = ev.MarginBounds.Height  / printFont.GetHeight(ev.Graphics) ;            //如果有上页的溢出文本,则先处理它
                while (count < lpp && overflowText != null) {
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    int linesPrinted = PrintLine(ev,overflowText, yPos);
                    count += linesPrinted;
                }            //现在,在文件上重复此操作以输出每行
                //首先检查行数,以便看不到不打印的行
                while (count < lpp && ((line=streamToPrint.ReadLine()) != null)) {
                    yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
                    int linesPrinted = PrintLine(ev,line, yPos);
                    count += linesPrinted;
                }            //如果有多行,则另外打印一页
                if (line != null)
                    ev.HasMorePages = true ;
                else
                    ev.HasMorePages = false ;
            }
            private int PrintLine(PrintPageEventArgs e, string text, float yStartPos) {
                Graphics graphics = e.Graphics;
                Margins margins = e.PageSettings.Margins;
                StringFormat format = new StringFormat();
                int lines;
                int characters;
                RectangleF rectangle = new RectangleF(margins.Left,
                                        yStartPos,
                                        e.MarginBounds.Width,
                                        e.MarginBounds.Height);            graphics.MeasureString(text, printFont, rectangle.Size, format, out characters, out lines);            //如果字符数少于 string.length,则无法适合该页上完整的段落,
                //因此打印可以看到的内容,将剩余的内容
                //保存在下一页
                if (characters < text.Length) {
                    overflowText = text.Substring(characters);
                } else {
                    overflowText = null;
                }            graphics.DrawString(text, printFont, Brushes.Black, rectangle, format);
                //处理空行
                if (lines == 0 )
                    lines = 1;            return lines;
            }    }
      

  5.   

    这是我的代码
    Graphics g = e.Graphics;
    g.PageUnit = GraphicsUnit.Millimeter;
    g.SmoothingMode = SmoothingMode.AntiAlias; //提高字体的质量

    leftMargin = e.MarginBounds.Left;
    topMargin = e.MarginBounds.Top;
    rightMargin = e.MarginBounds.Right;
    bottomMargin = e.MarginBounds.Bottom;可是为什么我取得的边距非常大?
    得到的边距=默认单位取得的边距 * 毫米/默认单位
    为什么?
    为什么取边距时,不能用毫米做单位来取?
      

  6.   

    获取表示边距以内的页面部分的矩形区域。
    [C#]
    public Rectangle MarginBounds {get;}属性值
    表示边距以内的页面部分的矩形区域。尺寸数量级为百分之一英寸。