C#打印绘图的实现方法
本文实例讲述了c#打印绘图的实现方法。分享给大家供大家参考。具体实现方法如下:
string drawstring = "";
font drawfont = null;
solidbrush drawbrush = null;
float x = 0f;
float y = 0f;
stringformat drawformat = new stringformat();
string test = "";
public string test
{
get { return test; }
set { test = value; }
}
public form1()
{
initializecomponent();
//设置纸张大小
papersize papersize = new papersize("dataorder", 1023, 614);
printdocument1.defaultpagesettings.papersize = papersize;
}
//打印
private void button1_click(object sender, eventargs e)
{
printdocument1.printpage += new printpageeventhandler(myprintdoc_printpage);
try
{
printpreviewdialog1.document = printdocument1;
printpreviewdialog1.formborderstyle = formborderstyle.fixed3d;
printpreviewdialog1.showdialog();
printdocument1.print();
}
catch
{
messagebox.show("请安装打印机", "提示", messageboxbuttons.ok, messageboxicon.error);
}
}
protected void myprintdoc_printpage(object sender, printpageeventargs e)
{
//字体 颜色 格式 坐标
drawfont = new font("arial", 8);
drawbrush = new solidbrush(color.black);
x = 0f;
y = 0f;
drawformat.formatflags = stringformatflags.nowrap;
//块数
int num = 6;
float weightall = 12.600f;
//标题
//标题第一行
string title = "钢板入库计量单";
string billcode = "吊号:" + "20080505170";
string classtype = "班别:" + "乙";
string type1 = "类型:" + "定轧";
string ordernum = "记录单号:" + "qw-y14-02-06";
//标题第二行
string arrivestation = "到站:";
string spline = "专用线:";
string saleno = "销售订单号:";
//标题第三行
string acceptname = "收货单位:" + "xxxx/12*2438*9144收二支";
string time = datetime.now.year.tostring() + "年" + datetime.now.month.tostring().padleft(2,'0')+"月"+datetime.now.day.tostring()+"日";
//表格下数据
string boardnum = "合计:" + num.tostring() + " 块 " + weightall.tostring()+" 吨 ";
string checkman1 = "检查员:" + "";
string mename = "计量员:"+"xx";
string checkman2 = "核对员:" + "";
//线条长度
pen line = new pen(drawbrush, 1);
//绘图--字的位置
//绘图--总标题
e.graphics.drawstring(title, drawfont, drawbrush, 400, 45, drawformat);
//绘图--标题--第一行
//吊号
e.graphics.drawstring(billcode, drawfont, drawbrush, 125,83, drawformat);
//班别
e.graphics.drawstring(classtype, drawfont, drawbrush, 354, 83, drawformat);
//类型
e.graphics.drawstring(type1, drawfont, drawbrush, 500, 83, drawformat);
//记录单号
e.graphics.drawstring(ordernum, drawfont, drawbrush, 685, 83, drawformat);
//绘图--标题--第二行
//到站
e.graphics.drawstring(arrivestation, drawfont, drawbrush, 125, 106, drawformat);
//专用线
e.graphics.drawstring(spline, drawfont, drawbrush, 354, 106, drawformat);
//销售订单号
e.graphics.drawstring(saleno, drawfont, drawbrush, 500, 106, drawformat);
//绘图--标题--第三行
//收货单位
e.graphics.drawstring(acceptname, drawfont, drawbrush, 125, 129, drawformat);
//时间
e.graphics.drawstring(time, drawfont, drawbrush, 685, 129, drawformat);
//绘图--表格下数据
//合计
e.graphics.drawstring(boardnum, drawfont, drawbrush, 125, 568, drawformat);
//检查员
e.graphics.drawstring(checkman1, drawfont, drawbrush, 400, 568, drawformat);
//计量员
e.graphics.drawstring(mename, drawfont, drawbrush, 550, 568, drawformat);
//核对员
e.graphics.drawstring(checkman2, drawfont, drawbrush, 700, 568, drawformat);
//绘图 表格
float leftbianju = 120;
float topbianju = 152;
float tablewidth = 770;
float tableheight = 393;
float cellwidth = 55;
float cellwidth1 = 110;
float cellheigh = 0f;
//绘图--线的位置 外边矩形
//横
e.graphics.drawline(line, leftbianju, topbianju,leftbianju+tablewidth,topbianju);
e.graphics.drawline(line, leftbianju, topbianju + tableheight, leftbianju + tablewidth, topbianju + tableheight);
//竖
e.graphics.drawline(line, leftbianju, topbianju, leftbianju, topbianju+tableheight);
e.graphics.drawline(line, leftbianju + tablewidth, topbianju, leftbianju + tablewidth, topbianju + tableheight);
//e.graphics.drawline(line, 15, 0, 15, 614);
//绘图--内部竖线的位置
y = topbianju+tableheight;
x = leftbianju + cellwidth;
e.graphics.drawline(line, x, topbianju, x,y );
x= leftbianju + cellwidth + cellwidth1;
e.graphics.drawline(line,x , topbianju, x, y);
x = leftbianju + cellwidth + cellwidth1 * 2;
e.graphics.drawline(line, x, topbianju, x, y);
x = leftbianju + cellwidth + cellwidth1 * 3;
e.graphics.drawline(line, x, topbianju, x, y);
x = leftbianju + cellwidth * 2 + cellwidth1 * 3;
e.graphics.drawline(line, x, topbianju, x, y);
x = leftbianju + cellwidth * 3 + cellwidth1 * 3;
e.graphics.drawline(line, x, topbianju, x, y);
x = leftbianju + cellwidth * 4 + cellwidth1 * 3;
e.graphics.drawline(line, x, topbianju, x, y);
x = leftbianju + cellwidth * 5 + cellwidth1 * 3;
e.graphics.drawline(line, x, topbianju, x, y);
x = leftbianju + cellwidth * 6 + cellwidth1 * 3;
e.graphics.drawline(line, x, topbianju, x, y);
x = leftbianju + cellwidth * 7 + cellwidth1 * 3;
e.graphics.drawline(line, x, topbianju, x, y);
//绘图--内部横线的位置
//根据块数计算表格高度
if (num != 0)
cellheigh = tableheight / (float)num;
else
cellheigh = 0;
//绘图--内部横线的位置
for (int i = 1; i < num; i++)
{
if (i == 1)
{
//数据
string str = "";
str = "序号";
x = leftbianju + cellwidth / 5;
y = topbianju + cellheigh / 3;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "卡片编号";
x = leftbianju + cellwidth + cellwidth1 / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "炉号";
x = leftbianju + cellwidth + cellwidth1 + cellwidth1 / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "物料编码";
x = leftbianju + cellwidth + cellwidth1*2 + cellwidth1 / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "牌号";
x = leftbianju + cellwidth + cellwidth1*3 + cellwidth / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "厚度";
x = leftbianju + cellwidth * 2 + cellwidth1 * 3 + cellwidth / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "宽度";
x = leftbianju + cellwidth * 3 + cellwidth1 * 3 + cellwidth / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "长度";
x = leftbianju + cellwidth * 4 + cellwidth1 * 3 + cellwidth / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "重量";
x = leftbianju + cellwidth * 5 + cellwidth1 * 3 + cellwidth / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "级别";
x = leftbianju + cellwidth * 6 + cellwidth1 * 3 + cellwidth / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
str = "偏差";
x = leftbianju + cellwidth * 7 + cellwidth1 * 3 + cellwidth / 5;
e.graphics.drawstring(str, drawfont, drawbrush, x, y, drawformat);
}
//横线
y = topbianju + cellheigh * i;
e.graphics.drawline(line, leftbianju, y, leftbianju + tablewidth, y);
}
e.hasmorepages = false;
}
运行效果如下图所示:
希望本文所述对大家的c#程序设计有所帮助。