用PrintDocument打印怎样取得总页数? 怎样在预览中显示出总页数? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //------------------------------------------------------------------------------/// <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()); } }} 先计算一次需要的页面数www.webmis.com.cn 楼主显然想问在不预览的情况下如何获取页数其实楼主可以根据高度和边距自己算一下就可以了和PRINTPAGE事件相比不用在上面画图写字,只需要量高 String和string菜鸟问题 2维的字符数组定义问题!就是 char[,] = {} ? 泛型怎么用? c#怎样实现后台运行 请问一个object类型的变量,怎么转换成string类型,谢谢!!!! 自动聊天机器人怎么做??? 关于LINQ 数据库丢失 我该选择什么样的开发工具?vc,C#,java,cb? 熟悉C#和Oracle的高手看过来? 关于C#中System.IO名称空间中的两个类的问题 怎么取得object[,]的行数和列数? C#打包的问题!请高手帮助!
/// <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());
} }
}
其实楼主可以根据高度和边距自己算一下就可以了
和PRINTPAGE事件相比不用在上面画图写字,只需要量高