C#打印表格

C#-WinForm-打印控件

打印控件(全添加)

绘制如下窗体

一、PrintDocument -打印的基础  首先放置PrintDocument控件,双击事件PrintPage设置要打印的样式(李献策lxc)

  

复制代码
//第一步 设置打印属性
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //设置字体样式
            Font f = new System.Drawing.Font("微软雅黑", 16);
            f = richTextBox1.Font;
            //设置字体颜色
            Brush b = new SolidBrush(richTextBox1.ForeColor);

            //e.绘制.画字符串(要打印的文本,文本格式,画刷-颜色和纹理,位置坐标)
            e.Graphics.DrawString(richTextBox1.Text, f, b, 20, 10);
        }
        //字体样式设置
        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult dr = fontDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                richTextBox1.Font = fontDialog1.Font;
                richTextBox1.ForeColor = fontDialog1.Color;
            }
        }
复制代码

二、PageSetupDialog - 打印页面设置

复制代码
 //第二步 设置打印页面设置
        private void button1_Click(object sender, EventArgs e)
        {
            pageSetupDialog1.Document = printDocument1;
            pageSetupDialog1.ShowDialog();
        }
复制代码

三、1、PrintPreviewContol - 打印预览格式一,在窗体中设置预览区域

//第三步 打印预览一
        private void button2_Click(object sender, EventArgs e)
        {
            printPreviewControl1.Document = printDocument1;
        }

2、PrintPreviewDialog - 打印预览格式二,在弹窗中预览

复制代码
//第三步 打印预览二
        private void button2_Click(object sender, EventArgs e)
        {
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();
		
        }
 
    
 
    
复制代码

四、PrintDialog - 开始打印

复制代码
 //第四步 开始打印
        private void button3_Click(object sender, EventArgs e)
        {
            printDialog1.Document = printDocument1;
            DialogResult dr = printDialog1.ShowDialog();
            if (dr == DialogResult.OK)
            {
                printDocument1.Print();
            }
        }
复制代码

 

 
 
Pen blackPen = new Pen ( Color . Black , 1 ); //黑色,1像素
//打印样式
System . Drawing . Font f = new System . Drawing . Font ( "黑体" , 10 , System . Drawing . FontStyle . Regular );
System . Drawing . Font TableFont = new Font ( "宋体" , 10 , FontStyle . Regular );
System . Drawing . Font boldFont = new System . Drawing . Font ( "黑体" , 12 , System . Drawing . FontStyle . Bold );
System . Drawing . Font msgf = new System . Drawing . Font ( "黑体" , 14 , System . Drawing . FontStyle . Regular );
string [,] strArr = new string [ 2 , 6 ]; //声明一个二维数组.
strArr [ 0 , 0 ] = "类别" ;
strArr [ 0 , 1 ] = "老师" ;
strArr [ 0 , 2 ] = "工作单位" ;
strArr [ 0 , 3 ] = "信息大学" ;
strArr [ 0 , 4 ] = "收费标准" ;
strArr [ 0 , 5 ] = 12 + "元" ;
strArr [ 1 , 0 ] = "姓名" ;
strArr [ 1 , 1 ] = "陈小莉" ;
strArr [ 1 , 2 ] = "性别" ;
strArr [ 1 , 3 ] = "女" ;
strArr [ 1 , 4 ] = "年龄" ;
strArr [ 1 , 5 ] = "" + 20 ;
int docX = 20 ;
int docY = 50 ;
int tableW = 600 ; //表宽
int tableH = 2 * 50 ; //表高
int tableX = docX ; //表的起点X坐标
int tableY = docY ; //表的起点Y坐标
int rowWidth = 50 ; //行宽
int colwidth = tableW / 6 ; //列宽
for ( int i = 0 ; i <= 2 ; i ++)
{
e . Graphics . DrawLine ( blackPen , tableX , tableY + i * rowWidth , tableX + tableW , tableY + i * rowWidth );
for ( int j = 0 ; j <= 6 ; j ++)
{
if ( i != 2 )
{
e . Graphics . DrawLine ( blackPen ,
tableX + j * colwidth , tableY + i * rowWidth ,
tableX + j * colwidth , tableY + i * rowWidth + rowWidth );
if ( j != 6 )
{
if ( j % 2 == 0 )
{
int textWidth = Convert . ToInt32 ( e . Graphics . MeasureString ( strArr [ i , j ], boldFont ). Width );
int textHeight = Convert . ToInt32 ( e . Graphics . MeasureString ( strArr [ i , j ], boldFont ). Height );
int centerX = ( colwidth - textWidth ) / 2 ; //字体X轴居中
int centerY = ( rowWidth - textHeight ) / 2 ; //字体Y轴居中
e . Graphics . DrawString ( strArr [ i , j ], boldFont , Brushes . Black ,
tableX + j * colwidth + centerX , tableY + i * rowWidth + centerY ); //写列名
}
else
{
e . Graphics . DrawString ( strArr [ i , j ], TableFont , Brushes . Black ,
tableX + j * colwidth + 15 , tableY + i * rowWidth + 15 ); //写列名
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值