C#打印绘图的实现方法

 更新时间:2020年6月25日 11:32  点击:2364

本文实例讲述了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#程序设计有所帮助。

[!--infotagslink--]

相关文章

  • C#实现简单的登录界面

    我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
  • 浅谈C# 字段和属性

    这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • C#连接SQL数据库和查询数据功能的操作技巧

    本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
  • C#实现简单的Http请求实例

    这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • C#中new的几种用法详解

    本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
  • 使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)

    这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#开发Windows窗体应用程序的简单操作步骤

    这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#和JavaScript实现交互的方法

    最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • 轻松学习C#的基础入门

    轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
  • HTTP 408错误是什么 HTTP 408错误解决方法

    相信很多站长都遇到过这样一个问题,访问页面时出现408错误,下面一聚教程网将为大家介绍408错误出现的原因以及408错误的解决办法。 HTTP 408错误出现原因: HTT...2017-01-22
  • C#变量命名规则小结

    本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
  • C#绘制曲线图的方法

    这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • c#自带缓存使用方法 c#移除清理缓存

    这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
  • Android子控件超出父控件的范围显示出来方法

    下面我们来看一篇关于Android子控件超出父控件的范围显示出来方法,希望这篇文章能够帮助到各位朋友,有碰到此问题的朋友可以进来看看哦。 <RelativeLayout xmlns:an...2016-10-02