时间:2021-01-12 13:17:08 | 栏目:.NET代码 | 点击:次
所以对于应用层用着还不是很方便。最近做一个项目顺便就封装了一个调用默认打印机的类。虽说有几个小bug,但对于目前来说,已经满足需求了。以后不够了在来升级吧。
1,关于打印上下左右边距和纸张的高宽。以往都把这些写死到代码里面。既然是调用默认打印机,打印机的型号自然有差异。所以我就把这些配置放到app.config里面。但又怕每次打印都加载config影响效率。故此设计了个PrintPaper类。里面所有属性都是静态的。还有一个静态的构造方法。这样只有在程序开始运行加载一次config。之后就直接从内存读取了。
PrintPaper类
<appSettings>
<!--******************************连接字符串设置************************************-->
<add key="DBConnectionStr" value=" "/>
<!--********************************打印边界设置**********************************-->
<!--打印纸的距四个边界的距离,为空这表示默认为0,可以为负值-->
<!--与上边界距离-->
<add key="marginTop" value=""/>
<!--与上边界距离-->
<add key="marginBottom" value=""/>
<!--与上边界距离-->
<add key="marginLeft" value=""/>
<!--与上边界距离-->
<add key="marginRight" value=""/>
<!--********************************打印纸大小设置**********************************-->
<!--打印纸张的大小,为空表示取默认值,不可以为负值 -->
<!--纸张的宽度-->
<add key="paperWidth" value=""/>
<!--纸张的高度-->
<add key="paperHeight" value=""/>
<!--*******************************************************************************-->
</appSettings>
</configuration>
打印表格是自适应表格里面的文字最大的宽度。但如果表格里面确实列很少,没列的最大宽度又很小。打印完真整个表格没有页面的纸张宽。那会自动拉宽每一列的宽度。
打印的核心类PrintCore
private Graphics graphic { set; get; }
/// 构造函数
/// </summary>
/// <param name="_printDoc">打印基础类</param>
/// <param name="_currentX">打印开始的x坐标,默认为0</param>
/// <param name="_currentY">打印开始的y坐标,默认为0</param>
public PrintCore(PrintDocument _printDoc, Graphics _graphics, Font _defaultFont, float _currentX = 0, float _currentY = 0)
{
this.printDoc = _printDoc;
this.currentX = _currentX;
this.currentY = _currentY;
this.defaultFont = _defaultFont;
this.graphic = _graphics;
this.defaultBrush = new SolidBrush(Color.Black); //默认加粗黑色
this.isCenter = false;
//读取配置文件
printDocConfig(_printDoc);
Error = PrintPaper.Error;
}
private void printDocConfig(PrintDocument _printDoc)
{
_printDoc.DefaultPageSettings.Margins = new Margins(PrintPaper.MarginLeft, PrintPaper.MarginRight, PrintPaper.MarginTop, PrintPaper.MarginBottom);
//当paper配置的宽度和高度都大于0时,才配置。否则忽略
if (PrintPaper.Width > 0 && PrintPaper.Height > 0)
{
_printDoc.DefaultPageSettings.PaperSize = new PaperSize("", PrintPaper.Width, PrintPaper.Height);
}
}
/// <summary>
/// 打印字符串,系统可以总动判断换行打印。
/// </summary>
/// <param name="prnStr">打印的字符串</param>
/// <param name="isPrintLine">打印完成后,是否换行,默认为true</param>
public void printString(string prnStr, bool isPrintLine = true)
{
//打印字符串,根据字符串长度,和纸张宽度,高度等自动换行
SizeF measure = graphic.MeasureString(prnStr, defaultFont);
//如果x坐标不为0,或者打印的一行宽度大于纸张的宽度,则居中打印是没用的。不考虑打印
if (!isCenter || currentX != 0 || printDoc.DefaultPageSettings.PaperSize.Width < measure.Width)
{
//计算打印这么多字要多少行
int rows = (int)Math.Ceiling(measure.Width / (printDoc.DefaultPageSettings.PaperSize.Width - currentX));
//根据行,算出要打印的边界矩形框
graphic.DrawString(prnStr, defaultFont, defaultBrush, new Rectangle((int)currentX, (int)currentY, (int)Math.Ceiling((printDoc.DefaultPageSettings.PaperSize.Width - currentX)), (int)Math.Ceiling((measure.Height * rows))));
if (isPrintLine)//如果换行
{
currentY = currentY + measure.Height * rows;
currentX = 0;
}
else
{
currentY = currentY + measure.Height * (rows - 1);
currentX = (measure.Width % (printDoc.DefaultPageSettings.PaperSize.Width - currentX)) + currentX;
}
}
else
{
//居中打印一行
//计算打印前的留白宽度
float blank = (printDoc.DefaultPageSettings.PaperSize.Width - measure.Width) / 2.0f;
currentX = currentX + blank;
graphic.DrawString(prnStr, defaultFont, defaultBrush, currentX, currentY);
if (isPrintLine)//如果换行
{
currentX = 0;
currentY = currentY + measure.Height;
}
else
{
currentX = currentX + measure.Width;
}
}
}
/// <summary>
/// 打印表格,自适应没列的宽度
/// </summary>
/// <param name="prnDgv"></param>
/// <param name="isPrintLine"></param>
public void printDataGridView(DataGridView prnDgv, Font titleFont, Brush titleBrush, Color titleBackGroup, bool isPrintLine = true)
{
if (prnDgv == null)
{
return;
}
prnDgv.AllowUserToAddRows = false;
//记录每一列的宽度
int[] columnWidths = new int[prnDgv.ColumnCount];
//******************取每列的最大宽度***********************
//先计算表头的宽度
for (int i = 0; i < prnDgv.ColumnCount; i++)
{
string celValue = prnDgv.Columns[i].HeaderText;
SizeF measure = graphic.MeasureString(celValue, titleFont);
columnWidths[i] = (int)Math.Ceiling(measure.Width);//把打印表头所占的宽度 先放到columnWidths里面
}
//计算表中数据打印的最大宽度
for (int i = 0; i < prnDgv.Rows.Count; i++)
{
for (int j = 0; j < prnDgv.ColumnCount; j++)
{
string celValue = prnDgv[j, i].Value.ToString();
SizeF measure = graphic.MeasureString(celValue, defaultFont);
if (columnWidths[j] < measure.Width)//如果宽度小于打印宽度,则把长的打印宽度赋值给列宽
{
columnWidths[j] = (int)Math.Ceiling(measure.Width);
}
}
}
//如果表格的宽度小于纸张的宽度,表格没列的宽度加大
int allColumsWidth = 0;
for (int i = 0; i < prnDgv.ColumnCount; i++)
{
allColumsWidth += columnWidths[i];//把打印表头所占的宽度 先放到columnWidths里面
}
if (allColumsWidth + prnDgv.ColumnCount < PrintPaper.Width)
{
int columnAddWidth = (PrintPaper.Width - allColumsWidth - prnDgv.ColumnCount) / prnDgv.ColumnCount;
for (int i = 0; i < prnDgv.ColumnCount; i++)
{
columnWidths[i] += columnAddWidth;
}
}
//*************************************************************
currentX = 0;
int titleHeight = (int)Math.Ceiling(graphic.MeasureString("1e{(汗", titleFont).Height);
//打印表头
for (int i = 0; i < prnDgv.ColumnCount; i++)
{
string celValue = prnDgv.Columns[i].HeaderText;
//打印背景
graphic.FillRectangle(new SolidBrush(titleBackGroup), new Rectangle((int)currentX, (int)currentY, columnWidths[i], titleHeight));
//打印内容
graphic.DrawString(celValue, titleFont, titleBrush, currentX, currentY);
//打印表格边框
graphic.DrawRectangle(new Pen(titleBrush), new Rectangle((int)currentX, (int)currentY, columnWidths[i], titleHeight));
currentX = currentX + columnWidths[i];
}
currentX = 0;
currentY = currentY + titleHeight;
int contentHeight = (int)Math.Ceiling(graphic.MeasureString("1e{(汗", defaultFont).Height);
//打印内容
for (int i = 0; i < prnDgv.Rows.Count; i++)
{
for (int j = 0; j < prnDgv.ColumnCount; j++)
{
string celValue = prnDgv[j, i].Value.ToString();//打印内容
graphic.DrawString(celValue, defaultFont, defaultBrush, currentX, currentY);//打印表格边框
graphic.DrawRectangle(new Pen(defaultBrush), new Rectangle((int)currentX, (int)currentY, columnWidths[j], contentHeight));
currentX = currentX + columnWidths[j];
}
currentX = 0;
currentY = currentY + contentHeight;
}
}
}
调用示例代码
总结:以上打印有两个小问题没有处理。一个是关于分页,一个是当表格的宽度过长,超过了页面的宽度,没有进行换行处理。
另附上源码 winfrom_mrdyj_jb51net
作者:Bonker