我想打印我的TextBox
,这是我的代码:
private void MenuItemPrint()
{
if (FileName != "")
{
PrintDocument document = new PrintDocument();
document.PrinterSettings.PrintFileName = FileName;
document.Print();
}
}
它不起作用。我该怎么办?
答案 0 :(得分:4)
试试这个:
private void MenuItemPrint()
{
if (!FileName.Trim().Equals(""))
{
using(PrintDocument pd = new PrintDocument())
{
using(PrintDialog printDialog=new PrintDialog())
{
if(printDialog.ShowDialog()==DialogResult.Yes)
{
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
pd.Print();
}
}
}
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
ev.Graphics.DrawString(FileName, new Font("Arial", 10), Brushes.Black,
ev.MarginBounds.Left, 0, new StringFormat());
}
答案 1 :(得分:4)
您可以使用以下代码使用所选的打印机打印文本框的内容:
private void PrintTextBoxContent()
{
#region Printer Selection
PrintDialog printDlg = new PrintDialog();
#endregion
#region Create Document
PrintDocument printDoc = new PrintDocument();
printDoc.DocumentName = "Print Document";
printDoc.PrintPage += printDoc_PrintPage;
printDlg.Document = printDoc;
#endregion
if(printDlg.ShowDialog() == DialogResult.OK)
printDoc.Print();
}
void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawString(this.textBox1.Text, this.textBox1.Font, Brushes.Black, 10, 25);
}
答案 2 :(得分:3)
试试以下代码
private void MenuItemPrint()
{
if(!string.IsNullOrEmpty(FileName.Trim())
{
PrintDialog printdg = new PrintDialog();
if (printdg.ShowDialog() == DialogResult.OK)
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings = printdg.PrinterSettings;
pd.PrintPage += PrintPage;
pd.Print();
pd.Dispose();
}
}
}
private void PrintPage(object o, PrintPageEventArgs e)
{
e.Graphics.DrawString(FileName, new Font("Arial", 20), Brushes.Black, 10, 25);
}