Printing Text To Paper From Text Boxes

436 views Asked by At

I am getting a few compile errors with my new Rectangle() - I have a windows form with a few text boxes for user input, and a button to print preview. When the button is pressed to print preview, I want the current windows form to display on the page. I am trying to code the values from the text boxes to display in the top left directly above an image that prints at the bottom half of the page. This is the syntax that I have, but I am getting multiple compile errors. What have I set-up incorrectly? I feel like writing text form a win form to a print document should be fairly straightforward, but I am failing!

private void btnPreview_Click(object sender, EventArgs e)
{
  PrintPreviewDialog PrintPreviewDlg = new PrintPreviewDialog();

  PrintPreviewDlg.ClientSize = new System.Drawing.Size(400, 300);
  PrintPreviewDlg.Location = new System.Drawing.Point(29, 29);
  PrintPreviewDlg.Name = "PrintPreviewDlg";

  PrintPreviewDlg.MinimumSize = new System.Drawing.Size(375, 250);

  PrintPreviewDlg.WindowState = FormWindowState.Maximized;
  PrintPreviewDlg.UseAntiAlias = true;

  dynamic printData = CreatePrintDocument();
  printData.DefaultPageSettings.Landscape = true;
  PrintPreviewDlg.Document = printData;
  PrintPreviewDlg.ShowDialog();
}

printData CreatePrintDocument()
{
  printData document = new printData();
  document.SetParentCtrl(this);
  document.PrintData.txtAssignmentName = MainInstance.txtAssignmentName.Text;
  document.PrintData.txtAssignmentNumber = MainInstance.txtAssignmentNumber.Text;
  document.PrintData.txtPreparedBy = MainInstance.txtPreparedBy.Text;
  document.PrintData.txtAssignmentSection = MainInstance.txtAssignmentSection.Text;
  document.PrintData.DocumentName = "Testing Print Functionality";

  return document;
}


class printData : PrintDocument
{  
  Size m_SubHeaderTextFieldSize;
  int m_NormalRowHeight = 0;
  class DataToPrintData
  {
      public string txtAssignmentName, txtAssignmentNumber, txtPreparedBy, txtAssignmentSection;
  }
  protected override void OnPrintPage(PrintPageEventArgs e)
  {
    //More print specs here
    int LeftSubHeadingWidth = 200;
    m_SubHeaderTextFieldSize = new Size(LeftSubHeadingWidth, m_NormalRowHeight);
    string printData = "Project Name: " + projectNumberTitle + System.Environment.NewLine +
               "Prepared By: " + txtPreparedBy + System.Environment.NewLine +
               "Assignment Section: " + txtAssignmentSection + System.Environment.NewLine;

    e.Graphics.DrawString(e, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0,0, m_SubHeaderTextFieldSize, StringAlignment.Near);
  }
}
2

There are 2 answers

2
Aleks Andreev On BEST ANSWER

Try this code:

var format = new StringFormat {Alignment = StringAlignment.Near};
e.Graphics.DrawString(
    printData, 
    new Font("Times New Roman", 12), 
    new SolidBrush(Color.Black), 
    new RectangleF(new PointF(0, 0), m_SubHeaderTextFieldSize), 
    format);
0
igorushi On

new RectangleF(0,0, m_SubHeaderTextFieldSize, StringAlignment.Near) it looks like you are providing wrong parameters to RectangleF's constructor. There are 2 overloads - first accepts a PointF and a size you can call it like this:
new RectangleF(new PointF(0f,0f), m_SubHeaderTextFieldSize)
other one 4 points, call it like this:
new RectangleF(0f,0f, m_SubHeaderTextFieldSize.Width, m_SubHeaderTextFieldSize.Height)
any way it looks like StringAlignment.Near relates to e.Graphics.DrawString function ...