winform 打印 预览正常,实际离边界最近的内容打印不完整

时间:2022-11-10 20:20:32
大家好:目前做winform打印发货单,打印预览很正常,但实际打印出现发现右边和下边 离边界最近的内容打印不完整。
printPage事件的代码如下:

private void printPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)//string Tid,
        {
            #region
            InitPrinter(e.Graphics);
            //try
            //{
            Graphics Gdraw = e.Graphics;
            StringFormat strFormat = new StringFormat();
            strFormat.Alignment = StringAlignment.Near;
            strFormat.LineAlignment = StringAlignment.Center;
            Font fontTitles;
            fontTitles = (Font)SerializableOperate.ObjectUnSerializable(MessageDecode.Fontset + MessageDecode.Fontset2 + InvoiceSet.txtInvoiceSample.Name);
            if (fontTitles == null)
                fontTitles = new Font("黑体", 11, FontStyle.Bold);

            Font fonts = new System.Drawing.Font("宋体", 10);
            Brush brushes = Brushes.Black;
            Pen pen = new Pen(Color.Black, 1);
            Rectangle rec = new Rectangle();
          

            DataRow dr = HB_table.Rows[DTrowIndex];
            {
                Font boldfont = new Font("宋体", 10, FontStyle.Bold);
                //Tid
                //Gdrwa.DrawRectangle(new Pen(Color.Red, 1), xml.txtboxRec("Tid"));
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/Tid"), fonts, brushes, xml.txtboxRec("Tid"));
                StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Far;
                Gdraw.DrawString(dr["Tid"].ToString().Substring(0, 15), boldfont, brushes, xml.txtboxRec("Tid"), sf);
                //打印时间
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/PrintTime") + DateTime.Now.ToString("yyyy-MM-dd"), fonts, brushes, xml.txtboxRec("PrintTime"));
                //我的买家
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/BuyerInfo"), fonts, brushes, xml.txtboxRec("BuyerInfo"));
                Gdraw.DrawString(dr["SDanwei"].ToString(), boldfont, brushes, xml.txtboxRec("BuyerInfoV"));
                //买家姓名
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/BuyerName"), fonts, brushes, xml.txtboxRec("BuyerName"));
                Gdraw.DrawString(dr["receiverName"].ToString(), boldfont, brushes, xml.txtboxRec("BuyerNameV"));
                //买家手机
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/BuyerMobilephone") + dr["StellPhone"].ToString(), fonts, brushes, xml.txtboxRec("BuyerMobilephone"));
                //买家电话
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/BuyerTelephone") + dr["Sphone"].ToString(), fonts, brushes, xml.txtboxRec("BuyerTelephone"));
                //买家地址
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/BuyerAddress") + dr["SAdress"].ToString(), fonts, brushes, xml.txtboxRec("BuyerAddress"));
                //我的卖家
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/SellerInfo"), fonts, brushes, xml.txtboxRec("SellerInfo"));
                Gdraw.DrawString(dr["JDanWei"].ToString(), boldfont, brushes, xml.txtboxRec("SellerInfoV"));
                //卖家姓名
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/SellerName"), fonts, brushes, xml.txtboxRec("SellerName"));
                //卖家手机

                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/SellerMobilephone"), fonts, brushes, xml.txtboxRec("SellerMobilephone"));
                //卖家备注
               
                string svalue = clearEnter(dr["slMemo"].ToString());
                fonts = new Font("宋体", 10, FontStyle.Bold);
                if (svalue.Length > 70)
                { fonts = new Font("宋体", 9, FontStyle.Bold); }
                Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/SellerMemo") + svalue, fonts, brushes, xml.txtboxRec("SellerMemo"));
                fonts = new System.Drawing.Font("宋体", 10);

            }
           
            #region
            //发货单打印样例
            Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/InvoiceSample"), fontTitles, brushes, xml.txtboxRec("InvoiceSample"));
            //Logo
            Gdraw.DrawImage(InvoiceSet.pbLogo.Image, xml.txtboxRec("Logo"));
            //物流编号
            Gdraw.DrawString(InvoiceTempleteXml.getAtrrValue("/Invoice_Templete_Set/TextBox_Set/WuliuNo") + dr["CompanyID"], fonts, brushes, xml.txtboxRec("WuliuNo"));
            //温馨提示
            Font ff = (Font)SerializableOperate.ObjectUnSerializable(MessageDecode.Fontset + MessageDecode.Fontset2 + InvoiceSet.txtTishi.Name);
            if (ff == null) { ff = new Font("黑体", 12); }
            Gdraw.DrawString(InvoiceTempleteXml.getTishi, ff, brushes, xml.txtboxRec("Tishi"));
            #endregion

            #endregion

            #region 绑定每一个订单的数据到DataGridView
            #region 画DataGridView的Header并赋值
            int rowHeight = 23;
            if (CurentRowNum >= 0)
            {
                headerheight = InvoiceSet.dgvOrder.ColumnHeadersHeight; //DataGridView 的标题行的高度

                Font font = InvoiceSet.dgvOrder.ColumnHeadersDefaultCellStyle.Font;//DataGridView 的单元格的文本内容格式
                if (font == null)
                    font = InvoiceSet.dgvOrder.DefaultCellStyle.Font;

                strFormat.Alignment = StringAlignment.Center;
                // strFormat.LineAlignment = StringAlignment.Center;

                rec.X = InvoiceSet.dgvOrder.Location.X;
                rec.Y = InvoiceSet.dgvOrder.Location.Y;

                for (int i = 0; i < ColsIndex.Count; i++)
                {
                    RectangleF cellBounds = new RectangleF(rec.X, rec.Y, ColsWidth[i], headerheight);
                    Gdraw.DrawRectangle(pen, rec.X, rec.Y, ColsWidth[i], headerheight);
                    Gdraw.DrawString(InvoiceSet.dgvOrder.Columns[ColsIndex[i]].HeaderText, font, brushes, cellBounds, strFormat);
                    rec.X += Convert.ToInt32(ColsWidth[i]);
                }
                X = PaperLeftMargin + (ContentWidth - dgvWidth) / 2; Y += headerheight;
                if (Y > PaperPageHeight - PaperBottomMargin - rowHeight && NowPage < PageCount) { NowPage++; Y = PaperTopMargin; }
            }
            #endregion
            #region 画DataGridView的Cell并赋值。
            int Total = 0;//每一页的产品数量
            Y = InvoiceSet.dgvOrder.Top + headerheight;
            for (int row_index = CurentRowNum; row_index < CurentRowNum + 11; row_index++)
            {
                if (row_index >= InvoiceSet.dgvOrder.Rows.Count) { continue; }
                Font font = InvoiceSet.dgvOrder.Rows[row_index].DefaultCellStyle.Font;
                if (font == null)
                {
                    font = new Font("宋体", 10);
                    //InvoiceSet.dgvOrder.DefaultCellStyle.Font;
                }
                rowHeight = InvoiceSet.dgvOrder.Rows[row_index].Height;
                X = InvoiceSet.dgvOrder.Left;
                for (int col_Index = 0; col_Index < ColsIndex.Count; col_Index++)
                {
                    RectangleF cellBounds = new RectangleF(X, Y, ColsWidth[col_Index], rowHeight);
                    Gdraw.DrawRectangle(pen, X, Y, Convert.ToInt32(ColsWidth[col_Index]), rowHeight);
                    if (col_Index == 1)
                    {
                        Total += Convert.ToInt32(InvoiceSet.dgvOrder.Rows[row_index].Cells[1].Value.ToString());
                    }
                    if (col_Index % 2 == 0)
                    {
                        StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center;
                        Gdraw.DrawString(InvoiceSet.dgvOrder.Rows[row_index].Cells[ColsIndex[col_Index]].Value.ToString(), font, brushes, cellBounds, sf);
                    }
                    else
                    {
                        Gdraw.DrawString(InvoiceSet.dgvOrder.Rows[row_index].Cells[ColsIndex[col_Index]].Value.ToString(), font, brushes, cellBounds, strFormat);
                    }
                    X += Convert.ToInt32(ColsWidth[col_Index]);
                }
                X = PaperLeftMargin + (ContentWidth - dgvWidth) / 2;
                Y += rowHeight;
            }
            CurentRowNum += 11;
            #endregion
            #region 总计
            X = InvoiceSet.dgvOrder.Left;
            int X2 = InvoiceSet.dgvOrder.Width;
            Gdraw.DrawRectangle(pen, X, Y, ColsWidth[0] + ColsWidth[1], headerheight);
            fonts = new System.Drawing.Font("微软雅黑", 10, FontStyle.Bold);

            Gdraw.DrawString("总计", fonts, brushes, new RectangleF(X, Y, ColsWidth[0], headerheight), strFormat);

            X += Convert.ToInt32(ColsWidth[0]);
            Gdraw.DrawString(Total.ToString(), fonts, brushes, new RectangleF(X, Y, ColsWidth[1], headerheight), strFormat);

            #endregion
            ColsIndex.Clear(); ColsWidth.Clear();
            #endregion
            if (CurentRowNum < InvoiceSet.dgvOrder.RowCount)
                e.HasMorePages = true;
            else
            {
                DTrowIndex++;
                if (DTrowIndex < HB_table.Rows.Count)
                {
                    InvoiceSet.dgvOrder.DataSource = getdate.Get_Child_Order(HB_table.Rows[DTrowIndex]["Tid"].ToString());
                    //初始化
                    CurentRowNum = 0; e.HasMorePages = true;
                }
            }
            
            pen.Dispose(); strFormat.Dispose();

7 个解决方案

#1


哦 差点忘了 PrintPage事件里面还有一个InitPrinter(Graphics g)方法

代码如下(求高手帮我看一下 哪里出了问题?):
 private void InitPrinter(Graphics g)
        {
            if (NowPage == 1)
            {
                if (!printDocument1.DefaultPageSettings.Landscape)
                {
                    PaperPageWidth = printDocument1.DefaultPageSettings.PaperSize.Width;
                    PaperPageHeight = printDocument1.DefaultPageSettings.PaperSize.Height;
                }
                else
                {
                    PaperPageHeight = printDocument1.DefaultPageSettings.PaperSize.Width;
                    PaperPageWidth = printDocument1.DefaultPageSettings.PaperSize.Height;
                }

                ////打印纸的上下左右边距
                PaperLeftMargin = printDocument1.DefaultPageSettings.Margins.Left;
                PaperTopMargin = printDocument1.DefaultPageSettings.Margins.Top;
                PaperRightMargin = printDocument1.DefaultPageSettings.Margins.Right;
                PaperBottomMargin = printDocument1.DefaultPageSettings.Margins.Bottom;

                //Page页的内容尺寸.
                ContentWidth = PaperPageWidth;//- PaperLeftMargin - PaperRightMargin;
                ContentHeight = PaperPageHeight;// -PaperTopMargin - PaperBottomMargin;

                dgvWidth = InvoiceSet.dataGridView1.Width;
                dgvHeight = InvoiceSet.dataGridView1.Height;
                if (dgvWidth > ContentWidth)
                    dgvWidth = ContentWidth;
                PageCount = (dgvHeight % ContentHeight == 0) ? dgvHeight / ContentHeight : dgvHeight / ContentHeight + 1;
                X = PaperLeftMargin + (ContentWidth - dgvWidth) / 2;
                Y = PaperTopMargin;

                for (int i = 0; i < InvoiceSet.dataGridView1.Columns.Count; i++)
                {
                    if (InvoiceSet.dataGridView1.Columns[i].Visible == true)
                    {
                        ColsIndex.Add(i);
                        ColsWidth.Add(((float)InvoiceSet.dataGridView1.Columns[i].Width / (float)InvoiceSet.dataGridView1.Width) * (float)dgvWidth);
                        //if (InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("Left") ||
                        //    InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("NotSet")) AlignmentList.Add(StringAlignment.Near);
                        //if (InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("Center"))
                        //    AlignmentList.Add(StringAlignment.Center);
                        //if (InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("Right"))
                        //    AlignmentList.Add(StringAlignment.Far);
                    }
                }
            }
        }

#2


会不会和打印机的纸张大小边距等设置有关?

#3


打印机和预览有时不那么一致,偏移引起的,再加个调整,把左上角的0,0,重新 调整一点点。

#4


检查一下打印位置和比例是否正确,这样的代码看着头晕,应该划分出若干个函数来调用。

#5


我觉得也是打印机的问题,或纸张位置倾斜了吧?可以预留的空间稍微多一些。

#6


引用 5 楼 hanguoji84 的回复:
我觉得也是打印机的问题,或纸张位置倾斜了吧?可以预留的空间稍微多一些。


预留的控件够充足,那我请教一下 怎么获取打印机的对纸张的默认设置啊?初学者不太懂 谢谢!

#7


@ 3楼:
打印机和预览不一致 有什么办法可以避免吗?
我的打印的模板都是设置好了的,没什么问题啊 

#1


哦 差点忘了 PrintPage事件里面还有一个InitPrinter(Graphics g)方法

代码如下(求高手帮我看一下 哪里出了问题?):
 private void InitPrinter(Graphics g)
        {
            if (NowPage == 1)
            {
                if (!printDocument1.DefaultPageSettings.Landscape)
                {
                    PaperPageWidth = printDocument1.DefaultPageSettings.PaperSize.Width;
                    PaperPageHeight = printDocument1.DefaultPageSettings.PaperSize.Height;
                }
                else
                {
                    PaperPageHeight = printDocument1.DefaultPageSettings.PaperSize.Width;
                    PaperPageWidth = printDocument1.DefaultPageSettings.PaperSize.Height;
                }

                ////打印纸的上下左右边距
                PaperLeftMargin = printDocument1.DefaultPageSettings.Margins.Left;
                PaperTopMargin = printDocument1.DefaultPageSettings.Margins.Top;
                PaperRightMargin = printDocument1.DefaultPageSettings.Margins.Right;
                PaperBottomMargin = printDocument1.DefaultPageSettings.Margins.Bottom;

                //Page页的内容尺寸.
                ContentWidth = PaperPageWidth;//- PaperLeftMargin - PaperRightMargin;
                ContentHeight = PaperPageHeight;// -PaperTopMargin - PaperBottomMargin;

                dgvWidth = InvoiceSet.dataGridView1.Width;
                dgvHeight = InvoiceSet.dataGridView1.Height;
                if (dgvWidth > ContentWidth)
                    dgvWidth = ContentWidth;
                PageCount = (dgvHeight % ContentHeight == 0) ? dgvHeight / ContentHeight : dgvHeight / ContentHeight + 1;
                X = PaperLeftMargin + (ContentWidth - dgvWidth) / 2;
                Y = PaperTopMargin;

                for (int i = 0; i < InvoiceSet.dataGridView1.Columns.Count; i++)
                {
                    if (InvoiceSet.dataGridView1.Columns[i].Visible == true)
                    {
                        ColsIndex.Add(i);
                        ColsWidth.Add(((float)InvoiceSet.dataGridView1.Columns[i].Width / (float)InvoiceSet.dataGridView1.Width) * (float)dgvWidth);
                        //if (InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("Left") ||
                        //    InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("NotSet")) AlignmentList.Add(StringAlignment.Near);
                        //if (InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("Center"))
                        //    AlignmentList.Add(StringAlignment.Center);
                        //if (InvoiceSet.dataGridView1.Rows[0].Cells[ColsIndex[i]].Style.Alignment.ToString().Contains("Right"))
                        //    AlignmentList.Add(StringAlignment.Far);
                    }
                }
            }
        }

#2


会不会和打印机的纸张大小边距等设置有关?

#3


打印机和预览有时不那么一致,偏移引起的,再加个调整,把左上角的0,0,重新 调整一点点。

#4


检查一下打印位置和比例是否正确,这样的代码看着头晕,应该划分出若干个函数来调用。

#5


我觉得也是打印机的问题,或纸张位置倾斜了吧?可以预留的空间稍微多一些。

#6


引用 5 楼 hanguoji84 的回复:
我觉得也是打印机的问题,或纸张位置倾斜了吧?可以预留的空间稍微多一些。


预留的控件够充足,那我请教一下 怎么获取打印机的对纸张的默认设置啊?初学者不太懂 谢谢!

#7


@ 3楼:
打印机和预览不一致 有什么办法可以避免吗?
我的打印的模板都是设置好了的,没什么问题啊