c#實現預覽列印和列印【列印的位置不準確】

來源:互聯網
上載者:User

        //http://dotnet.chinaitlab.com/List_233.html
        private void button4_Click(object sender, EventArgs e)
        {
           
            //預覽功能,需要增加一個控制項:ThePrintDocument
            string strText = richTextBox1.Text;
            StringReader myReader = new StringReader(strText);
            PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
            printPreviewDialog1.Document = ThePrintDocument;
            printPreviewDialog1.FormBorderStyle = FormBorderStyle.Fixed3D;
            printPreviewDialog1.ShowDialog();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //列印功能,需要增加一個控制項:printDialog
            printDialog1.Document = ThePrintDocument;
            string strText = richTextBox1.Text;
            StringReader myReader = new StringReader(strText);
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                //這個方法將調用ThePrintDocument_PrintPage事件完成列印任務
                ThePrintDocument.Print();
            }
        }

        private void ThePrintDocument_PrintPage(object sender, PrintPageEventArgs ev)
        {
            //真正列印功能的實現
            string strText = richTextBox1.Text;
            StringReader myReader = new StringReader(strText);

            float linesPerPage = 0;
            float yPosition = 0;
            int count = 0;

            float leftMargin = ev.MarginBounds.Left;
            float topMargin = ev.MarginBounds.Top;
           
            string line = null;
            Font printFont = richTextBox1.Font;
            SolidBrush myBrush = new SolidBrush(Color.Black);
           
            //計算每一頁列印多少行
            linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
           
            //重複使用StringReader對象 ,列印出richTextBox1中的所有內容
            while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
            {
                // 計算出要列印的下一行所基於頁面的位置
                yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));
                // 列印出richTextBox1中的下一行內容
                ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }
            // 判斷如果還要下一頁,則繼續列印
            if (line != null)
                ev.HasMorePages = true;
            else
                ev.HasMorePages = false;
            myBrush.Dispose();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.