如何在Winform中循环打印多页(或不同页)?(如i=1,随i++,打印i的值)
为什么打印的时候要人手动换页?而不是打印机自动换页的?请问有哪位高手知道,有没有更好的方法?谢谢赐教!
为什么打印的时候要人手动换页?而不是打印机自动换页的?请问有哪位高手知道,有没有更好的方法?谢谢赐教!
解决方案 »
- .net 里有没有和js中的join功能类似的方法
- 正则表达式匹配问题
- WinForm中ultraComboEditor的问题
- 如何防止sql注入
- 字符串如何转化为时间
- 斑竹怎么没有热心人啊,大家快来看看更新问题
- 我在使用Convert.ToInt32时,系统提示不能通过,请问怎么搞,前一段时间用时还可以啊!
- C#中,如何从SQL Server中读取图片PictureBoxk显示出业?
- 如何把另一个类文件引用到当前文件中来?
- 怎么关闭登录界面
- 把程序集作成通用接口怎么做?他的解决思路是什么啊(原理)有哪位大哥有例子让我学习下我以前没写过公司要的急谢谢各位(给出解决原理的
- 关于流的读取,这句话对吗?
/// <copyright from='1997' to='2001' company='Microsoft Corporation'>
/// 版权所有 (c) Microsoft Corporation。保留所有权利。
///
/// 此源代码仅作为 Microsoft 开发工具和/或联机文档
/// 的补充。有关 Microsoft 代码示例的详细信息,请
/// 参阅这些其他资料。
///
/// </copyright>
//------------------------------------------------------------------------------
namespace Microsoft.Samples.WinForms.Cs.PrintingExample1 {
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Printing;
using System.IO; public class PrintingExample1 : System.Windows.Forms.Form {
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components;
protected internal System.Windows.Forms.Button printButton; private Font printFont;
private StreamReader streamToPrint;
public PrintingExample1() { //
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); // 启动打印按钮的事件处理程序
printButton.Click += new System.EventHandler(printButton_Click);
} /// <summary>
/// 清理正在使用的所有资源。
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
} //在用户按下打印按钮时激发的事件
private void printButton_Click(object sender, EventArgs e) {
try { streamToPrint = new StreamReader ("PrintMe.Txt");
try {
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument(); //假定为默认打印机
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.Print();
} finally {
streamToPrint.Close() ;
} } catch(Exception ex) {
MessageBox.Show("打印文件时发生错误 - " + ex.Message);
}
} //每个要打印的页所激发的事件
private void pd_PrintPage(object sender, PrintPageEventArgs ev) {
float lpp = 0 ;
float yPos = 0 ;
int count = 0 ;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line=null; //算出每页的行数
//在事件上使用 MarginBounds 以达到此目的
lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) ; //现在,在文件上重复此操作以输出每行
//注意:假设单行比页宽窄
//首先检查行数,以便看不到不打印的行
while (count < lpp && ((line=streamToPrint.ReadLine()) != null)) {
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++;
} //如果有多行,则另外打印一页
if (line != null)
ev.HasMorePages = true ;
else
ev.HasMorePages = false ;
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用
/// 代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container ();
this.printButton = new System.Windows.Forms.Button ();
this.Text = "打印示例 1";
this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
this.ClientSize = new System.Drawing.Size (504, 381);
printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.Size = new System.Drawing.Size (136, 40);
printButton.TabIndex = 0;
printButton.Location = new System.Drawing.Point (32, 112);
printButton.Text = "打印文件";
this.Controls.Add (this.printButton);
} /// <summary>
/// 应用程序的主要入口点。
/// </summary>
[STAThread]
public static void Main(string[] args) {
Application.Run(new PrintingExample1());
} }
}
可以打开了