如何使用PrintDialog打印多行文本框?

时间:2014-11-14 00:34:41

标签: c# winforms

我有一个TextBox(m_txtFieldToSearch),应该打印控件内容中的内容。我的方法当前打印一个空白页面。

        PrintDialog printDialog = new PrintDialog();
        PrintDocument printDoc = new PrintDocument();

        printDoc.DocumentName = GlobalConstants.AppName;

        printDialog.Document = printDoc;
        printDialog.AllowSelection = true;

        printDialog.AllowSomePages = true;

        if (printDialog.ShowDialog() == DialogResult.OK)
            printDoc.Print();

1 个答案:

答案 0 :(得分:1)

请检查这是否有帮助。

        private void button1_Click(object sender, EventArgs e)
        {
            PrintDialog printDialog = new PrintDialog();
            PrintDocument printDoc = new PrintDocument();

            //PrintPage event to draw the textbox contents on page
            printDoc.PrintPage +=new PrintPageEventHandler(printDoc_PrintPage);

            printDoc.DocumentName = "Print";

            printDialog.Document = printDoc;
            printDialog.AllowSelection = true;

            printDialog.AllowSomePages = true;

            if (printDialog.ShowDialog() == DialogResult.OK)
                printDoc.Print();
        }
        private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            //Font
            Font f = new System.Drawing.Font("Arial",10,FontStyle.Bold);

            //Brush
            Brush b = new SolidBrush(Color.Black);

            //Where to draw the string
            PointF p = new PointF(10,10);

            //Draw some strings into the graphics
            e.Graphics.DrawString(m_txtFieldToSearch.Text,f,b,p);
        }