using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.Data;
using System.Windows.Forms;
namespace WeightSystem
{
public class cutePrinter2
{
private DataGridView dataGridView1;
private PrintDocument printDocument;
private PageSetupDialog pageSetupDialog;
private PrintPreviewDialog printPreviewDialog;
private string title = "";
int currentPageIndex = 0;
int rowCount = 0;
int pageCount = 0;
int titleSize = 20;
bool isCustomHeader = false;
Brush alertBrush = new SolidBrush(Color.Red);
string[] header = null; // 如果自定義就填入字符串,如果需要斜線分隔,就用\表示,例如:個數#名字 其中#為splitChar
string[] uplineHeader = null; // 上行文字數組
int[] upLineHeaderIndex = null; // 上行的文字index,如果沒有上行就設為-1;
public bool isEveryPagePrintTitle = true; // 是否每一頁都要打印標題。
public int headerHeight = 30; // 標題高度。
public int topMargin = 30// 頂邊距
public int cellTopMargin = 6; // 單元格頂邊距
public int cellLeftMargin = 4; // 單元格左邊距
public char splitChar = '#'; // 當header要用斜線表示的時候
public string falseStr = "×"; // 如果傳進來的DataGridView中有 false,把其轉換得字符。
public string trueStr = "√"; // 如果傳進來的DataGridView中有 true,把其轉換得字符。
public int pageRowCount = 30; // 每頁行數
public int rowGap = 28; // 行高
public int colGap = 5; // 每列間隔
public int leftMargin = 45; // 左邊距
public Font titleFont = new Font("黑體"24, FontStyle.Bold); // 標題字體
public Font font = new Font("宋體"10); // 正文字體
public Font headerFont = new Font("黑體"11, FontStyle.Bold); // 列名標題
public Font footerFont = new Font("Arial"8); // 頁腳顯示頁數的字體
public Font upLineFont = new Font("Arial"9, FontStyle.Bold); // 當header分兩行顯示的時候,上行顯示的字體。
public Font underLineFont = new Font("Arial"8); // 當header分兩行顯示的時候,下行顯示的字體。
public Brush brush = new SolidBrush(Color.Black); // 畫刷
public bool isAutoPageRowCount = true; // 是否自動計算行數。
public int buttomMargin = 80; // 底邊距
public bool needPrintPageIndex = true; // 是否打印頁腳頁數
public bool setTongji = false; // 設置是否顯示統計
string sTitle = ""; // 顯示標題
string sTongJi01 = ""; // 統計01
string sTongJi02 = ""; // 統計02
string sTongJi03 = ""; // 統計03
bool isTongji = false; // 是否顯示統計
string time01; // 具體時間標題
string time02;
Font tongJiFont = new Font("宋體"14); // 統計
Font dateFont = new Font("宋體"12, FontStyle.Bold); // 日期標題
// / <summary>
// / 日統計報表打印
// / </summary>
// / <param name="dGView">DataGridView</param>
// / <param name="title">標題</param>
// / <param name="times01">起始時間</param>
// / <param name="times02">中止時間</param>
// / <param name="tj01">統計01</param>
// / <param name="tj02">統計02</param>
// / <param name="tj03">統計03</param>
// / <param name="tj">確認是否打印統計</param>
// / <param name="iPrintType">打印樣式選擇,默認2</param>
public cutePrinter2(DataGridView dGView, string title, string times01, string times02, string tj01, string tj02, string tj03, bool tj)
{
this.title = title;
this.sTongJi01 = tj01;
this.sTongJi02 = tj02;
this.sTongJi03 = tj03;
this.time01 = times01;
this.time02 = times02;
this.setTongji = tj;
this.dataGridView1 = dGView;
printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);
}
public bool setTowLineHeader(string[] upLineHeader, int[] upLineHeaderIndex)
{
this.uplineHeader = upLineHeader;
this.upLineHeaderIndex = upLineHeaderIndex;
this.isCustomHeader = true;
return true;
}
public bool setHeader(string[] header)
{
this.header = header;
return true;
}
private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
#region 打印 --- 統計報表(年報表)
int width = e.PageBounds.Width;
int height = e.PageBounds.Height;
this.leftMargin = 40; // 重新設置左邊距
if (this.isAutoPageRowCount)
{
pageRowCount = (int)((height - this.topMargin - titleSize - 25 - this.headerFont.Height - this.headerHeight - this.buttomMargin) / this.rowGap);
}
pageCount = (int)(rowCount / pageRowCount);
if (rowCount % pageRowCount > 0)
pageCount++;
if (this.setTongji && pageCount == 1)
{
pageRowCount = (int)((height - this.topMargin - titleSize - 25 - this.headerFont.Height - this.headerHeight - this.buttomMargin - 25/ this.rowGap);
pageCount = (int)(rowCount / pageRowCount);
if (rowCount % pageRowCount > 0)
pageCount++;
}
string sDateTitle = time01 + " — " + time02;
int xoffset = (int)((width - e.Graphics.MeasureString(this.title, this.titleFont).Width) / 2);
int xoffset2 = (int)((width - e.Graphics.MeasureString(sDateTitle, dateFont).Width) / 2);
int colCount = 0;
int x = 0;
int y = topMargin;
string cellValue = "";
int startRow = currentPageIndex * pageRowCount;
int endRow = startRow + this.pageRowCount < rowCount ? startRow + pageRowCount : rowCount;
int currentPageRowCount = endRow - startRow;
if (this.currentPageIndex == 0 || this.isEveryPagePrintTitle)
{
e.Graphics.DrawString(this.title, titleFont, brush, xoffset, y);
e.Graphics.DrawString(sDateTitle, dateFont, brush, xoffset2, y + 40);
y += titleSize + 20;
}
try
{
colCount = dataGridView1.ColumnCount;
y += rowGap;
x = leftMargin;
DrawLine(new Point(x, y), new Point(x, y + currentPageRowCount * rowGap + this.headerHeight), e.Graphics); // 最左邊的豎線
int lastIndex = -1;
int lastLength = 0;
int indexj = -1;
for (int j = 0; j < colCount; j++)
{
int colWidth = dataGridView1.Columns[j].Width;
if (colWidth > 0)
{
indexj++;
if (this.header == null || this.header[indexj] == "")
cellValue = dataGridView1.Columns[j].Name;
else
cellValue = header[indexj];
if (this.isCustomHeader)
{
if (this.upLineHeaderIndex[indexj] != lastIndex)
{
if (lastLength > 0 && lastIndex > -1)// 開始畫上一個upline
{
string upLineStr = this.uplineHeader[lastIndex];
int upXOffset = (int)((lastLength - e.Graphics.MeasureString(upLineStr, this.upLineFont).Width) / 2);
if (upXOffset < 0)
upXOffset = 0;
e.Graphics.DrawString(upLineStr, this.upLineFont, brush, x - lastLength + upXOffset, y + (int)(this.cellTopMargin / 2));
DrawLine(new Point(x - lastLength, y + (int)(this.headerHeight / 2)), new Point(x, y + (int)(this.headerHeight / 2)), e.Graphics); // 中線
DrawLine(new Point(x, y), new Point(x, y + (int)(this.headerHeight / 2)), e.Graphics);
}
lastIndex = this.upLineHeaderIndex[indexj];
lastLength = colWidth + colGap;
}
else
{
lastLength += colWidth + colGap;
}
}
// int currentY=y+cellTopMargin;
int Xoffset = 10;
int Yoffset = 20;
int leftWordIndex = cellValue.IndexOf(this.splitChar);
if (this.upLineHeaderIndex != null && this.upLineHeaderIndex[indexj] > -1)
{
if (leftWordIndex > 0)
{
string leftWord = cellValue.Substring(0, leftWordIndex);
string rightWord = cellValue.Substring(leftWordIndex + 1, cellValue.Length - leftWordIndex - 1);
// 上面的字
Xoffset = (int)(colWidth + colGap - e.Graphics.MeasureString(rightWord, this.upLineFont).Width);
Yoffset = (int)(this.headerHeight / 2 - e.Graphics.MeasureString("a"this.underLineFont).Height);
Xoffset = 6;
Yoffset = 10;
e.Graphics.DrawString(rightWord, this.underLineFont, brush, x + Xoffset - 4, y + (int)(this.headerHeight / 2));
e.Graphics.DrawString(leftWord, this.underLineFont, brush, x + 2, y + (int)(this.headerHeight / 2+ (int)(this.cellTopMargin / 2+ Yoffset - 2);
DrawLine(new Point(x, y + (int)(this.headerHeight / 2)), new Point(x + colWidth + colGap, y + headerHeight), e.Graphics);
x += colWidth + colGap;
DrawLine(new Point(x, y + (int)(this.headerHeight / 2)), new Point(x, y + currentPageRowCount * rowGap + this.headerHeight), e.Graphics);
}
else
{
e.Graphics.DrawString(cellValue, headerFont, brush, x, y + (int)(this.headerHeight / 2+ (int)(this.cellTopMargin / 2));
x += colWidth + colGap;
DrawLine(new Point(x, y + (int)(this.headerHeight / 2)), new Point(x, y + currentPageRowCount * rowGap + this.headerHeight), e.Graphics);
}
}
else
{
if (leftWordIndex > 0)
{
string leftWord = cellValue.Substring(0, leftWordIndex);
string rightWord = cellValue.Substring(leftWordIndex + 1, cellValue.Length - leftWordIndex - 1);
// 上面的字
Xoffset = (int)(colWidth + colGap - e.Graphics.MeasureString(rightWord, this.upLineFont).Width);
Yoffset = (int)(this.headerHeight - e.Graphics.MeasureString("a"this.underLineFont).Height);
e.Graphics.DrawString(rightWord, this.headerFont, brush, x + Xoffset - 4, y + 2);
e.Graphics.DrawString(leftWord, this.headerFont, brush, x + 2, y + Yoffset - 4);
DrawLine(new Point(x, y), new Point(x + colWidth + colGap, y + headerHeight), e.Graphics);
x += colWidth + colGap;
DrawLine(new Point(x, y), new Point(x, y + currentPageRowCount * rowGap + this.headerHeight), e.Graphics);
}
else
{
e.Graphics.DrawString(cellValue, headerFont, brush, x, y + cellTopMargin);
x += colWidth + colGap;
DrawLine(new Point(x, y), new Point(x, y + currentPageRowCount * rowGap + this.headerHeight), e.Graphics);
}
}
}
}
// // 循環結束,畫最后一個的upLine
if (this.isCustomHeader)
{
if (lastLength > 0 && lastIndex > -1)// 開始畫上一個upline
{
string upLineStr = this.uplineHeader[lastIndex];
int upXOffset = (int)((lastLength - e.Graphics.MeasureString(upLineStr, this.upLineFont).Width) / 2);
if (upXOffset < 0)
upXOffset = 0;
e.Graphics.DrawString(upLineStr, this.upLineFont, brush, x - lastLength + upXOffset, y + (int)(this.cellTopMargin / 2));
DrawLine(new Point(x - lastLength, y + (int)(this.headerHeight / 2)), new Point(x, y + (int)(this.headerHeight / 2)), e.Graphics); // 中線
DrawLine(new Point(x, y), new Point(x, y + (int)(this.headerHeight / 2)), e.Graphics);
}
}
int rightBound = x;
DrawLine(new Point(leftMargin, y), new Point(rightBound, y), e.Graphics); // 最上面的線
// DrawLine(new Point(leftMargin,y+this.headerHeight),new Point(rightBound,y+this.headerHeight),e.Graphics); // 列名的下面的線
y += this.headerHeight;
// print all rows
for (int i = startRow; i < endRow; i++)
{
x = leftMargin;
for (int j = 0; j < colCount; j++)
{
if (dataGridView1.Columns[j].Width > 0)
{
cellValue = dataGridView1.Rows[i].Cells[j].Value.ToString();
if (cellValue == "False")
cellValue = falseStr;
if (cellValue == "True")
cellValue = trueStr;
e.Graphics.DrawString(cellValue, font, brush, x + this.cellLeftMargin, y + cellTopMargin);
x += dataGridView1.Columns[j].Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] { '\r''\n' }).Length - 1);
}
}
DrawLine(new Point(leftMargin, y), new Point(rightBound, y), e.Graphics);
y += rowGap;
}
DrawLine(new Point(leftMargin, y), new Point(rightBound, y), e.Graphics);
currentPageIndex++;
if (this.setTongji && currentPageIndex == pageCount)
this.isTongji = true;
if (this.isTongji)
{
int xoffsetTongJi = (int)((width - e.Graphics.MeasureString(sTongJi01, dateFont).Width) / 2);
e.Graphics.DrawString(this.sTongJi01, this.tongJiFont, brush, xoffsetTongJi, y + 25); // 統計01
e.Graphics.DrawString(this.sTongJi02, this.tongJiFont, brush, xoffsetTongJi, y + 50); // 統計03
e.Graphics.DrawString(this.sTongJi03, this.tongJiFont, brush, xoffsetTongJi + 340, y + 50); // 統計04
}
if (this.needPrintPageIndex)
{
if (pageCount != 1)
{
e.Graphics.DrawString("" + pageCount.ToString() + " 頁,當前第 " + this.currentPageIndex.ToString() + " 頁"this.footerFont, brush, width - 200, (int)(height - this.buttomMargin / 2 - this.footerFont.Height));
}
}
string s = cellValue;
string f3 = cellValue;
if (currentPageIndex < pageCount)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
this.currentPageIndex = 0;
}
// iPageNumber+=1;
}
catch
{
}
#endregion
}
private void DrawLine(Point sp, Point ep, Graphics gp)
{
Pen pen = new Pen(Color.Black);
gp.DrawLine(pen, sp, ep);
}
public PrintDocument GetPrintDocument()
{
return printDocument;
}
public void Print()
{
rowCount = 0;
try
{
if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataSet")
{
rowCount = ((DataSet)dataGridView1.DataSource).Tables[0].Rows.Count;
}
else if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataView")
{
rowCount = ((DataView)dataGridView1.DataSource).Count;
}
pageSetupDialog = new PageSetupDialog();
pageSetupDialog.AllowOrientation = true;
pageSetupDialog.Document = printDocument;
pageSetupDialog.Document.DefaultPageSettings.Landscape = true; // 設置打印為橫向
pageSetupDialog.ShowDialog();
printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
printPreviewDialog.Height = 600;
printPreviewDialog.Width = 800;
printPreviewDialog.ClientSize = new System.Drawing.Size(1024768);
printPreviewDialog.PrintPreviewControl.Zoom = 1;
printPreviewDialog.ShowDialog();
}
catch
{
// throw new Exception("Printer error." + e.Message);
}
}
}
}