using System;

using System.Collections.Generic;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Printing;

using System.Drawing;

namespace Lds2013

{

/**//// <summary>

/// 打印类

/// </summary>

public class Printer

{

private DataGridView dataview;

private PrintDocument printDoc;

//打印有效区域的宽度

int width;

int height;

int columns;

double Rate;

bool hasMorePage = false;

int currRow = 0;

int rowHeight = 20;

//打印页数

int PageNumber;

//当前打印页的行数

int pageSize = 20;

//当前打印的页码

int PageIndex;

private int PageWidth; //打印纸的宽度

private int PageHeight; //打印纸的高度

private int LeftMargin; //有效打印区距离打印纸的左边大小

private int TopMargin;//有效打印区距离打印纸的上面大小

private int RightMargin;//有效打印区距离打印纸的右边大小

private int BottomMargin;//有效打印区距离打印纸的下边大小

int rows;

/**//// <summary>

/// 构造函数

/// </summary>

/// <param name="dataview">要打印的DateGridView</param>

/// <param name="printDoc">PrintDocument用于获取打印机的设置</param>

public Printer(DataGridView dataview, PrintDocument printDoc)

{

this.dataview = dataview;

this.printDoc = printDoc;

PageIndex = 0;

//获取打印数据的具体行数

this.rows = dataview.RowCount;

this.columns = dataview.ColumnCount;

//判断打印设置是否是横向打印

if (!printDoc.DefaultPageSettings.Landscape)

{

PageWidth = printDoc.DefaultPageSettings.PaperSize.Width;

PageHeight = printDoc.DefaultPageSettings.PaperSize.Height;

}

else

{

PageHeight = printDoc.DefaultPageSettings.PaperSize.Width;

PageWidth = printDoc.DefaultPageSettings.PaperSize.Height;

}

LeftMargin = printDoc.DefaultPageSettings.Margins.Left;

TopMargin = printDoc.DefaultPageSettings.Margins.Top;

RightMargin = printDoc.DefaultPageSettings.Margins.Right;

BottomMargin = printDoc.DefaultPageSettings.Margins.Bottom;

height = PageHeight - TopMargin - BottomMargin - 2;

width = PageWidth - LeftMargin - RightMargin - 2;

double tempheight = height;

double temprowHeight = rowHeight;

while (true)

{

string temp = Convert.ToString(tempheight / Math.Round(temprowHeight, 3));

int i = temp.IndexOf('.');

double tt = 100;

if (i != -1)

{

tt = Math.Round(Convert.ToDouble(temp.Substring(temp.IndexOf('.'))), 3);

}

if (tt <= 0.01)

{

rowHeight = Convert.ToInt32(temprowHeight);

break;

}

else

{

temprowHeight = temprowHeight + 0.01;

}

}

pageSize = height / rowHeight;

if ((rows + 1) <= pageSize)

{

pageSize = rows + 1;

PageNumber = 1;

}

else

{

PageNumber = rows / (pageSize - 1);

if (rows % (pageSize - 1) != 0)

{

PageNumber = PageNumber + 1;

}

}

}

/**//// <summary>

/// 初始化打印

/// </summary>

private void InitPrint()

{

PageIndex = PageIndex + 1;

if (PageIndex == PageNumber)

{

hasMorePage = false;

if (PageIndex != 1)

{

pageSize = rows % (pageSize - 1) + 1;

}

}

else

{

hasMorePage = true;

}

}

//打印头

private void DrawHeader(Graphics g)

{

Font font = new Font("宋体", 12, FontStyle.Bold);

int temptop = (rowHeight / 2) + TopMargin + 1;

int templeft = LeftMargin + 1;

for (int i = 0; i < this.columns; i++)

{

string headString = this.dataview.Columns[i].HeaderText;

float fontHeight = g.MeasureString(headString, font).Height;

float fontwidth = g.MeasureString(headString, font).Width;

float temp = temptop - (fontHeight) / 3;

g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));

templeft = templeft + (int)(this.dataview.Columns[i].Width / Rate) + 1;

}

}

//画表格

private void DrawTable(Graphics g)

{

Rectangle border = new Rectangle(LeftMargin, TopMargin, width, (pageSize) * rowHeight);

g.DrawRectangle(new Pen(Brushes.Black, 2), border);

for (int i = 1; i < pageSize; i++)

{

if (i != 1)

{

g.DrawLine(new Pen(Brushes.Black, 1), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));

}

else

{

g.DrawLine(new Pen(Brushes.Black, 2), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));

}

}

//计算出列的总宽度和打印纸比率

Rate = Convert.ToDouble(GetDateViewWidth()) / Convert.ToDouble(width);

int tempLeft = LeftMargin + 1;

int endY = (pageSize) * rowHeight + TopMargin;

for (int i = 1; i < columns; i++)

{

tempLeft = tempLeft + 1 + (int)(this.dataview.Columns[i - 1].Width / Rate);

g.DrawLine(new Pen(Brushes.Black, 1), new Point(tempLeft, TopMargin), new Point(tempLeft, endY));

}

}

/**//// <summary>

/// 获取打印的列的总宽度

/// </summary>

/// <returns></returns>

private int GetDateViewWidth()

{

int total = 0;

for (int i = 0; i < this.columns; i++)

{

total = total + this.dataview.Columns[i].Width;

}

return total;

}

//打印行数据

private void DrawRows(Graphics g)

{

Font font = new Font("宋体", 12, FontStyle.Regular);

int temptop = (rowHeight / 2) + TopMargin + 1 + rowHeight;

for (int i = currRow; i < pageSize + currRow - 1; i++)

{

int templeft = LeftMargin + 1;

for (int j = 0; j < columns; j++)

{

string headString = this.dataview.Rows[i].Cells[j].Value.ToString();

float fontHeight = g.MeasureString(headString, font).Height;

float fontwidth = g.MeasureString(headString, font).Width;

float temp = temptop - (fontHeight) / 3;

while (true)

{

if (fontwidth <= (int)(this.dataview.Columns[j].Width / Rate))

{

break;

}

else

{

headString = headString.Substring(0, headString.Length - 1);

fontwidth = g.MeasureString(headString, font).Width;

}

}

g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));

templeft = templeft + (int)(this.dataview.Columns[j].Width / Rate) + 1;

}

temptop = temptop + rowHeight;

}

currRow = pageSize + currRow - 1;

}

/**//// <summary>

/// 在PrintDocument中的PrintPage方法中调用

/// </summary>

/// <param name="g">传入PrintPage中PrintPageEventArgs中的Graphics</param>

/// <returns>是否还有打印页 有返回true,无则返回false</returns>

public bool Print(Graphics g)

{

InitPrint();

DrawTable(g);

DrawHeader(g);

DrawRows(g);

//打印页码

string pagestr = PageIndex + " / " + PageNumber;

Font font = new Font("宋体", 12, FontStyle.Regular);

g.DrawString(pagestr, font, Brushes.Black, new PointF((PageWidth / 2) - g.MeasureString(pagestr, font).Width, PageHeight - (BottomMargin / 2) - g.MeasureString(pagestr, font).Height));

//打印查询的功能项名称

string temp = dataview.Tag.ToString() + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");

g.DrawString(temp, font, Brushes.Black, new PointF(PageWidth - 5 - g.MeasureString(temp, font).Width, PageHeight - 5 - g.MeasureString(temp, font).Height));

return hasMorePage;

}

}

}

[asp.net]c# winform打印类的更多相关文章

  1. WinForm打印

    WinForm打印要用打印控件: PageSetupDialog:打印设置对话框 PrintDialog:打印对话框 PrintDocument:要打印的对象,非常重要 PrintPreviewCon ...

  2. ASP无惧上传类不能上传中文双引号文件及ASP函数InStr存在bug

    ASP无惧上传类不能上传中文双引号文件及ASP函数InStr存在bug 近日发现eWebEditor V2.8 asp 版本上传文件文件名不能包含中文双引号,发现eWebEditor使用ASP“无惧上 ...

  3. c# winform打印excel(使用NPOI+Spire.xls+PrintDocument直接打印excel)

    前言 c#做winform程序要求生成并打印Excel报告,为了不安装Office相应组件,我选择了NPOI来生成Excel报告,用winform的PrintDocument控件来触发打印操作,而难点 ...

  4. ASP.NET 的IP帮助类

    个人网站地址: https://www.lesg.cn/netdaima/net/2016-239.html ASP.NET 的IP帮助类 在Web开发中会出现需要调用客户IP的方法: 一般调用方法就 ...

  5. -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息

    -XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num     #instances         #bytes  class name ------ ...

  6. 一点ASP.NET MVC Html.Helper类的方法

    一点ASP.NET MVC Html.Helper类 这里就只写一个Html.ActionLink()和Html.DropdownList(). Html.ActionLink()里有三个参数,第一个 ...

  7. 在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table)

    原文:在ASP.NET2.0里打印网页指定的内容(比如打印网页里的一个Table) 打印指定内容: <html> <head> <script   type= " ...

  8. python 以单例模式封装logging相关api实现日志打印类

    python 以单例模式封装logging相关api实现日志打印类   by:授客QQ:1033553122 测试环境: Python版本:Python 2.7   实现功能: 支持自由配置,如下lo ...

  9. __str__被print函数调用,目的是打印类的内容到屏幕上

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #__str__被print函数调用,目的是打印类的内容到屏幕上 class APIError(): def ...

随机推荐

  1. Kruskal算法

    1.基本思想:设无向连通网为G=(V, E),令G的最小生成树为T=(U, TE),其初态为U=V,TE={ },然后,按照边的权值由小到大的顺序,考察G的边集E中的各条边.若被考察的边的两个顶点属于 ...

  2. window 下如何安装ghost博客

    1.安装nodejs # Node v0.12.x and v4.2+ LTS - supported 我本地安装的是4.2 安装其他版本可能提示系统不兼容 2.安装mysql 3.安装bower 4 ...

  3. jquery.easyui代码详解,和遇到的问题,提供大家在使用的时候少走弯路(一)

    初次使用jquery.easyui这个东东,虽然简单,但还是很费力的去研究了一下使用,在使用过程中遇到的问题,下面代码会详细的注释到 引用的文件jquery.min.js              j ...

  4. Android手机刷机失败的自救方法

    刷机对于一些android手机的高级用户来说已经是家常便饭了,很多新手也都跟着教程轻松了学会刷机.升级系统,也都开始经常在网上搜罗一些自制的系统进行刷机,体验新系统带来的新感觉.但是有句古话叫常在河边 ...

  5. 页面无法加载main.css

    html应该用append,不是html,会覆盖掉文件

  6. 安装Fedora 24后必要的设置

    安装Fedora 24后必要的设置 导读 Fedora 是一个 Linux 发行版,是一款由全球社区爱好者构建的面向日常应用的快速.稳定.强大的操作系统.它允许任何人自由地使用.修改和重发布,无论现在 ...

  7. 手把手教你配置UltraEdit对Oracle的PLSQL着色

    http://hi.baidu.com/kingbridge/blog/item/94e225ad5fad4b194b36d60d.html   UltraEdit-32 12.1版本配置默认文件显示 ...

  8. JSWindow对象

    Window 对象 Window 对象表示浏览器中打开的窗口. 如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 window 对象,并为每个框架创建一个额外 ...

  9. Dos学习笔记(3)attrib命令

    今天和昨天一直在摸索这个命令觉得这个命令为什么改变不了文件夹的属性, 因为我试着用attrib +r /s 去修改子文件夹的时候发现没用,然后如果输入 attrib +r /d 又提示说/d需要和/s ...

  10. android之xmlpullparse解析器

    Pull解析和Sax解析很相似,都是轻量级的解析,在Android的内核中已经嵌入了Pull,所以我们不需要再添加第三方jar包来支持Pull.Pull解析和Sax解析不一样的地方有(1)pull读取 ...