C# 简单票据功能打印如何实现

代码导航 其他问答 1

我想在一张纸质支付凭证模板上面对应的地方打印上相应的数据,请问如何实现。如果用Graphics或 简单做个panel的话 如何确定打印数据的位置。或者还有更好的办法。

回复

共1条回复 我来回复
  • 源码项目助手
    这个人很懒,什么都没有留下~
    评论
      /// <summary>
        /// 小票打印
        /// </summary>
        public class PosPrint
        {
            private System.Windows.Forms.PrintPreviewDialog printv_pos = null;
            private System.Drawing.Printing.PrintDocument printd_pos = null;
            public static string strContent = string.Empty;
            private string printContent = string.Empty;
    
            public PosPrint(string printContent)
            {
                this.printv_pos = new System.Windows.Forms.PrintPreviewDialog();
                this.printd_pos = new System.Drawing.Printing.PrintDocument();
                this.printv_pos.AutoScrollMargin = new System.Drawing.Size(0, 0);
                this.printv_pos.AutoScrollMinSize = new System.Drawing.Size(0, 0);
                this.printv_pos.ClientSize = new System.Drawing.Size(500, 200);
                this.printv_pos.Document = this.printd_pos;
                this.printv_pos.Enabled = true;
                this.printv_pos.Name = "printPreviewDialog1";
                this.printv_pos.Visible = false;
                this.printd_pos.DocumentName = "社区活动积分小票";
                this.printd_pos.OriginAtMargins = true;
                this.printd_pos.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printd_pos_PrintPage);
    
                this.printContent = printContent;
            }
    
            private int getYc(double cm)
            {
                return (int)(cm / 25.4) * 100;
            }
    
            public void print_view(IWin32Window win,int x,int y)
            {
    
                this.printd_pos.PrintController = new System.Drawing.Printing.StandardPrintController();
                this.printd_pos.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printd_pos_PrintPage);
    
                //设置边距
                Margins margins = new Margins(45, 20, 50, 20);
                this.printd_pos.DefaultPageSettings.Margins = margins;
                this.printd_pos.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("First custom size", getYc(x), y);
                //printv_pos.PrintPreviewControl.AutoZoom = false;
                //printv_pos.PrintPreviewControl.Zoom = 1;
                //this.printv_pos.ShowDialog(win);
                try
                {
                    printd_pos.Print();
                }
                catch
                {
                    printd_pos.PrintController.OnEndPrint(printd_pos, new PrintEventArgs());
                }
            }
            /// <summary>
            /// 打印内容
            /// </summary>
            public string GetPrintStr(string printContent)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(CommonClass.config.PrintHeader + "\n");
                sb.Append("日  期:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\n");
                sb.Append("==============================\n");
                sb.Append(printContent);
                sb.Append("------------------------------\n");
                sb.Append("操作员:" + CommonClass.loginEntity.O_OperatorName + "\n");
                string myfoot = string.Format("{0}     \n", CommonClass.config.PrintFoot);
                sb.Append(myfoot);
                sb.Append("\r\n小勇软件工作室提供技术支持");
                return sb.ToString();
            }
            private void printd_pos_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                Graphics g = e.Graphics;
                string strFile = GetPrintStr(this.printContent);
                Font ft = new Font("宋体", 9.0F, FontStyle.Regular);
                Point pt = new Point(0, 0);
                g.DrawString(strFile, ft, new SolidBrush(Color.Black), pt);
            }
        }
    
    0条评论

发表回复

登录后才能评论