2011-11-14 7 views
1

C#으로 주소록 WinForm을 만들었으며이를 텍스트 파일로 인쇄하는 방법을 알고 싶습니다. 어떻게해야합니까?Windows Form을 인쇄하는 방법

DataGridView에 모든 것을 표시 했으므로 테이블의 정보를 텍스트로 인쇄하는 것이 가장 이상적입니다. 윈도우에서

답변

0

당신이하고 싶은 것에 대한 자세한 내용을 제공해야합니다.

어떻게 양식을 텍스트 파일로 인쇄 하시겠습니까? 어떻게 레이블, 버튼 및 기타 컨트롤과 같은 그래픽을 텍스트로 변환합니까?

당신이 가능하며이 두 가지 그래픽 렌더링이나 텍스트 만에 인쇄 된 내용의 모든 측면을 제어 할 수 있습니다 물어 출발점으로 여기 봐 가지고 무엇을 :

Windows Forms Print Support

-1

simpliest 방법이다 텍스트 파일을 만들고 그 안에 값을 쓰십시오. 이처럼 :

var textFile = File.CreateText("Address.txt"); 
textFile.WriteLine("Name: Fischermaen"); 
textFile.Close(); 
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."); 
    } 
} 
+0

... 버튼을 클릭하여 텍스트 파일로 양식을 인쇄 할 수 있습니다 저장 함수에서 txtFile이 나오는 부분을 전달합니까? – JaredH20

관련 문제