public class TriplicatePrint
{
private const int HeadHeight = 14;
private const string FontName = "黑体";
private readonly SolidBrush _drawBrush_Black = new SolidBrush(Color.Black);
private static readonly Font TableFont = new Font(FontName, 8, FontStyle.Regular);//表格字体
private readonly int _yUnit = TableFont.Height * 2;//表格高度
public int TotalNum = 0;//当前要所要打印的记录行数,由计算得到
private int WordMaxNum = 20;//最长可输出的字数
private int twoRowNum = 0;//换行的行数
private bool flag = false;//有没有换行
private int TableFootHeight = 50;//脚注高度
private TriplicateMsg ticketMsg;
private DataRow _dataRow;
private DataTable _dataTable;
private int _firstPrintRecordNumber;//第一页的记录数
private string _headText = string.Empty;
private int _pBottom;//页面下边距
private int _pHeigh;//页面左边距
private int _pTop;//页面上边距
private int _pageRecordNumber;//总记录行数
private PrintDocument _printDocument;
private int _printRecordComplete;//答应成功的条数
private int _printRecordNumber;//每页打印的记录条数
private int _printingPageNumber = 1;//当前打印页
//第一页打印的记录条数
//与列名无关的统计数据行的类目数(如,总计,小计......)
private int _totalPage;//总页数
private int[] _xUnit;//列宽集合
/// <summary>
/// 打印信息
/// </summary>
private string TicketJson;
public TriplicatePrint(string order)
{
TicketJson = order;
}
/// <summary>
/// 执行打印
/// </summary>
public void Print()
{
// 通过多线程操作打印
Thread thread = new Thread(PrintOrder);
thread.IsBackground = true;
thread.Start();
}
/// <summary>
/// 打印普通消费小票
/// </summary>
private void PrintOrder()
{
Thread.Sleep(100);
if (string.IsNullOrEmpty(Setting.TriplicatePrintName))
{
LoggerHelper.Info("请先设置默认打印机!");
return;
}
if (Setting.Triplicate == 0)
{
LoggerHelper.Info("未开启三联单自动打印!");
return;
}
if (Setting.TriplicateTicketCount < 1)
{
LoggerHelper.Info("打印份数小于1!");
return;
}
ticketMsg = JsonConvert.DeserializeObject<TriplicateMsg>(TicketJson);
_dataTable = new DataTable();
if (ticketMsg.template.center != null && ticketMsg.template.center.Any())
{
if (ticketMsg.template.center.Any(it => it.key == "GoodCode"))
{
_dataTable.Columns.Add("商品编号", System.Type.GetType("System.String"));
}
if (ticketMsg.template.center.Any(it => it.key == "Name"))
{
_dataTable.Columns.Add("名称", System.Type.GetType("System.String"));
}
if (ticketMsg.template.center.Any(it => it.key == "Specs"))
{
_dataTable.Columns.Add("规格", System.Type.GetType("System.String"));
}
if (ticketMsg.template.center.Any(it => it.key == "Unit"))
{
_dataTable.Columns.Add("单位", System.Type.GetType("System.String"));
}
if (ticketMsg.template.center.Any(it => it.key == "Price"))
{
_dataTable.Columns.Add("单价", System.Type.GetType("System.String"));
}
}
//else
//{
// _dataTable.Columns.Add("商品编号", System.Type.GetType("System.String"));
// _dataTable.Columns.Add("名称", System.Type.GetType("System.String"));
// _dataTable.Columns.Add("规格", System.Type.GetType("System.String"));
// _dataTable.Columns.Add("单位", System.Type.GetType("System.String"));
// _dataTable.Columns.Add("单价", System.Type.GetType("System.String"));
//}
_dataTable.Columns.Add("数量", System.Type.GetType("System.String"));
_dataTable.Columns.Add("金额", System.Type.GetType("System.String"));
foreach (var item in ticketMsg.detaillist)
{
DataRow dr = _dataTable.NewRow();
if (ticketMsg.template.center != null && ticketMsg.template.center.Any())
{
if (ticketMsg.template.center.Any(it => it.key == "GoodCode"))
{
dr["商品编号"] = item.GoodsCode;
}
if (ticketMsg.template.center.Any(it => it.key == "Name"))
{
dr["名称"] = item.GoodsName;
}
if (ticketMsg.template.center.Any(it => it.key == "Specs"))
{
dr["规格"] = item.SpecsValue;
}
if (ticketMsg.template.center.Any(it => it.key == "Unit"))
{
dr["单位"] = item.Unit;
}
if (ticketMsg.template.center.Any(it => it.key == "Price"))
{
dr["单价"] = item.DiscountMoney;
}
}
//else
//{
// dr["商品编号"] = item.GoodsCode;
// dr["名称"] = item.GoodsName;
// dr["规格"] = item.SpecsValue;
// dr["单位"] = item.Unit;
// dr["单价"] = item.DiscountMoney;
//}
dr["数量"] = item.Number;
dr["金额"] = item.TotalMoney;
_dataTable.Rows.Add(dr);
}
if (ticketMsg.template != null && ticketMsg.template.header != null)
{
foreach (var item in ticketMsg.template.header)
{
_headText += item.value + "|";
}
}
【第二十四篇】C#进行A4打印
最新推荐文章于 2022-12-09 19:53:55 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)