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.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
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());
}
}
{
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.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
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());
}
}
streamToPrint = new StreamReader("C:\\t1.txt",System.Text.Encoding.GetEncoding("GB2312"));现在的问题是:如果我要打印word文档,现在打出来的全部是乱码了,怎么解决????
streamToPrint = new StreamReader("C:\\t1.doc",System.Text.Encoding.GetEncoding("GB2312"));此时System.Text.Encoding.GetEncoding("GB2312")参数就不能使用了,System.Text.Encoding.GetEncoding("GB2312")参数支持.txt文件,不支持.doc文件
可是应该是用什么参数我不知道,大家又知道的么?
或者.doc文件根本不能用数据流读出打印?这个我了解得不多
如果打印成功,当关闭应为打印而打开的word进程的时候,应为是在本机操作word,这两个进程同时操作了一个word后台文件,所以当第二个进程打算关闭这个后台文件的时候,第一个进程又不允许它关,因此又会出现保存后台文件的对话框(恼火),这个打印一个当前显示的.doc文件竟然出现了这么大的麻烦,真不知道微软打印它的word文件是怎么实现的,大家有没有什么好的想法拉兄弟一把附上打印类代码:
public class Printing //打印当前程序所在路经的ti.doc文件的打印类{ public Printing() {
}
public bool Bln_PrintAllFiles(string StrFileName) { Word.Application ObjWord =new Word.Application(); //Create Word Application
//wd = new Word.ApplicationClass() 类
//ObjWord =new Word.Application() 接口 object M=Type.Missing; //M is missing...hi..hi object V= StrFileName; //Convert to Object.. object I=false; //Convert to Object
try { if(StrFileName.EndsWith(".doc")) //Ends with .doc. itz Word document.... { ObjWord.Documents.Open(ref V,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M); ObjWord.PrintOut(ref I,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M,ref M); ObjWord.Quit(ref M,ref M,ref M); return true;
} else //for all other types..I did check for .txt,.pdf and .Msg
{
System.Diagnostics.Process P=new Process(); //Create a process P.StartInfo.FileName = StrFileName.ToString(); //convert ....> Readable P.StartInfo.CreateNoWindow = true; //Perfect...Create No Window P.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//WindowStyle-HIDDEN P.StartInfo.Verb = "print"; P.Start(); //Start the process P.Dispose(); ObjWord.Quit(ref M,ref M,ref M); return true; } } catch(Exception ee) //Err Handler..... {
ObjWord.Quit(ref M,ref M,ref M); MessageBox.Show(ee.Message);
return false; }
} }}