110,536
社区成员
发帖
与我相关
我的任务
分享
打印如果只有一行的话调整好了,但是如果是多行的话,商品名称汉字有的多有的少就调不齐了,想过补空格
但是得计算有点费劲,请问有简单的办法没有?
sb.AppendLine(string.Format("单据编号:{0}",dr["BillNo"]));
sb.AppendLine(string.Format("{0,-15}{1,23}",ClubName + "(客户联)","充值"));
sb.AppendLine("================================================");
sb.AppendLine(string.Format("{0,-19}{1,18}", "客户编号:" + CustNo, "客户名称:" + CustName));
sb.AppendLine("------------------------------------------------");
sb.AppendLine(string.Format("{0}{1}{2}{3}{4}", "单据编号 ", "充值时间 ", "账户余额 ", "充值金额 ", " 账户金额"));
sb.AppendLine(string.Format("{0,-10}{1,-9:yy-MM-dd}{2,-9}{3,-9}{4,10}", dr["BillNo"], dr["ResultDate"], dr["RemainAccount"].ToString().Replace(".00", ""), dr["ResultMoney"].ToString().Replace(".00", ""), dr["Account"].ToString().Replace(".00", "")));
sb.AppendLine("------------------------------------------------");
sb.AppendLine(string.Format("打印日期:{0:yyyy-MM-dd HH:mm}------操作员:{1,5}",DateTime.Now,dr["Operator"]));
#region Print_CashResult
public static void Print_CashResult(DataTable dt)
{
SafeFileHandle sfh = null;
string instruction = null;
IntPtr ptr = CreateFile(PrintPort, FileAccess.Write, FileShare.Write, 0, FileMode.Open, 0, IntPtr.Zero);
if (ptr.ToInt32() == -1)
{
MessageBox.Show(string.Format("连接打印机:{0} 失败,请检查连接端口是否正确。", PrintPort ));
return;
}
sfh = new SafeFileHandle(ptr, true);
StreamWriter sw = new StreamWriter( new FileStream( sfh, FileAccess.Write), System.Text.Encoding.Default);
StringBuilder sb = new StringBuilder();
DataRow dr = dt.Rows[0]; //一次充值只有1行
sb.AppendLine(string.Format("单据编号:{0}",dr["BillNo"]));
sb.AppendLine(string.Format("{0,-15}{1,23}",ClubName + "(客户联)","充值"));
sb.AppendLine("================================================");
sb.AppendLine(string.Format("{0,-19}{1,18}", "客户编号:" + CustNo, "客户名称:" + CustName));
sb.AppendLine("------------------------------------------------");
sb.AppendLine(string.Format("{0}{1}{2}{3}{4}", "单据编号 ", "充值时间 ", "账户余额 ", "充值金额 ", " 账户金额"));
sb.AppendLine(string.Format("{0,-10}{1,-9:yy-MM-dd}{2,-9}{3,-9}{4,10}", dr["BillNo"], dr["ResultDate"], dr["RemainAccount"].ToString().Replace(".00", ""), dr["ResultMoney"].ToString().Replace(".00", ""), dr["Account"].ToString().Replace(".00", "")));
sb.AppendLine("------------------------------------------------");
sb.AppendLine(string.Format("打印日期:{0:yyyy-MM-dd HH:mm}------操作员:{1,5}",DateTime.Now,dr["Operator"]));
sw.WriteLine(sb); //第1次 打客户联
instruction = Convert.ToChar(29) + instruction + Convert.ToChar(86) + instruction + Convert.ToChar(66) + instruction + Convert.ToChar(100); //切纸
sw.WriteLine(instruction); //切纸
sw.WriteLine(sb.Replace("(客户联)","(留底联)")); //第2次 打(留底联)
sw.WriteLine(instruction); //切纸
sw.Close();
CloseHandle(ptr);
}
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string passWord = "1234";
string fullnumber = "9999";
System.Drawing.Printing.PrintDocument pdBays = new System.Drawing.Printing.PrintDocument();
public Form1()
{
InitializeComponent();
pdBays.PrintPage += new PrintPageEventHandler(pbay_PrintPage);
pdBays.DefaultPageSettings.PaperSize = new PaperSize("",600, 800);
}
private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 0; i++)
{
pdBays.Print();
}
}
public void pbay_PrintPage(object sender, PrintPageEventArgs ev)
{
Graphics dc = ev.Graphics;
Pen pen = new Pen(Color.Black, 1);
Font drawFont = new Font("仿宋_GB2312", 12);
SolidBrush drawBrush = new SolidBrush(Color.Black);
PointF drawPoint = new PointF(30, 200);
//这句可以隐藏“正在打印”窗体
pdBays.PrintController = new System.Drawing.Printing.StandardPrintController();
dc.DrawString("\n密码:" + passWord + "\n充值券编号:" + fullnumber, drawFont, drawBrush, drawPoint);
}
////网上例子
//private void btnPrint_Click(object sender, EventArgs e)
//{
// //打印预览
// //PrintPreviewDialog ppd = new PrintPreviewDialog();
// PrintDocument pd = new PrintDocument();
// //设置边距
// Margins margin = new Margins(20, 20, 20, 20);
// pd.DefaultPageSettings.Margins = margin;
// ////纸张设置默认
// //PaperSize pageSize = new PaperSize("First custom size", 800, 600);
// //pd.DefaultPageSettings.PaperSize = pageSize;
// //打印事件设置
// pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
// //ppd.Document = pd;
// //ppd.ShowDialog();
// try
// {
// pd.Print();
// }
// catch (Exception ex)
// {
// }
//}
}
}