.net打印方法总结
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
// 计算出要打印的下一行所基于页面的位置 float yPos = top + (lineCount * printFont.GetHeight(e.Graphics)); e.Graphics.DrawString(line, printFont, new SolidBrush(Color.Black), left,
打印就是绘制,和画图类似,画图是在 WinForm的Paint事件中绘制,而打印则是在 PrintDocument的PrintPage事件中绘制。
打印基本条件
1. using System.Drawing.Printing //引用命名空间 2. PrintDocument printdoc = new PrintDocument();
System.Drawing.GraphicsUnit.Pixel); }
图片分页打印
private int imgPage = 1; private void printdoc_PrintPage(object sender, PrintPageEventArgs e) {
int x = e.MarginBounds.X;int y = e.MarginBounds.Y; int width = img.Width;int height = img.Height; width = e.MarginBounds.Width ; height = (img.Height * e.MarginBounds.Width / img.Width); Rectangle destRect = new Rectangle(x, y, width, height); Rectangle imgRect; if (imgPage == 1) {
大pdf文件转化为图片的长和宽过大10000像素左右会发生内存泄漏所以这时要控制转换图片的长度和宽度
主要内容
• WinForm打印原理 • 文档打印 • 图片打印 • 报表打印
– 图片模板打印 – Word模板打印 – Excel模板打印 – Flexcell模板打印
• PDF打印
WinForm打印原理
基本打印方式
• 打印文字(e.Graphics.DrawString) • 打印图形
(e.Graphics.DrawLine/e.Graphics.DrawRectangle) • 打印图像(e.Graphics.DrawImage)
简单文档打印
private static void printdoc_PrintPage(object sender,PrintPageEventArgs e) {
// 插入图片 Excel.Range picRange = (Excel.Range)xlWorksheet.Cells[1, 1]; picRange.Select(); xlWorksheet.Shapes.AddPicture(picName, Office.Core.MsoTriState.msoFalse, Office.Core.MsoTriState.msoTrue, Convert.ToSingle(picRange.Left), Convert.ToSingle(picRange.Top), img.Width, img.Height);
yPos, new StringFormat());// 打印一行 lineCount++;// 行数+1 } if (line != null) // 如果还有下一页,则继续打印 e.HasMorePages = true; else e.HasMorePages = false; }
打印分页
private void printdoc_PrintPage(object sender,PrintPageEventArgs e)
//实例化打印对象 3. printdoc.PrintPage += new
PrintPageEventHandler(printdoc_PrintPage); //定义打印对象的事件 4. printdoc.Print(); //开始打印
简单打印示例
public static void SimplePrint() {
// 打印预览 xlWorksheet.PrintPreview();
报表打印
• 简单报表打印
– DataGridView打印 – 图片模 – Excel模板打印 – FlexCell模板打印
DataGridView打印
// 打印预览窗体 PrintPreviewDialog preDlg = new PrintPreviewDialog(); preDlg.Document = printdoc; preDlg.ShowDialog();
// 页面设置窗体 PageSetupDialog preDlg = new PageSetupDialog(); preDlg.Document = printdoc; preDlg.ShowDialog();
width = e.MarginBounds.Width; height = img.Height * e.MarginBounds.Width / img.Width; } else { height = e.MarginBounds.Height; width = img.Width * e.MarginBounds.Height / img.Height; } Rectangle destRect = new Rectangle(x, y, width, height); e.Graphics.DrawImage(img, destRect, 0, 0, img.Width, img.Height,
基本打印事件
1.// 打印开始事件--准备打印材料 printdoc.BeginPrint += new PrintEventHandler(printdoc_BeginPrint); 2. // 打印页面事件--打印主要内容 printdoc.PrintPage += new PrintPageEventHandler(printdoc_PrintPage); 3.// 打印结束事件--释放打印资源 printdoc.EndPrint +=new PrintEventHandler(printdoc_EndPrint);
20),Brushes.Black,new PointF(200,400)); }
.net提供的打印辅助窗体
// 打印设置窗体 PrintDialog preDlg = new PrintDialog(); preDlg.Document = printdoc; preDlg.ShowDialog();
{ e.HasMorePages = true;// 需要分页 e.HasMorePages = false;// 不需要分页
}
图片打印
private static void printdoc_PrintPage(object sender, PrintPageEventArgs e) {
Image img = Image.FromFile(picName); int x = e.MarginBounds.X; int y = e.MarginBounds.Y; int width = img.Width; int height = img.Height; // 自适应页面 if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height)) {
int lineCount =0; float left = e.MarginBounds.Left; float top = e.MarginBounds.Top; string line = null; Font printFont = new Font("宋体", 12); // 计算每一页打印多少行 float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics); // 循环读取StringReader对象 while (lineCount < linesPerPage && ((line = m_streamReader.ReadLine()) != null)) {
图片分页打印2
1. 添加Dll文件:Microsoft.Office.Interop.Excel.dll 2. 添加Dll文件:Office.dll 3. 新建Excel 4. 插入图片 5. 页面设置—页面—缩放—调整页宽和页高 6. 打印
// 新建Excel文件 Excel.ApplicationClass xlApp = new Excel.ApplicationClass(); xlApp.Visible = true;// 文件可见 Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(Type.Missing); Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets[1];
//实例化打印对象 PrintDocument printdoc = new PrintDocument(); //定义打印对象的事件 printdoc.PrintPage+=new PrintPageEventHandler(printdoc_PrintPage); //开始打印 printdoc.Print(); } private static void printdoc_PrintPage(object sender, PrintPageEventArgs e) { //控制打印是什么内容 打印内容\字体对象\填充颜色\打印到纸上的起始坐标 e.Graphics.DrawString(“Hello World!”, new Font(“宋体”,
imgRect = new Rectangle(0, 0, img.Width / 2, img.Height / 2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = true; } else if (imgPage == 2) { imgRect = new Rectangle(img.Width / 2, 0, img.Width/2, img.Height/2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = true; } else if (imgPage == 3) { imgRect = new Rectangle(0, img.Height / 2, img.Width/2, img.Height/2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = true; } else if (imgPage == 4) { imgRect = new Rectangle(img.Width / 2, img.Height / 2, img.Width/2, img.Height/2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = false; }
打印就是绘制,和画图类似,画图是在 WinForm的Paint事件中绘制,而打印则是在 PrintDocument的PrintPage事件中绘制。
打印基本条件
1. using System.Drawing.Printing //引用命名空间 2. PrintDocument printdoc = new PrintDocument();
System.Drawing.GraphicsUnit.Pixel); }
图片分页打印
private int imgPage = 1; private void printdoc_PrintPage(object sender, PrintPageEventArgs e) {
int x = e.MarginBounds.X;int y = e.MarginBounds.Y; int width = img.Width;int height = img.Height; width = e.MarginBounds.Width ; height = (img.Height * e.MarginBounds.Width / img.Width); Rectangle destRect = new Rectangle(x, y, width, height); Rectangle imgRect; if (imgPage == 1) {
大pdf文件转化为图片的长和宽过大10000像素左右会发生内存泄漏所以这时要控制转换图片的长度和宽度
主要内容
• WinForm打印原理 • 文档打印 • 图片打印 • 报表打印
– 图片模板打印 – Word模板打印 – Excel模板打印 – Flexcell模板打印
• PDF打印
WinForm打印原理
基本打印方式
• 打印文字(e.Graphics.DrawString) • 打印图形
(e.Graphics.DrawLine/e.Graphics.DrawRectangle) • 打印图像(e.Graphics.DrawImage)
简单文档打印
private static void printdoc_PrintPage(object sender,PrintPageEventArgs e) {
// 插入图片 Excel.Range picRange = (Excel.Range)xlWorksheet.Cells[1, 1]; picRange.Select(); xlWorksheet.Shapes.AddPicture(picName, Office.Core.MsoTriState.msoFalse, Office.Core.MsoTriState.msoTrue, Convert.ToSingle(picRange.Left), Convert.ToSingle(picRange.Top), img.Width, img.Height);
yPos, new StringFormat());// 打印一行 lineCount++;// 行数+1 } if (line != null) // 如果还有下一页,则继续打印 e.HasMorePages = true; else e.HasMorePages = false; }
打印分页
private void printdoc_PrintPage(object sender,PrintPageEventArgs e)
//实例化打印对象 3. printdoc.PrintPage += new
PrintPageEventHandler(printdoc_PrintPage); //定义打印对象的事件 4. printdoc.Print(); //开始打印
简单打印示例
public static void SimplePrint() {
// 打印预览 xlWorksheet.PrintPreview();
报表打印
• 简单报表打印
– DataGridView打印 – 图片模 – Excel模板打印 – FlexCell模板打印
DataGridView打印
// 打印预览窗体 PrintPreviewDialog preDlg = new PrintPreviewDialog(); preDlg.Document = printdoc; preDlg.ShowDialog();
// 页面设置窗体 PageSetupDialog preDlg = new PageSetupDialog(); preDlg.Document = printdoc; preDlg.ShowDialog();
width = e.MarginBounds.Width; height = img.Height * e.MarginBounds.Width / img.Width; } else { height = e.MarginBounds.Height; width = img.Width * e.MarginBounds.Height / img.Height; } Rectangle destRect = new Rectangle(x, y, width, height); e.Graphics.DrawImage(img, destRect, 0, 0, img.Width, img.Height,
基本打印事件
1.// 打印开始事件--准备打印材料 printdoc.BeginPrint += new PrintEventHandler(printdoc_BeginPrint); 2. // 打印页面事件--打印主要内容 printdoc.PrintPage += new PrintPageEventHandler(printdoc_PrintPage); 3.// 打印结束事件--释放打印资源 printdoc.EndPrint +=new PrintEventHandler(printdoc_EndPrint);
20),Brushes.Black,new PointF(200,400)); }
.net提供的打印辅助窗体
// 打印设置窗体 PrintDialog preDlg = new PrintDialog(); preDlg.Document = printdoc; preDlg.ShowDialog();
{ e.HasMorePages = true;// 需要分页 e.HasMorePages = false;// 不需要分页
}
图片打印
private static void printdoc_PrintPage(object sender, PrintPageEventArgs e) {
Image img = Image.FromFile(picName); int x = e.MarginBounds.X; int y = e.MarginBounds.Y; int width = img.Width; int height = img.Height; // 自适应页面 if ((width / e.MarginBounds.Width) > (height / e.MarginBounds.Height)) {
int lineCount =0; float left = e.MarginBounds.Left; float top = e.MarginBounds.Top; string line = null; Font printFont = new Font("宋体", 12); // 计算每一页打印多少行 float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics); // 循环读取StringReader对象 while (lineCount < linesPerPage && ((line = m_streamReader.ReadLine()) != null)) {
图片分页打印2
1. 添加Dll文件:Microsoft.Office.Interop.Excel.dll 2. 添加Dll文件:Office.dll 3. 新建Excel 4. 插入图片 5. 页面设置—页面—缩放—调整页宽和页高 6. 打印
// 新建Excel文件 Excel.ApplicationClass xlApp = new Excel.ApplicationClass(); xlApp.Visible = true;// 文件可见 Excel.Workbook xlWorkbook = xlApp.Workbooks.Add(Type.Missing); Excel.Worksheet xlWorksheet = (Excel.Worksheet)xlWorkbook.Worksheets[1];
//实例化打印对象 PrintDocument printdoc = new PrintDocument(); //定义打印对象的事件 printdoc.PrintPage+=new PrintPageEventHandler(printdoc_PrintPage); //开始打印 printdoc.Print(); } private static void printdoc_PrintPage(object sender, PrintPageEventArgs e) { //控制打印是什么内容 打印内容\字体对象\填充颜色\打印到纸上的起始坐标 e.Graphics.DrawString(“Hello World!”, new Font(“宋体”,
imgRect = new Rectangle(0, 0, img.Width / 2, img.Height / 2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = true; } else if (imgPage == 2) { imgRect = new Rectangle(img.Width / 2, 0, img.Width/2, img.Height/2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = true; } else if (imgPage == 3) { imgRect = new Rectangle(0, img.Height / 2, img.Width/2, img.Height/2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = true; } else if (imgPage == 4) { imgRect = new Rectangle(img.Width / 2, img.Height / 2, img.Width/2, img.Height/2); e.Graphics.DrawImage(img, destRect, imgRect, System.Drawing.GraphicsUnit.Pixel); imgPage++; e.HasMorePages = false; }