最近一周竟然有2位以前的同事问我在winfor应用程序里面打印怎么搞,所以才有了写这篇文章的打算,索性现在没事就写出来  

在窗体上简单的布局设置一下如图

定义一个Model 我在里面放了属性之外还从写了ToString方法和返回模型的方法。

代码如下

  #region Model
public DateTime CreateDate = DateTime.Today;
public string DocNumber;
public string Title { get; set; }
public string Consignee { get; set; }
public string ProductName { get; set; }
public string ProductLot { get; set; }
public string Specification { get; set; }
public string Factory { get; set; }
public string RegNo { get; set; }
public string PermitNo { get; set; }
public DateTime ExpirationDate { get; set; }
public DateTime DeliveryDate { get; set; }
public string Unit { get; set; }
public int Count { get; set; }
public float Price { get; set; }
public float Money { get; set; }
#endregion private const char Spliter = '|';//分割txt里面的值 public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(CreateDate);
sb.Append(Spliter);
sb.Append(DocNumber);
sb.Append(Spliter);
sb.Append(Title);
sb.Append(Spliter);
sb.Append(Consignee);
sb.Append(Spliter);
sb.Append(ProductName);
sb.Append(Spliter);
sb.Append(ProductLot);
sb.Append(Spliter);
sb.Append(Specification);
sb.Append(Spliter);
sb.Append(Factory);
sb.Append(Spliter);
sb.Append(RegNo);
sb.Append(Spliter);
sb.Append(PermitNo);
sb.Append(Spliter);
sb.Append(ExpirationDate);
sb.Append(Spliter);
sb.Append(DeliveryDate);
sb.Append(Spliter);
sb.Append(Unit);
sb.Append(Spliter);
sb.Append(Count);
sb.Append(Spliter);
sb.Append(Price);
sb.Append(Spliter);
sb.Append(Money);
sb.Append(Spliter);
return sb.ToString();
} public static OutshipDoc FromString(string s)
{
try
{
int i = ; OutshipDoc doc = new OutshipDoc();
string[] pars = s.Split(new char[] { Spliter });
doc.CreateDate = DateTime.Parse(pars[i++]);
doc.DocNumber = pars[i++];
doc.Title = pars[i++];
doc.Consignee = pars[i++];
doc.ProductName = pars[i++];
doc.ProductLot = pars[i++];
doc.Specification = pars[i++];
doc.Factory = pars[i++];
doc.RegNo = pars[i++];
doc.PermitNo = pars[i++];
doc.ExpirationDate = DateTime.Parse(pars[i++]);
doc.DeliveryDate = DateTime.Parse(pars[i++]);
doc.Unit = pars[i++];
doc.Count = int.Parse(pars[i++]);
doc.Price = float.Parse(pars[i++]);
doc.Money = float.Parse(pars[i++]);
return doc;
}
catch (System.Exception ex)
{
return null;
}
}

Model里面的东西已经准备完了,现在就开始前台的代码了

 OutshipDoc doc;

        internal OutshipDoc Doc
{
get { return doc; }
set { doc = value; }
}

以上是model,接下来就是定义公用的

PrintDocument
private System.Drawing.Printing.PrintDocument docToPrint =
new System.Drawing.Printing.PrintDocument();//创建一个PrintDocument的实例

定义好了就用构造函数初始化Model

 public PrintPreview()
{
InitializeComponent();
doc = new OutshipDoc();
doc.Title = "广州市xx医疗器械有限公司送货清单";
doc.Consignee = "阳江人民医院";
doc.ProductName = "银尔通活性银离子抗菌液III型";
doc.DocNumber = "X2012040201";
doc.DeliveryDate = DateTime.Parse("2012/4/2");
doc.Factory = "西安康旺抗菌科技股份有限公司";
doc.Specification = "1200ml/盒";
doc.RegNo = "陕食药监械(准)字2008第2640064号";
doc.PermitNo = "陕食药监械生产许20052286号";
doc.ProductLot = "";
doc.ExpirationDate = DateTime.Parse("2014/04/01");
doc.Unit = "盒";
doc.Count = ;
doc.Price = 39.00F;
doc.Money = 37441.18F; docToPrint.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(docToPrint_PrintPage);
}
//画打印格式
void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int margin = ;
int width = ;
int height = e.PageBounds.Height;
int left = (e.PageBounds.Width - width) / ;
int top = ;
renderDocument(doc, e.Graphics, left, top, width, height); }
public static void renderDocument(OutshipDoc d, Graphics g, int left, int top, int width, int height)
{
width = ; Brush br = Brushes.Black; // 打印抬头
Font ft;
StringFormat sf; ft = new Font("宋体", );
sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.FormatFlags = StringFormatFlags.NoClip; g.DrawString(d.Title, ft, br
, new RectangleF(new PointF(left, top), new SizeF(width , height ))
, sf); string info1 = "收货单位:" + d.Consignee + "   " + "品名:" + d.ProductName;
ft.Dispose();
ft = new Font("宋体", );
g.DrawString(info1, ft, br, new PointF(left, top + )); info1 = "编号:" + d.DocNumber;
g.DrawString(info1, ft, br, new PointF(left + width - , top + )); ft.Dispose();
ft = new Font("宋体", );
info1 = "生产厂家:" + d.Factory;
g.DrawString(info1, ft, br, new PointF(left, top + ));
info1 = "送货日期:" + d.DeliveryDate.Year + "年" + d.DeliveryDate.Month + "月" + d.DeliveryDate.Day + "日";
g.DrawString(info1, ft, br, new PointF(left + width - , top + )); int x = left, y = top + ; Pen pn = Pens.Black;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + , y));
g.DrawLine(pn, new Point(left + + , y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y));
y += ;
g.DrawLine(pn, new Point(left, y), new Point(left + width, y)); int y1 = top + ;
int y2 = top + ;
int y3 = top + ;
g.DrawLine(pn, new Point(x, y1), new Point(left, y3));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y3));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y2));
x += ;
g.DrawLine(pn, new Point(x, y1), new Point(x, y3));
x = left + width;
g.DrawLine(pn, new Point(x, y1), new Point(x, y3)); sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center; ft.Dispose();
ft = new Font("宋体", ); x = left;
g.DrawString("规格", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("产品注册证号", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("医疗器械生产企业许可证号", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("产品批号", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("失效日期", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("单位", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("数量", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("单价", ft, br, new RectangleF(x, y1, , ), sf);
x += ;
g.DrawString("金额", ft, br, new RectangleF(x, y1, , ), sf);
x += ; x = left;
g.DrawString(d.Specification, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.RegNo, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.PermitNo, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.ProductLot, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.ExpirationDate.ToString("yyyyMMdd"), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Unit, ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Count.ToString(), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Price.ToString("#.00"), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ;
g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y1 + , , ), sf);
x += ; x = left;
sf.Alignment = StringAlignment.Near;
g.DrawString("合计:", ft, br, new RectangleF(x, y2, , ), sf);
x += ;
g.DrawString("人民币(大写)" + moneyToString(d.Money), ft, br, new RectangleF(x, y2, , ), sf);
x += ;
x += ;
x += ;
x += ;
x += ;
x += ;
x += ;
sf.Alignment = StringAlignment.Center;
g.DrawString(d.Money.ToString("#.00"), ft, br, new RectangleF(x, y2, , ), sf);
x += ; x = left;
sf.Alignment = StringAlignment.Near;
g.DrawString("收货单位:", ft, br, new RectangleF(x, y3, , ), sf);
x += ;
g.DrawString("送货单位:", ft, br, new RectangleF(x, y3, , ), sf); }
 //货币大写转换
private static string moneyToString(double p)
{
string[] numbers = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "镹"};
string[] unit = new string[] { "元", "拾", "佰", "仟", "万", "拾万", "佰万", "仟万" };
string[] xunit = new string[] { "角", "分" }; int x = (int)p; string s = "";
int i = ;
for (i = ; i < && x > ; i++)
{
if (x % != )
s = numbers[x % ] + unit[i] + s;
x /= ;
}
if (s == "")
s = "零";
if (!s.EndsWith("元"))
s += "元"; double y = p - (int)p;
if (y != 0.0f)
{
y *= ;
if ((int)y != )
s += numbers[(int)y] + "角";
y = y - (int)y;
y *= ;
if ((int)y != )
s += numbers[(int)y] + "分";
} if (s.EndsWith("元"))
s += "整";
return s;
}

输出的格式浏览就已经写完最好就差真正的打印了

private void btnPrint_Click(object sender, EventArgs e)
{
PrintDialog pd = new PrintDialog();
pd.Document = docToPrint;
if (pd.ShowDialog() == DialogResult.OK)
{
docToPrint.Print();
}
}

到此就已经写完了看看效果了,如图下面就是最后的效果

最新文章

  1. Web之路笔记之一
  2. nginx事件模块分析(一)
  3. jquery on off 方法
  4. mysql_fetch_row,mysql_fetch_array,mysql_fetch_assoc的区别
  5. javascript类继承系列二(原型链)
  6. 安卓图表引擎AChartEngine(六) - 框架源码结构图
  7. Gradle 1.12用户指南翻译——第四十九章. Build Dashboard 插件
  8. Win10系列:C#应用控件基础6
  9. Docker上运行MySQL服务
  10. linux 安装 Python
  11. 一文读懂 深度强化学习算法 A3C (Actor-Critic Algorithm)
  12. 安装 gradle
  13. 图文转化(Alpha)版使用说明
  14. 给MySQL中某表增加一个新字段,设为主键值为自动增长。
  15. iOS-自定义起始时间选择器视图
  16. JS的全局函数eval解析JSON字符串
  17. Oracle 通用存储过程
  18. WSDL-学习总结
  19. Java将科学计数法数据转为字符串
  20. php简易实现计划任务

热门文章

  1. 如何访问google?
  2. Centos 内存释放
  3. Codeforces 1144F(二分染色)
  4. Git命令---递归克隆
  5. hihocoder1079 离散化
  6. Android自定义zxing扫描界面的实现
  7. Hello Shell
  8. 2019年今日头条机试_JAVA后台岗_第一题
  9. SQL数据库移植到ARM板步骤
  10. 为什么要使用spl_autoload_unregister