kawun
2016-09-10 00:38
采纳率: 57.1%
浏览 2.9k
已采纳

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

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

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 张云勇 2016-09-13 06:44
    已采纳
      /// <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);
            }
        }
    
    已采纳该答案
    打赏 评论
  • 牧云说 2016-09-10 01:03

    利用table布局进行打印 可以内嵌js

    打赏 评论

相关推荐 更多相似问题