请问一个c#中的打印问题

yangbc 2003-10-10 03:25:56
想请问一个c#中的打印问题,下面是我写的一段打印一个文件的代码,你看看这样写支持网络打印么,我用网络上的打印机,结果打出来的是空白页面,怎么解决?
private void button1_Click(object sender, System.EventArgs e)
{
printDialog1=new PrintDialog();
printDialog1.AllowPrintToFile =false;
PrintDocument docPrn = new PrintDocument();
docPrn.DocumentName="t1.txt";
printDialog1.Document=docPrn;
if(printDialog1.ShowDialog()==DialogResult.OK)
{
try
{
docPrn.Print();
}
catch
{
MessageBox.Show ("Error While Printing", "Print Error");
}
}
}
...全文
46 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
yangbc 2003-10-14
  • 打赏
  • 举报
回复
up
yaner2003 2003-10-13
  • 打赏
  • 举报
回复
up
yangbc 2003-10-13
  • 打赏
  • 举报
回复
wd_318(饭加加) ,这个想过,我用ildasm.exe看了Interop.Word.dll,眼睛都看花了,我注意到word的ApplicationClass()创建的wd = new Word.ApplicationClass()中是有一个printout方法,可是调用它的同时还是新打开了一个word进程,还是2个进程操作一个word文件的问题; 我现在想能不能打印的时候杀掉前面一个word进程,不过这样就看不到显示的文件了,最好在显示文件的同时可以打印当前显示的.doc文件。

不知大家有什么看法?????????????
wd_318 2003-10-13
  • 打赏
  • 举报
回复
能不能试着直接用调用WORD自身的打印程序?
yangbc 2003-10-13
  • 打赏
  • 举报
回复
自己给自己提一下,请大家关注我的问题,如果解决了,我会把代码共享出来
yangbc 2003-10-13
  • 打赏
  • 举报
回复
现在我的打印.doc文件的问题解决了,用到了一个打印类(后面我会附上代码),谢谢以上大家的帮助,但我不想结贴,我现在又有新的问题了:我现在的情况是我可以在我的程序理显示.doc文件,当我想打印的时候,我的代码是这样的,取得当前显示的.doc文件的路径,然后用打印类打印出来,问题出在当我显示.doc文件的时候,系统已经开启了一个word进程,如果我用这个打印类来打印此.doc文件,这个类会另外再打开一个word进程(这个可以通过windows任务管理器看到),如果对微软word了解的人都知道,word文件可以共享多人浏览,为了防止多人修改,word的安全措施允许多个进程打开同一个.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;

   }


  }

 }

}
yan1617 2003-10-12
  • 打赏
  • 举报
回复
up
yangbc 2003-10-12
  • 打赏
  • 举报
回复
问题还没解决,大家帮忙提提
ybc5211 2003-10-10
  • 打赏
  • 举报
回复
up
ybc5211 2003-10-10
  • 打赏
  • 举报
回复
问题还没解决,大家帮忙提提
yangbc 2003-10-10
  • 打赏
  • 举报
回复
问题还没解决,大家帮忙提提
yangbc 2003-10-10
  • 打赏
  • 举报
回复
打印word文件的文件格式为:.doc
streamToPrint = new StreamReader("C:\\t1.doc",System.Text.Encoding.GetEncoding("GB2312"));

此时System.Text.Encoding.GetEncoding("GB2312")参数就不能使用了,System.Text.Encoding.GetEncoding("GB2312")参数支持.txt文件,不支持.doc文件
可是应该是用什么参数我不知道,大家又知道的么?
或者.doc文件根本不能用数据流读出打印?这个我了解得不多
yangbc 2003-10-10
  • 打赏
  • 举报
回复
打印txt文件的中文乱码问题解决了,修改代码如下:
streamToPrint = new StreamReader("C:\\t1.txt",System.Text.Encoding.GetEncoding("GB2312"));

现在的问题是:如果我要打印word文档,现在打出来的全部是乱码了,怎么解决????
aspnetwuxueyou 2003-10-10
  • 打赏
  • 举报
回复
gz
yangbc 2003-10-10
  • 打赏
  • 举报
回复
谢谢 abcynic(门外汉) ,现在可以打印了,可是打印汉字的时候是乱码,该怎么解决????
abcynic 2003-10-10
  • 打赏
  • 举报
回复
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());
}
}
xiaodele 2003-10-10
  • 打赏
  • 举报
回复
打印页面需要自己画的吧,至于网络只要你的机器中指定了一台打印机就可以了

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧