如何打印Windows窗体

时间:2011-11-14 09:38:39

标签: c# winforms printing

我在C#中创建了一个地址簿WinForm,想知道如何将其作为文本文件打印,我该怎么做呢?

我已经在DataGridView中显示了所有内容,理想情况下我只想将表格中的信息打印为文本。

4 个答案:

答案 0 :(得分:1)

你可以这样试试......

[STAThread]
  static void Main() 
  {
    Application.Run(new PrintPreviewDialog());
  }

  private void btnOpenFile_Click(object sender, System.EventArgs e)
  {
    openFileDialog.InitialDirectory = @"c:\";
    openFileDialog.Filter = "Text files (*.txt)|*.txt|" +
            "All files (*.*)|*.*";
    openFileDialog.FilterIndex = 1;              //  1 based index

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
      StreamReader reader = new StreamReader(openFileDialog.FileName);
      try
      {
        strFileName = openFileDialog.FileName;
        txtFile.Text = reader.ReadToEnd();
      }
      catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
        return;
      }
      finally
      {
        reader.Close();
      }
    }
  }

  private void btnSaveFile_Click(object sender, System.EventArgs e)
  {
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.InitialDirectory = @"c:\";
    sfd.Filter = "Text files (*.txt)|*.txt|" +
            "All files (*.*)|*.*";
    sfd.FilterIndex = 1;              //  1 based index

    if (strFileName != null)
      sfd.FileName = strFileName;
    else
      sfd.FileName = "*.txt";

    if (sfd.ShowDialog() == DialogResult.OK)
    {
      StreamWriter writer = new StreamWriter(strFileName,false);
      try
      {
        strFileName = sfd.FileName;
        writer.Write(txtFile.Text);
      }
      catch(Exception ex)
      {
        MessageBox.Show(ex.Message);
        return;
      }
      finally
      {
        writer.Close();
      }
    }
  }

//在这里,您可以通过单击按钮将表单打印为文本文件..

  private void btnPageSetup_Click(object sender, System.EventArgs e)
  {
    PageSetupDialog psd = new PageSetupDialog();
    psd.Document = printDocument;
    psd.ShowDialog();
  }

  private void btnPrint_Click(object sender, System.EventArgs e)
  {
    PrintDialog pdlg = new PrintDialog();
    pdlg.Document = printDocument;

    if (pdlg.ShowDialog() == DialogResult.OK)
    {
      try
      {
        printDocument.Print();
      }
      catch(Exception ex)
      {
        MessageBox.Show("Print error: " + ex.Message);
      }
    }
  }

  private void btnPrintPreview_Click(object sender, System.EventArgs e)
  {
    PrintPreviewDialog ppdlg = new PrintPreviewDialog();
    ppdlg.Document = printDocument;
    ppdlg.ShowDialog();
  }

  private void pdPrintPage(object sender, PrintPageEventArgs e)
  {
    float linesPerPage = 0;
    float verticalOffset = 0;
    float leftMargin = e.MarginBounds.Left;
    float topMargin = e.MarginBounds.Top;
    int linesPrinted = 0;
    String strLine = null;

    linesPerPage = e.MarginBounds.Height / currentFont.GetHeight(e.Graphics);

    while (linesPrinted < linesPerPage &&
        ((strLine = stringReader.ReadLine())!= null ))
    {
      verticalOffset = topMargin + (linesPrinted * currentFont.GetHeight(e.Graphics));
      e.Graphics.DrawString(strLine, currentFont, Brushes.Black, leftMargin, verticalOffset);
      linesPrinted++;
    }

    if (strLine != null)
      e.HasMorePages = true;
    else
      e.HasMorePages = false;

  }

  private void pdBeginPrint(object sender, PrintEventArgs e)
  {
    stringReader = new StringReader(txtFile.Text);
    currentFont = txtFile.Font;
  }

  private void pdEndPrint(object sender, PrintEventArgs e)
  {
    stringReader.Close();
    MessageBox.Show("Done printing.");
  }
}

答案 1 :(得分:0)

使用.NET打印命名空间从Windows窗体应用程序预览和打印 http://msdn.microsoft.com/en-us/magazine/cc188767.aspx

它有点旧(2003年),但看起来仍然相关。

答案 2 :(得分:0)

你应该详细说明你想做什么。

您打算如何将表单打印为文本文件?如何将标签,按钮和其他控件等图形转换为文本?

您的要求是可能的,您可以通过图形渲染或仅文本两种方式控制打印内容的各个方面,请看这里作为起点:

Windows Forms Print Support

答案 3 :(得分:0)

最简单的方法是创建一个文本文件并在其中写入值。像这样:

var textFile = File.CreateText("Address.txt");
textFile.WriteLine("Name: Fischermaen");
textFile.Close();