我是调用API添加了自定义纸张,计算后每页应显示30行,
     预览正常,可打印时,后10行却打不出来.请大家帮忙看看!
//DBPaperSize 是调用API添加自定义纸张类
            DBPaperSize myDBPaperSize = new DBPaperSize();
            //SetPrintForm方法添加自定纸张,参数分别是:打印机名,纸张名,宽,长. 
            myDBPaperSize.SetPrintForm(printDocument1.PrinterSettings.DefaultPageSettings.PrinterSettings.PrinterName,
                "enghui", 240, 140);            //遍历以取出自定义纸张
            string paperName = "enghui";
            foreach (PaperSize paperSize in this.printDocument1.PrinterSettings.PaperSizes)
            {
                if (paperSize.PaperName == paperName)
                {
                    this.printDocument1.DefaultPageSettings.PaperSize = paperSize;
                    break;
                }
            }
            
            //设置页边距
            this.printDocument1.DefaultPageSettings.Margins.Top = this.printDocument1.DefaultPageSettings.Margins.Top / 2;
            this.printDocument1.DefaultPageSettings.Margins.Bottom = this.printDocument1.DefaultPageSettings.Margins.Bottom / 2;       //计算打印行数,kVerticalCellLeeway是行间距,
       linesPerpage = (PageHeight - TopMargin - BottomMargin) / (8 + kVerticalCellLeeway);

解决方案 »

  1.   

    打印代码有点乱,如下:using System;
    using System.Windows.Forms;
    using System.Drawing;
    using System.Drawing.Printing;
    using System.Data;
    using System.Collections;namespace lumber
    {
        class DataSetPrinter
        {
            #region 参数
            private PrintDocument ThePrintDocument;
            private DataTable TheTable;
            
            public static int RowCount = 0; // current count of rows;
            //private int Count = 0;
            private const int kVerticalCellLeeway = 5;
            public int PageNumber = 1;        int PageWidth;
            int PageHeight;
            int TopMargin;
            int BottomMargin;
            public int linesPerpage;        public static int iLumberCode = 0;
            public static string Data = string.Empty;
            public static int BlockNum_amount;
            public static double ChaiJi_amount;
            #endregion        public DataSetPrinter(PrintDocument aPrintDocument, DataTable aTable)
            {
                //
                // TODO: Add constructor logic here
                //
                ThePrintDocument = aPrintDocument;
                TheTable = aTable;            //得到打印参数
                PageWidth = ThePrintDocument.DefaultPageSettings.PaperSize.Width;
                PageHeight = ThePrintDocument.DefaultPageSettings.PaperSize.Height;
                TopMargin = ThePrintDocument.DefaultPageSettings.Margins.Top;
                BottomMargin = ThePrintDocument.DefaultPageSettings.Margins.Bottom ;
                linesPerpage = (PageHeight - (16 + 4 * kVerticalCellLeeway + TopMargin) - BottomMargin) / (8 + kVerticalCellLeeway) - 1;//-7
            }        public void DrawHeader(Graphics g)
            {
                SolidBrush ForeBrush = new SolidBrush(Color.Black);
                Pen TheLinePen = new Pen(Color.Black, 1);
                StringFormat cellformat = new StringFormat();
                cellformat.Trimming = StringTrimming.EllipsisCharacter;
                cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;            int columnwidth = PageWidth * 13 / 15 / (this.TheTable.Columns.Count + 1);
                float startxposition = PageWidth / 15;            g.DrawString("日 报 表", new Font("Arial", 8, FontStyle.Regular), new SolidBrush(Color.Black), PageWidth / 15, TopMargin, new StringFormat());
                g.DrawString(Data, new Font("Arial", 8, FontStyle.Regular), new SolidBrush(Color.Black), startxposition + 2 * columnwidth, TopMargin, new StringFormat());
                g.DrawString("堆号: " + DataSetPrinter.iLumberCode.ToString(), new Font("Arial", 8, FontStyle.Regular), new SolidBrush(Color.Black), startxposition + 4 * columnwidth, TopMargin, new StringFormat());            
                int initialRowCount = RowCount;            // draw the table header
                
                RectangleF nextcellbounds = new RectangleF(0, 0, 0, 0);            //RectangleF HeaderBounds = new RectangleF(0, 0, 0, 0);            //HeaderBounds.X = PageWidth / 15;
                //HeaderBounds.Y = 25 + 4 * kVerticalCellLeeway + TopMargin;
                //HeaderBounds.Height = 10 + kVerticalCellLeeway;
                //HeaderBounds.Width = PageWidth * 13 / 15;            //g.FillRectangle(BackBrush, HeaderBounds);            for (int k = 0; k < TheTable.Columns.Count - 1; k++)
                {                string nextcolumn = TheTable.Columns[k].ToString();
                    switch (nextcolumn)
                    {
                        case "Provider":
                            nextcolumn = "供应商";
                            break;
                        case "Category":
                            nextcolumn = "材种";
                            break;
                        case "Length":
                            nextcolumn = "长";
                            break;
                        case "High":
                            nextcolumn = "高";
                            break;
                        case "Yard":
                            nextcolumn = "码数";
                            break;
                        case "BlockNum":
                            nextcolumn = "块数";
                            break;
                        case "ChaiJi":
                            nextcolumn = "材积";
                            break;
                        default:
                            break;
                    }                RectangleF cellbounds;                if (k == 0 || k == 1)
                    {
                        cellbounds = new RectangleF(startxposition, 8 + 2 * kVerticalCellLeeway + TopMargin,
                        columnwidth * 2,
                        8 + kVerticalCellLeeway);
                    }
                    else
                    {
                        cellbounds = new RectangleF(startxposition, 8 + 2 * kVerticalCellLeeway + TopMargin,
                        columnwidth,
                        8 + kVerticalCellLeeway);
                    }
                    nextcellbounds = cellbounds;                if (startxposition + columnwidth <= PageWidth)
                    {                    g.DrawString(nextcolumn, new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellbounds, cellformat);
                    }                if (k == 0 || k == 1)
                    {
                        startxposition = startxposition + columnwidth * 2;
                    }
                    else
                    {
                        startxposition = startxposition + columnwidth;
                    }
                }
                g.DrawLine(TheLinePen, PageWidth / 15, nextcellbounds.Bottom, PageWidth * 14 / 15, nextcellbounds.Bottom);
            }
      

  2.   

    public bool DrawRows(System.Drawing.Printing.PrintPageEventArgs e)
            {
                int lastRowBottom = 16 + 4 * kVerticalCellLeeway + TopMargin;
                try
                {
                    SolidBrush ForeBrush = new SolidBrush(Color.Black);
                    Pen TheLinePen = new Pen(Color.Black, 1);
                    StringFormat cellformat = new StringFormat();
                    cellformat.Trimming = StringTrimming.EllipsisCharacter;
                    cellformat.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit;
                    int columnwidth = PageWidth * 13 / 15 / (this.TheTable.Columns.Count + 1);                //int initialRowCount = RowCount;                RectangleF RowBounds = new RectangleF(0, 0, 0, 0);                #region 如果不是最后一页                if (TheTable.Rows.Count - RowCount > linesPerpage)
                    {
                        #region
                        for (int i = 0; i < linesPerpage; i++)
                        {                        DataRow dr = TheTable.Rows[RowCount];
                            int startxposition = PageWidth / 15;                        for (int j = 0; j < TheTable.Columns.Count - 1; j++)
                            {
                                RectangleF cellbounds;
                                if (j == 0 || j == 1)
                                {
                                    cellbounds = new RectangleF(startxposition,
                                    16 + 4 * kVerticalCellLeeway + TopMargin + i * (8 + kVerticalCellLeeway),
                                    columnwidth * 2,
                                    8 + kVerticalCellLeeway);
                                }
                                else
                                {
                                    cellbounds = new RectangleF(startxposition,
                                       16 + 4 * kVerticalCellLeeway + TopMargin + i * (8 + kVerticalCellLeeway),
                                       columnwidth,
                                       8 + kVerticalCellLeeway);
                                }
                                if (startxposition + columnwidth <= PageWidth)
                                {
                                    e.Graphics.DrawString(dr[j].ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellbounds, cellformat);
                                    lastRowBottom = (int)cellbounds.Bottom;
                                }
                                if (j == 0 || j == 1)
                                {
                                    startxposition = startxposition + columnwidth * 2;
                                }
                                else
                                {
                                    startxposition = startxposition + columnwidth;
                                }
                            }
                            RowCount++;
                            if (Convert.ToInt32((TheTable.Rows[RowCount])[BULumber.Field_LumberCode]) != DataSetPrinter.iLumberCode)
                            {                            RectangleF cellboundsHeJi = new RectangleF(PageWidth / 15 + 6 * columnwidth,
                                       16 + 4 * kVerticalCellLeeway + TopMargin + (i + 1) * (8 + kVerticalCellLeeway),
                                       columnwidth,
                                       8 + kVerticalCellLeeway);
                                e.Graphics.DrawString("合计:", new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsHeJi, cellformat);                            RectangleF cellboundsBlockNum = new RectangleF(PageWidth / 15 + 7 * columnwidth,
                                               16 + 4 * kVerticalCellLeeway + TopMargin + (i + 1) * (8 + kVerticalCellLeeway),
                                               columnwidth,
                                               8 + kVerticalCellLeeway);
                                e.Graphics.DrawString(DataSetPrinter.BlockNum_amount.ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsBlockNum, cellformat);                            RectangleF cellboundsChaiJi = new RectangleF(PageWidth / 15 + 8 * columnwidth,
                                               16 + 4 * kVerticalCellLeeway + TopMargin + (i + 1) * (8 + kVerticalCellLeeway),
                                               columnwidth,
                                               8 + kVerticalCellLeeway);
                                e.Graphics.DrawString(DataSetPrinter.ChaiJi_amount.ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsChaiJi, cellformat);                            DataSetPrinter.iLumberCode = Convert.ToInt32((TheTable.Rows[RowCount])[BULumber.Field_LumberCode]);
                                SetAmount();
                                break;
                            }
                        }
                        #endregion
                    }
    #endregion
      

  3.   

    #region 如果是最后一页
                    else
                    {
                        #region
                        bool isLastPage = true;
                        int count = 0;
                        while (RowCount < TheTable.Rows.Count)
                        {
                            DataRow dr = TheTable.Rows[RowCount];
                            int startxposition = PageWidth / 15;
                            for (int j = 0; j < TheTable.Columns.Count - 1; j++)
                            {
                                RectangleF cellbounds;
                                if (j == 0 || j == 1)
                                {
                                    cellbounds = new RectangleF(startxposition,
                                    16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                    columnwidth * 2,
                                    8 + kVerticalCellLeeway);
                                }
                                else
                                {
                                    cellbounds = new RectangleF(startxposition,
                                       16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                       columnwidth,
                                       8 + kVerticalCellLeeway);
                                }
                                if (startxposition + columnwidth <= PageWidth)
                                {
                                    e.Graphics.DrawString(dr[j].ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellbounds, cellformat);
                                    lastRowBottom = (int)cellbounds.Bottom;
                                }
                                if (j == 0 || j == 1)
                                {
                                    startxposition = startxposition + columnwidth * 2;
                                }
                                else
                                {
                                    startxposition = startxposition + columnwidth;
                                }
                            }
                            RowCount++;
                            count++;
                            if(RowCount == TheTable.Rows.Count)
                                break;
                            if (Convert.ToInt32((TheTable.Rows[RowCount])[BULumber.Field_LumberCode]) != DataSetPrinter.iLumberCode)
                            {
                                isLastPage = false;                            RectangleF cellboundsHeJi = new RectangleF(PageWidth / 15 + 6 * columnwidth,
                                       16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                       columnwidth,
                                       8 + kVerticalCellLeeway);
                        e.Graphics.DrawString("合计:", new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsHeJi, cellformat);                    RectangleF cellboundsBlockNum = new RectangleF(PageWidth / 15 + 7 * columnwidth,
                                       16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                       columnwidth,
                                       8 + kVerticalCellLeeway);
                        e.Graphics.DrawString(DataSetPrinter.BlockNum_amount.ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsBlockNum, cellformat);                    RectangleF cellboundsChaiJi = new RectangleF(PageWidth / 15 + 8 * columnwidth,
                                       16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                       columnwidth,
                                       8 + kVerticalCellLeeway);
                        e.Graphics.DrawString(DataSetPrinter.ChaiJi_amount.ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsChaiJi, cellformat);
                        
                                DataSetPrinter.iLumberCode = Convert.ToInt32((TheTable.Rows[RowCount])[BULumber.Field_LumberCode]);
                                SetAmount();
                                break;
                            }
                        }
                        if (isLastPage == true)
                        {
                            RectangleF cellboundsHeJi = new RectangleF(PageWidth / 15 + 6 * columnwidth,
                                           16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                           columnwidth,
                                           8 + kVerticalCellLeeway);
                            e.Graphics.DrawString("合计:", new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsHeJi, cellformat);                        RectangleF cellboundsBlockNum = new RectangleF(PageWidth / 15 + 7 * columnwidth,
                                           16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                           columnwidth,
                                           8 + kVerticalCellLeeway);
                            e.Graphics.DrawString(DataSetPrinter.BlockNum_amount.ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsBlockNum, cellformat);                        RectangleF cellboundsChaiJi = new RectangleF(PageWidth / 15 + 8 * columnwidth,
                                           16 + 4 * kVerticalCellLeeway + TopMargin + count * (8 + kVerticalCellLeeway),
                                           columnwidth,
                                           8 + kVerticalCellLeeway);
                            e.Graphics.DrawString(DataSetPrinter.ChaiJi_amount.ToString(), new Font("Arial", 8, FontStyle.Regular), ForeBrush, cellboundsChaiJi, cellformat);
                        }
                        #endregion
                    }
                    #endregion
                }
                #region catch
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    return false;
                }
                return true;
                #endregion
            }        private void SetAmount()
            {
                BaseDBCon myBaseDBCon = new BaseDBCon();
                BULumber myBULumber = new BULumber();
                myBaseDBCon.Open();
                object[] paramObject = { DataSetPrinter.Data, DataSetPrinter.iLumberCode };
                DataSetPrinter.BlockNum_amount = myBULumber.GetBlockNumSum(myBaseDBCon, paramObject);
                DataSetPrinter.ChaiJi_amount = myBULumber.GetChaiJiSum(myBaseDBCon, paramObject);
                myBaseDBCon.Close();
            }        public bool DrawDataSet(System.Drawing.Printing.PrintPageEventArgs e)
            {
                bool bContinue;
                try
                {
                    DrawHeader(e.Graphics);
                    bContinue = DrawRows(e);
                    if (RowCount < TheTable.Rows.Count)
                    {
                        e.HasMorePages = true;
                    }
                    else
                    {
                        e.HasMorePages = false;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                    return false;
                }
                return bContinue;
            }
        }
    }