In nội dung từ TextBox ra máy in (In giấy)
Nếu bạn thực sự muốn in ra giấy nội dung của TextBox, bạn cần dùng PrintDocument.
▶ Ví dụ in nội dung từ TextBox:
using System.Drawing.Printing;
PrintDocument pd = new PrintDocument();
pd.PrintPage += (sender, e) =>
{
e.Graphics.DrawString(textBox1.Text, new Font("Arial", 12), Brushes.Black, new PointF(100, 100));
};
pd.Print(); // Bắt đầu in
🔹 Có thể thêm hộp thoại in như PrintDialog, PrintPreviewDialog nếu cần giao diện.
using System.Drawing;
using System.Drawing.Printing;
private void btnPrint_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.Pd_PrintPage);
PrintDialog printDlg = new PrintDialog();
printDlg.Document = pd;
if (printDlg.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
private void Pd_PrintPage(object sender, PrintPageEventArgs e)
{
string noiDung = textBox1.Text;
Font font = new Font("Arial", 12);
Brush brush = Brushes.Black;
float x = 100, y = 100;
e.Graphics.DrawString(noiDung, font, brush, new PointF(x, y));
}
In nội dung Textbox