winform 打印 页边距设置

.Net技术 码拜 9年前 (2016-02-29) 3506次浏览
需要打印预览右边文字的边距和左边一样
winform 打印 页边距设置
本人的代码是这样写的,没有效果

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //打印方向(纵/横)       
            this.printDocument1.DefaultPageSettings.Landscape = false;
            //设置纸张类型
            PaperSize ps = new PaperSize("A4", (int)(5 * 100), (int)(10 * 100));
            printDocument1.DefaultPageSettings.PaperSize = ps;
            //边距,上下左右
            int leftMargin = e.MarginBounds.Left;
            int topMargin = e.MarginBounds.Top;
            int rightMargin = e.MarginBounds.Right;
            int bottomMargin = e.MarginBounds.Bottom;
            int nWidth = e.MarginBounds.Width - rightMargin - leftMargin;
            int nHeight = e.MarginBounds.Height - topMargin - bottomMargin; 
            printDocument1.DefaultPageSettings.Margins = new Margins(leftMargin, rightMargin, topMargin, bottomMargin);
            float linesPerPage = 0; //记录每页最大行数
            float yPos = 0;//记录将要打印的一行数据在垂直方向的位置
            int count = 0;  //记录每页已打印行数
            string line = null;//从RichTextBox中读取一段字符将存到line中
            //每页最大行数=一页纸打印区域的高度/一行字符的高度
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics); //假如当前页已打印行数小于每页最大行数而且读出数据不为null,继续打印
            while (count < linesPerPage && ((line = streamToPrint.ReadLine()) != null))
            {
                yPos = topMargin + (count * printFont.GetHeight(e.Graphics)); //yPos为要打印的当前行在垂直方向上的位置
                e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());  //打印,参见第五章
                count++;//已打印行数加1
            }
            if (line != null)//能否需要打印下一页
                e.HasMorePages = true;//需要打印下一页
            else
                e.HasMorePages = false;//不需要打印下一页
        }

应该怎么修改,麻烦帮本人看看,谢谢!

解决方案

100

RectangleF rectF1 = new RectangleF(30, 10, 100, 122);
e.Graphics.DrawString(text1, font1, Brushes.Blue, rectF1);
/* 
      text1:要绘制的文本
      font1:文本的字体
      Brushes:笔刷,上面是蓝色的笔刷
       rectF1:指定的矩形,RectangleF 类型,四个参数分别是:开始点的横坐标、开始点的纵坐标、宽度、高度。(文本绘制的位置在矩形范围内自动换行)
*/

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明winform 打印 页边距设置
喜欢 (0)
[1034331897@qq.com]
分享 (0)