記事本列印功能的一段代碼

來源:互聯網
上載者:User

  最近要做一個合約列印的功能,合約中的文字是富文本,有一定格式,但是不限填寫的內容.在網上也沒有找到相關的文章,下面寫了一個記事本列印的功能, 不過只能做到都是採用相同的字型,可以換頁處理.
         string strPrintText;
         private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
         {
             System.Drawing.Font font = new Font("宋體", 14);
             Graphics     g   = e.Graphics;
             float        cyFont = font.GetHeight(g);
                StringFormat strfmt = new StringFormat();
                RectangleF   rectfFull, rectfText;
                int          iChars, iLines;

                if (g.VisibleClipBounds.X < 0)     
                {
                    rectfFull = e.MarginBounds;
                }
                else                                 
                {
                    rectfFull = new RectangleF(
                    e.MarginBounds.Left - (e.PageBounds.Width -
                    g.VisibleClipBounds.Width) / 2,
                    e.MarginBounds.Top - (e.PageBounds.Height -
                    g.VisibleClipBounds.Height) / 2,
                    e.MarginBounds.Width, e.MarginBounds.Height);
                }

              

                rectfText = RectangleF.Inflate(rectfFull, 0, -2 * cyFont);//去掉二行,可以列印標題和頁碼

                int iDisplayLines = (int)Math.Floor(rectfText.Height / cyFont); //計算可以列印的行數
                rectfText.Height = iDisplayLines * cyFont; //計算可以正式列印的高度

                strfmt.Trimming = StringTrimming.Word;

                if (strPrintText.Length == 0)
                {
                    e.Cancel = true;
                    return;
                }
                g.DrawString(strPrintText, font, Brushes.Black,rectfText, strfmt); //所有文字全部列印

                g.MeasureString(strPrintText, font, rectfText.Size, strfmt, out iChars, out iLines); //擷取該地區能列印了多少個字元
                strPrintText = strPrintText.Substring(iChars); //把列印過的字元去掉,下次就沒有了.
                e.HasMorePages = (strPrintText.Length >0);//判斷是否還有字要列印.

       }
       
        private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
         
            strPrintText = richTextBox1.Text;
        }

 

對於富文本的處理,不知有沒有好的辦法.

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.