从文本框中将文本打印到纸上
Printing Text To Paper From Text Boxes
我的 new Rectangle()
出现了一些编译错误 - 我有一个 windows 表单,其中有几个供用户输入的文本框和一个用于打印预览的按钮。当按下按钮进行打印预览时,我希望当前的 windows 表单显示在页面上。我正在尝试对文本框中的值进行编码,以显示在左上角,直接显示在页面下半部分打印的图像上方。这是我的语法,但出现多个编译错误。我设置错了什么?我觉得将文本从 win 表单写入打印文档应该相当简单,但我失败了!
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);
}
}
new RectangleF(0,0, m_SubHeaderTextFieldSize, StringAlignment.Near)
看来您向 RectangleF 的构造函数提供了错误的参数。有 2 个重载 - 首先接受一个 PointF 和一个你可以这样称呼它的大小:
new RectangleF(new PointF(0f,0f), m_SubHeaderTextFieldSize)
另外一个4分,这样称呼:
new RectangleF(0f,0f, m_SubHeaderTextFieldSize.Width, m_SubHeaderTextFieldSize.Height)
任何看起来 StringAlignment.Near
与 e.Graphics.DrawString
函数相关的方式...
试试这个代码:
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);
我的 new Rectangle()
出现了一些编译错误 - 我有一个 windows 表单,其中有几个供用户输入的文本框和一个用于打印预览的按钮。当按下按钮进行打印预览时,我希望当前的 windows 表单显示在页面上。我正在尝试对文本框中的值进行编码,以显示在左上角,直接显示在页面下半部分打印的图像上方。这是我的语法,但出现多个编译错误。我设置错了什么?我觉得将文本从 win 表单写入打印文档应该相当简单,但我失败了!
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);
}
}
new RectangleF(0,0, m_SubHeaderTextFieldSize, StringAlignment.Near)
看来您向 RectangleF 的构造函数提供了错误的参数。有 2 个重载 - 首先接受一个 PointF 和一个你可以这样称呼它的大小:
new RectangleF(new PointF(0f,0f), m_SubHeaderTextFieldSize)
另外一个4分,这样称呼:
new RectangleF(0f,0f, m_SubHeaderTextFieldSize.Width, m_SubHeaderTextFieldSize.Height)
任何看起来 StringAlignment.Near
与 e.Graphics.DrawString
函数相关的方式...
试试这个代码:
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);