打印按钮事件:
#region 【打印按钮】
private void btnX_print_Click(object sender, EventArgs e)
{
PrintPreviewDialog ppd_dgv = new PrintPreviewDialog();
PrintDocument printdt = new PrintDocument();
ppd_dgv.PrintPreviewControl.Document = printdt;
printdt.DefaultPageSettings.Landscape = true;//设置为横向
printdt.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
try
{
ppd_dgv.ShowDialog();
}
catch (Exception ec)
{
MessageBox.Show(ec.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
PrintPage代码:
private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int rowCount = 0;//行数
int colCount = 0;//列数
int x = 0;
int y = 0;
int rowGap = 20;//行间距
int colGap = 30;//列间距
int leftMargin = 20;//左边距
Font font = new Font("Arial", 10);
Font headingFont = new Font("宋体", 11, FontStyle.Underline);//标题字体
Font captionFont = new Font("Arial", 10, FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
string cellValue = ""; if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataTable")
{
rowCount = ((DataTable)dataGridView1.DataSource).Rows.Count;
}
else if (dataGridView1.DataSource.GetType().ToString() == "System.Collections.ArrayList")
{
rowCount = ((ArrayList)dataGridView1.DataSource).Count;
}
colCount = dataGridView1.ColumnCount;
//print 标题
y += rowGap;
x = leftMargin;
if (y <= e.PageBounds.Height)
{
e.HasMorePages = false;
}
else
{
e.HasMorePages = true;
}
for (int j = 0; j < colCount; j++)
{
if (dataGridView1.Columns[j].Width > 0)
{
cellValue = dataGridView1.Columns[j].HeaderText;
e.Graphics.DrawString(cellValue, headingFont, brush, x, y);
x += dataGridView1.Columns[j].Width + colGap;
}
} //print all rows
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
y += rowGap;
x = leftMargin;
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Columns[j].Width > 0)
{
cellValue = dataGridView1.Rows[i].Cells[j].Value.ToString();
e.Graphics.DrawString(cellValue, font, brush, x, y);
x += dataGridView1.Columns[j].Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] { ' ', ' ' }).Length - 1);
}
}
}
string s = cellValue;
string f3 = cellValue; }
#region 【打印按钮】
private void btnX_print_Click(object sender, EventArgs e)
{
PrintPreviewDialog ppd_dgv = new PrintPreviewDialog();
PrintDocument printdt = new PrintDocument();
ppd_dgv.PrintPreviewControl.Document = printdt;
printdt.DefaultPageSettings.Landscape = true;//设置为横向
printdt.PrintPage += new PrintPageEventHandler(docToPrint_PrintPage);
try
{
ppd_dgv.ShowDialog();
}
catch (Exception ec)
{
MessageBox.Show(ec.Message, "打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
#endregion
PrintPage代码:
private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
int rowCount = 0;//行数
int colCount = 0;//列数
int x = 0;
int y = 0;
int rowGap = 20;//行间距
int colGap = 30;//列间距
int leftMargin = 20;//左边距
Font font = new Font("Arial", 10);
Font headingFont = new Font("宋体", 11, FontStyle.Underline);//标题字体
Font captionFont = new Font("Arial", 10, FontStyle.Bold);
Brush brush = new SolidBrush(Color.Black);
string cellValue = ""; if (dataGridView1.DataSource.GetType().ToString() == "System.Data.DataTable")
{
rowCount = ((DataTable)dataGridView1.DataSource).Rows.Count;
}
else if (dataGridView1.DataSource.GetType().ToString() == "System.Collections.ArrayList")
{
rowCount = ((ArrayList)dataGridView1.DataSource).Count;
}
colCount = dataGridView1.ColumnCount;
//print 标题
y += rowGap;
x = leftMargin;
if (y <= e.PageBounds.Height)
{
e.HasMorePages = false;
}
else
{
e.HasMorePages = true;
}
for (int j = 0; j < colCount; j++)
{
if (dataGridView1.Columns[j].Width > 0)
{
cellValue = dataGridView1.Columns[j].HeaderText;
e.Graphics.DrawString(cellValue, headingFont, brush, x, y);
x += dataGridView1.Columns[j].Width + colGap;
}
} //print all rows
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
y += rowGap;
x = leftMargin;
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Columns[j].Width > 0)
{
cellValue = dataGridView1.Rows[i].Cells[j].Value.ToString();
e.Graphics.DrawString(cellValue, font, brush, x, y);
x += dataGridView1.Columns[j].Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] { ' ', ' ' }).Length - 1);
}
}
}
string s = cellValue;
string f3 = cellValue; }
解决方案 »
- c# 调用 vc dll 函数方法
- 求解:如何将本地的Csv文件导入到服务器上
- 请教高手:在ASP.NET如何制作一个浮动的层,无论是缩放窗口还是拉动滚动条,这个层总是在左边中间位置?
- c#汉字转码问题(打印机用)
- GDI+ 怎样为透明窗体设计一个橡皮擦?
- 请教一下ListItem的双向选择的列表框怎样才能用服务器的方式接收
- web service和存储过程相比,有什么优点和缺点?
- 透明图片的问题!急!
- C# 事务 Update不成功,难!难!!!!!!!!!!!!!!!
- firefox装个什么插件才能视频聊天
- 如何给winform的listview加按钮?
- 当停止windows服务时如何处理?
using System.Drawing.Printing;namespace SimpleEditor
{
public partial class SimpleEditorForm : Form
{
private string filename = "Untitled"; //1、實例化打印文檔
PrintDocument pdDocument = new PrintDocument(); private string[] lines;
private int linesPrinted;
public SimpleEditorForm()
{
InitializeComponent(); //2、訂閱事件 //訂閱PinrtPage事件,用於繪製各個頁內容
pdDocument.PrintPage += new PrintPageEventHandler(OnPrintPage);
//訂閱BeginPrint事件,用於得到被打印的內容
pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);
//訂閱EndPrint事件,用於釋放資源
pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);
} private void OnFilePrint(object sender, EventArgs e)
{
try
{
//調用打印
pdDocument.Print(); /*
* PrintDocument對象的Print()方法在PrintController類的幫助下,執行PrintPage事件。
*/
}
catch (InvalidPrinterException ex )
{
MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
throw;
}
} /// <summary>
/// 3、得到打印內容
/// 每個打印任務衹調用OnBeginPrint()一次。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pdDocument_BeginPrint(object sender, PrintEventArgs e)
{
char[] param ={ '\n' };
lines = textBoxEdit.Text.Split(param); int i = 0;
char[] trimParam ={ '\r' };
foreach (string s in lines)
{
lines[i++] = s.TrimEnd(trimParam);
}
}
/// <summary>
/// 4、繪制多個打印頁面
/// printDocument的PrintPage事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnPrintPage(object sender, PrintPageEventArgs e)
{
/*
* 得到TextBox中每行的字符串數組
* \n換行
* \r回車
*/ int x = 20;
int y = 20;
while (linesPrinted<lines.Length)
{
//繪製要打印的頁面
e.Graphics.DrawString(lines[linesPrinted++], new Font("Arial", 10), Brushes.Black, x, y); y += 55; //判斷超過一頁時,允許進行多頁打印
if (y >= e.PageBounds.Height - 80)
{
//允許多頁打印
e.HasMorePages = true; /*
* PrintPageEventArgs類的HaeMorePages屬性為True時,通知控件器,必須再次調用OnPrintPage()方法,打印一個頁面。
* PrintLoopI()有一個用於每個要打印的頁面的序例。如果HasMorePages是False,PrintLoop()就會停止。
*/
return;
}
} linesPrinted = 0;
//繪制完成後,關閉多頁打印功能
e.HasMorePages = false; } /// <summary>
///5、EndPrint事件,釋放資源
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void pdDocument_EndPrint(object sender, PrintEventArgs e)
{
//變量Lines占用和引用的字符串數組,現在釋放
lines = null;
}
}
}
2、訂閱事件(訂閱BeginPrint事件,用於得到被打印的內容;PinrtPage事件,用於繪製各個頁內容; EndPrint事件,用於釋放資源)
3、調用BeginPrint事件的方法,得到打印內容
4、調用PinrtPage事件的方法,繪制多個打印頁面,並根據判斷,設置是否進行多頁打印
5、調用EndPrint事件的方法,釋放資源,完成后開始打印
很丰富的。
{
i = 0;
} private void docToPrint_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// print One Page rows
for (; i < dataGridView1.Rows.Count; i++)
{
y += rowGap;
if (y > e.PageBounds.Height) break; // 不要超出页尾
x = leftMargin;
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Columns[j].Width > 0)
{
cellValue = dataGridView1.Rows[i].Cells[j].Value.ToString();
e.Graphics.DrawString(cellValue, font, brush, x, y);
x += dataGridView1.Columns[j].Width + colGap;
y = y + rowGap * (cellValue.Split(new char[] { ' ', ' ' }).Length - 1);
}
}
} e.HasMorePages = (i < dataGridView1.Rows.Count); // 判断是否还有更多的数据待打印
}