C#打印类
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Drawing;
namespace WinAppPrint
{
/// <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;
}
}
}
C#打印类的更多相关文章
- [asp.net]c# winform打印类
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using ...
- -XX:-PrintClassHistogram 按下Ctrl+Break后,打印类的信息
-XX:+PrintClassHistogram –按下Ctrl+Break后,打印类的信息: num #instances #bytes class name ------ ...
- python 以单例模式封装logging相关api实现日志打印类
python 以单例模式封装logging相关api实现日志打印类 by:授客QQ:1033553122 测试环境: Python版本:Python 2.7 实现功能: 支持自由配置,如下lo ...
- __str__被print函数调用,目的是打印类的内容到屏幕上
# -*- coding: utf-8 -*- #python 27 #xiaodeng #__str__被print函数调用,目的是打印类的内容到屏幕上 class APIError(): def ...
- C#Lpt端口打印类的操作浅析
C#LPT端口打印类的操作是什么呢?首先让我们看看什么是LPT端口(打印机专用)?LPT端口是一种增强了的双向并行传输接口,在USB接口出现以前是扫描仪,打印机最常用的接口.最高传输速度为1.5Mbp ...
- Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件
Android日志打印类LogUtils,能够定位到类名,方法名以及出现错误的行数并保存日志文件 在开发中,我们常常用打印log的方式来调试我们的应用.在Java中我们常常使用方法System.out ...
- JAVA打印类(带预览)
package tool; import java.awt.*; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; ...
- python 打印类的属性、方法
打印变量db的类(class):[root@fuel ~]# pythonPython 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)[GCC 4.4.7 2012 ...
- ZPL条码打印类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
随机推荐
- windows系统共享设置最顺的一次
1.关掉防火墙 2.控制面板\网络和 Internet\网络和共享中心\高级共享设置 3.设置要共享的文件夹属性
- Android 数据存储(XML解析)
在androd手机中处理xml数据时很常见的事情,通常在不同平台传输数据的时候,我们就可能使用xml,xml是与平台无关的特性,被广泛运用于数据通信中,那么在android中如何解析xml文件数据 ...
- SDL 威胁建模工具入门 threat modeling tool
http://msdn.microsoft.com/zh-cn/magazine/dd347831.aspx threat modeling tool 威胁建模工具 minifuzz 文件模糊工具 c ...
- SecureCRT图形界面
一般的咱们用这个工具连接服务器啥的都是命令行模式的,其实他也可以连接图形界面 一.Xmanager SecureCRT连接图形界面的话必须要有 Xmanager 工具的配合才行,SecureCRT显示 ...
- javafx 表格某一列设置未复选框
1.表格要设置成可编辑.2.对列进行如下设置.列定义: @FXMLprivate TableColumn<Hole, Boolean> id_colCheck; 列设置id_colChec ...
- MySQL索引优化案例浅析
MySQL是关系型数据库的一种,查询功能强,数据一致性高,数据安全性高,支持二级索引.但是性能比起非关系型数据库稍弱,特别是百万级以上的数据,很容易出现查询慢的现象.这时候要分析慢的原因,一般情况下是 ...
- SpringBoot-基于Maven工程使用SpringBoot
Spring Boot SpringBoot是一个社区反馈推动的项目.SpringBoot可以说是至少五年来Spring乃至整个Java社区最有影响力的项目之一.SpringBoot主要包括以下特性: ...
- (2.1)备份与还原--sql server文件的概念及操作
概述:sql server是以文件形式存储数据与日志 1.数据文件 sql server数据文件分为2类 (1)主数据库文件 主数据库文件包含数据库的启动信息.系统对象,并指向数据库的其他文件(从数据 ...
- 015-HQL中级5-hive创建索引
索引是hive0.7之后才有的功能,创建索引需要评估其合理性,因为创建索引也是要磁盘空间,维护起来也是需要代价的 创建索引 hive> create index [index_studentid ...
- spring中的缓存--Caching
1.spring从3.1开始支持缓存功能.spring 自带的缓存机制它只在方法上起作用,对于你使用其他持久化层的框架来讲,是没有影响的,相对来讲这种缓存方式还是不错的选择. 2.提供缓存的接口:or ...