有个html或者txt 在 C:\abc.html(txt) 处,我想写个winform直接打印出该文件,请问是否可行?代码如何写?谢谢!

解决方案 »

  1.   

     private void mnuFileOpen_Click(object sender, System.EventArgs e)
            {
                if (dlgOpen.ShowDialog()==DialogResult.OK)
                {
                    StreamReader sr=null;
                    try
                    {
                        sr=new StreamReader(dlgOpen.FileName,Encoding.Default,true);
                        txtDoc.Text=sr.ReadToEnd();
                    }
                    catch
                    {
                        MessageBox.Show("打开文件失败!","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
                    finally
                    {
                        if (sr!=null) sr.Close();
                    }
                }
            }        private void mnuFormatFont_Click(object sender, System.EventArgs e)
            {
                dlgFont.Font=txtDoc.Font;
                if (dlgFont.ShowDialog()==DialogResult.OK)
                {
                    txtDoc.Font=dlgFont.Font;
                }
            }        private void pdoc_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
            {
                //MessageBox.Show("开始打印啦");
            }        private void pdoc_EndPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
            {
                //MessageBox.Show("打印结束");
            }        private void pdoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
            {
                Graphics g=e.Graphics;
                float lineHeight=txtDoc.Font.GetHeight(g);
                int linesPerPage=(int)(e.MarginBounds.Height/lineHeight);
                int count=0;  //本页已打印行数            while (count<linesPerPage && totalLines<txtDoc.Lines.Length)
                {
                    g.DrawString(txtDoc.Lines[totalLines],txtDoc.Font,Brushes.Black,e.MarginBounds.X,e.MarginBounds.Y+lineHeight*count);
                    count++;
                    totalLines++;
                }            if (totalLines<txtDoc.Lines.Length)
                {
                    e.HasMorePages=true;
                }
                else
                {
                    e.HasMorePages=false;
                    totalLines=0;
                }
            }        private void mnuFilePrint_Click(object sender, System.EventArgs e)
            {
                if (dlgPrinterSetup.ShowDialog()==DialogResult.OK)
                {
                    pdoc.Print();  //开始执行打印
                }
            }
            private void mnuFilePrintPreview_Click(object sender, System.EventArgs e)
            {
                ppd.ShowDialog();
            }        private void mnuFilePageSetup_Click(object sender, System.EventArgs e)
           {
                Margins oldMargins=dlgPageSetup.PageSettings.Margins;
                dlgPageSetup.PageSettings.Margins=new Margins((int)(oldMargins.Left*2.54),(int)(oldMargins.Right*2.54),(int)(oldMargins.Top*2.54),(int)(oldMargins.Bottom*2.54));
                if (dlgPageSetup.ShowDialog()==DialogResult.Cancel)
                {
                    dlgPageSetup.PageSettings.Margins=oldMargins;
                }
            }
        }
    }
      

  2.   

    http://msdn.microsoft.com/zh-cn/library/9yfktb35(VS.80).aspx
    这个也可以参考!
      

  3.   

    你是说print到screen?还是要用printer?
      

  4.   

    认真看了下代码,如果目标文件是个 html ,那么好像就会打印html源代码出来吧?
    是否可以打印html布局页面出来呢?
      

  5.   

    Guyschaos,请问是否有办法解决呢?当然最好不要解析 html
      

  6.   

    放到 webbrowser 内,然后 webbrowser1.Print() 就成功了,非常方便,超级感谢 Guyschaos,亲个,。。