关于打印(Winform)? C#,怎么实现呀,感觉特迷惑。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Windows 窗体 PrintDocument 组件用于设置一些属性,这些属性说明在基于 Windows 的应用程序中要打印什么内容以及打印文档的能力。可将它与 PrintDialog 组件一起使用来控制文档打印的各个方面。 使用 PrintDocument 组件涉及 PrintDocument 组件的两种主要情况是: 简单的打印作业,如打印单个文本文件。在这种情况下,应将 PrintDocument 组件添加到 Windows 窗体,然后在 PrintPage 事件处理程序中添加打印文件的编程逻辑。该编程逻辑应以使用 Print 方法打印文档结束。此方法向打印机发送一个 Graphics 对象,该对象包含在 PrintPageEventArgs 类的 Graphics 属性中。更为复杂的打印作业,如想要重新使用已编写的打印逻辑的情况。在这种情况下,应从 PrintDocument 组件派生一个新组件,并重写(参见 Visual Basic 的 重写 或 C# 的 重写)PrintPage 事件。将 PrintDocument 组件添加到窗体后,它出现在 Windows 窗体设计器底部的栏中。Visual Studio 中对此功能提供了广泛的支持。有关更多信息,请参见 PrintDocument 组件(Windows 窗体)。public class PrintingExample : System.Windows.Forms.Form { private System.ComponentModel.Container components; private System.Windows.Forms.Button printButton; private Font printFont; private StreamReader streamToPrint; public PrintingExample() : base() { // The Windows Forms Designer requires the following call. InitializeComponent(); } // The Click event is raised when the user clicks the Print button. private void printButton_Click(object sender, EventArgs e) { try { streamToPrint = new StreamReader ("C:\\My Documents\\MyFile.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); } } // The PrintPage event is raised for each page to be printed. private void pd_PrintPage(object sender, PrintPageEventArgs ev) { float linesPerPage = 0; float yPos = 0; int count = 0; float leftMargin = ev.MarginBounds.Left; float topMargin = ev.MarginBounds.Top; string line = null; // Calculate the number of lines per page. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics); // Print each line of the file. while(count < linesPerPage && ((line=streamToPrint.ReadLine()) != null)) { yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)); ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat()); count++; } // If more lines exist, print another page. if(line != null) ev.HasMorePages = true; else ev.HasMorePages = false; } // The Windows Forms Designer requires the following procedure. private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.printButton = new System.Windows.Forms.Button(); this.ClientSize = new System.Drawing.Size(504, 381); this.Text = "Print Example"; printButton.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; printButton.Location = new System.Drawing.Point(32, 110); printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; printButton.TabIndex = 0; printButton.Text = "Print the file."; printButton.Size = new System.Drawing.Size(136, 40); printButton.Click += new System.EventHandler(printButton_Click); this.Controls.Add(printButton); } // This is the main entry point for the application. public static void Main(string[] args) { Application.Run(new PrintingExample()); }} http://www.wave12.com/web/SigCon.asp?bCate=50&sCateName=%B9%A6%C4%DC%BD%E9%C9%DC&ID=173&CateName=wsReport4.6 www.webmis.com.cn源码www.AnyReport.Net套打 GridControl 去除左边表头 DataGdirView+BindingSouce如何快速进行多字段查询与定位 try/catch与性能 急,vs2008不能获取gridview中checkbox的状态? 关于C#与数据库的问题,动态添加数据.SQL Server2005 使用stopWatch的问题 关于使用PANEL的问题 请教大家,怎么获得一个应用程序上的输入框? 如何在Web Service中使用Hashtable?江湖救急。 怎样设置环境变量 SOS!!! 如何用C#实现在程序里直接将当前所用的域用户设置为本机管理员
涉及 PrintDocument 组件的两种主要情况是: 简单的打印作业,如打印单个文本文件。在这种情况下,应将 PrintDocument 组件添加到 Windows 窗体,然后在 PrintPage 事件处理程序中添加打印文件的编程逻辑。该编程逻辑应以使用 Print 方法打印文档结束。此方法向打印机发送一个 Graphics 对象,该对象包含在 PrintPageEventArgs 类的 Graphics 属性中。更为复杂的打印作业,如想要重新使用已编写的打印逻辑的情况。在这种情况下,应从 PrintDocument 组件派生一个新组件,并重写(参见 Visual Basic 的 重写 或 C# 的 重写)PrintPage 事件。将 PrintDocument 组件添加到窗体后,它出现在 Windows 窗体设计器底部的栏中。Visual Studio 中对此功能提供了广泛的支持。有关更多信息,请参见 PrintDocument 组件(Windows 窗体)。
public class PrintingExample : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
private Font printFont;
private StreamReader streamToPrint; public PrintingExample() : base()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
} // The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
try
{
streamToPrint = new StreamReader
("C:\\My Documents\\MyFile.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);
}
} // The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
string line = null; // Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics); // Print each line of the file.
while(count < linesPerPage &&
((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count *
printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
} // If more lines exist, print another page.
if(line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}
// The Windows Forms Designer requires the following procedure.
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.printButton = new System.Windows.Forms.Button(); this.ClientSize = new System.Drawing.Size(504, 381);
this.Text = "Print Example"; printButton.ImageAlign =
System.Drawing.ContentAlignment.MiddleLeft;
printButton.Location = new System.Drawing.Point(32, 110);
printButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
printButton.TabIndex = 0;
printButton.Text = "Print the file.";
printButton.Size = new System.Drawing.Size(136, 40);
printButton.Click += new System.EventHandler(printButton_Click); this.Controls.Add(printButton);
} // This is the main entry point for the application.
public static void Main(string[] args)
{
Application.Run(new PrintingExample());
}
}