Exemplo n.º 1
0
        /// <summary>
        /// 计算打印位置
        /// </summary>
        /// <param name="mPrintDetail"></param>
        /// <param name="DefultX"></param>
        /// <param name="DefaultY"></param>
        /// <param name="ps"></param>
        /// <returns></returns>
        private CurrentSite GetSite(PrintDetailModel mPrintDetail, int DefultX, int DefaultY, PageSettings ps)
        {
            CurrentSite cs = new CurrentSite();

            switch (mPrintDetail.Align)
            {
            case 0:
                cs.X = DefultX + mPrintDetail.SiteX;
                cs.Y = DefaultY + mPrintDetail.SiteY;
                break;

            case 1:
                int nStringLength = mPrintDetail.ContentDesc.Length * mPrintDetail.FontSize;
                int PageWidth     = ps.Landscape == true ? ps.PaperSize.Height : ps.PaperSize.Width;
                cs.X = int.Parse((PageWidth / 2 - nStringLength / 2).ToString());
                cs.Y = DefaultY + mPrintDetail.SiteY;
                break;

            default:
                cs.X = DefultX;
                cs.Y = DefaultY;
                break;
            }
            return(cs);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 计算打印位置
 /// </summary>
 /// <param name="mPrintDetail"></param>
 /// <param name="DefultX"></param>
 /// <param name="DefaultY"></param>
 /// <param name="ps"></param>
 /// <returns></returns>
 private CurrentSite GetSite(PrintDetailModel mPrintDetail, int DefultX, int DefaultY, PageSettings ps)
 {
     CurrentSite cs = new CurrentSite();
     switch (mPrintDetail.Align)
     {
         case 0:
             cs.X = DefultX + mPrintDetail.SiteX;
             cs.Y = DefaultY + mPrintDetail.SiteY;
             break;
         case 1:
             int nStringLength = mPrintDetail.ContentDesc.Length * mPrintDetail.FontSize;
             int PageWidth = ps.Landscape == true ? ps.PaperSize.Height : ps.PaperSize.Width;
             cs.X = int.Parse((PageWidth / 2 - nStringLength / 2).ToString());
             cs.Y = DefaultY + mPrintDetail.SiteY;
             break;
         default:
             cs.X = DefultX;
             cs.Y = DefaultY;
             break;
     }
     return cs;
 }
Exemplo n.º 3
0
        /// <summary>
        /// 打印表格以及自定义字符.输出到打印文档上
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            x = ps.Margins.Left;
            y = ps.Margins.Top;
            #region 打印自定义内容

            if (mPrint.PrintId > 0 && mPrint.PrintDetailList.Count > 0)
            {
                int nY = 0;
                foreach (PrintDetailModel mPrintDetail in mPrint.PrintDetailList)
                {
                    FontStyle fs = FontStyle.Regular;
                    if (mPrintDetail.FontBold)
                    {
                        fs |= FontStyle.Bold;
                    }
                    if (mPrintDetail.UnderLine)
                    {
                        fs |= FontStyle.Underline;
                    }
                    Font ft = new Font("Arial", mPrintDetail.FontSize, fs);

                    CurrentSite cs = GetSite(mPrintDetail, x, y, e.PageSettings);

                    Brush bsh = new SolidBrush(mPrintDetail.FontColor);
                    e.Graphics.DrawString(mPrintDetail.ContentDesc, ft, bsh, cs.X, cs.Y);
                    nY = nY < cs.Y ? cs.Y : nY;
                }
                y += nY;//取最大的高.用于未来打印表格.
            }

            #endregion

            #region 打印列表

            for (; dgvIndex < dataGridView.Length; dgvIndex++)
            {
                rowCount = dataGridView[dgvIndex].Rows.Count;

                colCount = dataGridView[dgvIndex].ColumnCount;
                //y += rowGap;

                //x = ps.Margins.Left;
                x = GetTableSiteX(dataGridView[dgvIndex], ps);
                //x = IsShowSetupDialog == true ? pageSetupDialog.PageSettings.Margins.Left : LeftMargin;

                #region 打印列头

                for (int j = 0; j < colCount; j++)
                {
                    if (dataGridView[dgvIndex].Columns[j].Width > 0 && dataGridView[dgvIndex].Columns[j].Visible == true)
                    {
                        cellValue = dataGridView[dgvIndex].Columns[j].HeaderText;

                        if (ShowLine)
                        {
                            //绘制矩形边框
                            e.Graphics.DrawRectangle(Pens.Black, x, y, dataGridView[dgvIndex].Columns[j].Width, rowGap);
                        }
                        //写入数据
                        e.Graphics.DrawString(cellValue, headingFont, brush, x, y);

                        x += dataGridView[dgvIndex].Columns[j].Width;
                    }
                }

                #endregion

                #region 打印所有行

                for (; i < rowCount; i++)
                {
                    y += rowGap;
                    x  = GetTableSiteX(dataGridView[dgvIndex], ps);
                    //x = ps.Margins.Left;
                    //x = IsShowSetupDialog == true ? pageSetupDialog.PageSettings.Margins.Left : LeftMargin;

                    for (int j = 0; j < colCount; j++)
                    {
                        if (dataGridView[dgvIndex].Columns[j].Width > 0 && dataGridView[dgvIndex].Columns[j].Visible == true)
                        {
                            cellValue = dataGridView[dgvIndex].Rows[i].Cells[j].Value != null ? dataGridView[dgvIndex].Rows[i].Cells[j].Value.ToString() : "";
                            if (ShowLine)
                            {
                                e.Graphics.DrawRectangle(Pens.Black, x, y, dataGridView[dgvIndex].Columns[j].Width, rowGap);
                            }
                            e.Graphics.DrawString(cellValue, font, brush, x, y);

                            x += dataGridView[dgvIndex].Columns[j].Width;
                        }
                    }

                    #region 分页

                    if (y >= e.PageBounds.Height - 80)
                    {
                        //允許多頁打印
                        y = 0;
                        e.HasMorePages = true;
                        i++;
                        return;
                    }

                    #endregion
                }
                #endregion

                y += rowGap;

                //for (int j = 0; j < colCount; j++)
                //{
                //    e.Graphics.DrawString(" ", font, brush, x, y);
                //}
                i = 0;
            }
            #endregion

            e.HasMorePages = false;
            dgvIndex       = 0;
        }