C#打印自动换行

//打印内容
        string prtStr;
        StringFormat stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces, 0);
        int count, rows;
 /// <summary>
        /// 打印文档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
            //像素偏移方式,像素在水平和垂直距离上均偏移若干个单位,以进行高速锯齿消除。
            e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
            //也可以通过设置Graphics对不平平滑处理方式解决,代码如下: 
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            Font drawFont = new Font("宋体", 14, FontStyle.Bold);
            #region 换行逻辑
            SizeF sf = e.Graphics.MeasureString(prtStr, drawFont, e.MarginBounds.Size, stringFormat, out count, out rows);
            e.Graphics.DrawString(prtStr.Substring(0, count), drawFont, new SolidBrush(Color.Black), e.MarginBounds, stringFormat);
            prtStr = prtStr.Remove(0, count < prtStr.Length ? count : prtStr.Length);
            if (prtStr.Length > 0)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            #endregion
        }

转载地址https://blog.csdn.net/wrgoodliness/article/details/52703660

猜你喜欢

转载自blog.csdn.net/zhou_xuexi/article/details/86571186