Click here to Skip to main content
16,022,333 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the code I have on Windows 11, Visual Studio and am trying to print a text file. Everthing works except I get a blank sheet from the printer with no text I am trying to print. Help would be appreciated
C#
#region - btnPrintRpt_Click - Start
/////////////////////////////////////////////////////////////////////////
//
// btnPrintRpt_Click 
//
///////////////////////////////////////////////////////////////////////// 
private void btnPrintRpt_Click(object sender, EventArgs e)
{
  try
  {
    PrintDialog printDlg = new PrintDialog();
    PrintDocument printDoc = new PrintDocument();
    printDoc.DocumentName = @"c:\MyData\Textfile.txt";
    printDlg.Document = printDoc;
    printDlg.AllowSelection = true;
    printDlg.AllowSomePages = true;
    if (printDlg.ShowDialog() == DialogResult.OK)
      printDoc.Print();
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message.ToString(), "btnPrintRpt_Click", MessageBoxButtons.OK);
  }
}
#endregion - btnPrintRpt_Click - End


What I have tried:

I have tried all obvious steps
Posted
Updated 29-Jun-24 8:11am
v2

1 solution

It seems you didn't read the documentation on PrintDocument[^]. You have to wire up the PrintDocument.PrintPage event and supply the code in your handler for it to draw everything on each page yourself.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900