C#中使用GDI+实现复杂打印

if( a != null )

 

                       {

 

                                a.Clear();

 

                       }

 

                       for( int m = 0; m < 3; m ++ )

 

                       {

 

                                ArrayList a1 = new ArrayList();

 

                                string x0 = "住院患者一日清单";

 

                                string x1 = "入院日期:2007-11-11    结算日期:2007-11-11 至 2007-11-15 ";

 

                                string x2 = "姓名:欧阳峰  住院号:20090909090   床:512 妇科";

 

                                string x3 = "项目编号*项目名称*单价*数量*单位";  // 注意其中的*标志

 

                                string[] x4 = new string[ 90 ]; // 假设有90条数据,不固定

 

                                for( int i = 0; i < 90; i ++ )

 

                                {

 

                                         x4[ i ] = "juejue1984*www.loyosoft.com*11.00*11*鸟"; // 注意其中的*标志

 

                                }

 

                                string x5 = "费用累计:2500*小计:400 ";

 

                                string x6 = "累计按金:3600*余额:1600 ";

 

                                string x7 = "”;

 

                                string x8 = "”;

 

                                a1.Add( x0 );

 

                                a1.Add( x1 );

 

                                a1.Add( x2 );

 

                                a1.Add( x3 );

 

                                a1.Add( x4 );

 

                                a1.Add( x5 );

 

                                a1.Add( x6 );

 

                                a1.Add( x7 );

 

                                a1.Add( x8 );

 

a.       Add( a1 );

 

 

 

 

然后在PrintDocument对象的PrinPage事件作如下处理:

 

Size sTotal = new Size( 420, e.PageBounds.Height );

 

Graphics g = e.Graphics;

 

Font fTitle = new Font( "宋体", 16 ); // 标题字体

 

int titleHeight = 20; // 标题的高度

 

int textHeight = 13; // 普通文本的高度

 

Font fText = new Font( "宋体", 9 ); // 文本字体

 

 

 

 

int top = 30; // 顶部间距

 

int  left = 10; // 左边距

 

int right = 10; // 右边距

 

int titleMargin = 15; // 标题和正文行间距

 

int textMargin = 5; // 行间距

 

int rectangleMargin = 3; // 边框和文本间距

 

int oneHeight = 19; // 一行文本 + 2 * 行间距的高度

 

int oneWidth = sTotal.Width - left - right; // 打印内容的宽度

 

 

 

 

int y = 0;

 

while ( j < a.Count ) // Count为要打印的患者个数

 

{

 

       ArrayList a3 = a[ j ] as ArrayList; // a3位要打印的患者的清单内容。

 

       if( y >= e.PageBounds.Height - titleHeight  ) // 如果已经到了底部则开始下一页

 

       {

 

              e.HasMorePages = true;

 

              return;

 

       }

 

       //

 

       string x = a3[ 0 ] as string;

 

       if( !this.JudgeString( x ) ) // 如果此行记录还未打印过

 

       {

 

             

 

              //string x = "住院患者一日清单"; // a1

 

              SizeF sTitle = g.MeasureString( x, fTitle ); // 计算标题的长度和高度

 

              Point pTitle = new Point( ( sTotal.Width - (int)sTitle.Width ) / 2, y + top ); // 使标题绝对居中

 

              y += top; // 1

 

              //

 

              g.DrawString( x, fTitle, Brushes.Black, pTitle.X, y ); // 标题

 

              this.AddPrintedSign( ref x );

 

              a3[ 0 ] = x; // 标记该患者的此行内容为已经打印过

 

              y +=  titleHeight + titleMargin; // Y坐标下移

 

       }

 

 

 

 

      

 

       //

 

       if( y >= e.PageBounds.Height - oneHeight  )

 

       {

 

              e.HasMorePages = true;

 

              return;

 

       }

 

 

 

 

       //// 正文第一行

 

       string r1 = a3[ 1 ] as String;

 

       if( !this.JudgeString( r1 ) )

 

       {

 

              g.DrawString( r1, fText, Brushes.Black, left,  y );

 

              this.AddPrintedSign( ref r1 );

 

              a3[ 1 ] = r1;

 

              y += textHeight + textMargin; // 3

 

       }

 

 

 

 

       //

 

       if(  y >= e.PageBounds.Height - oneHeight  )

 

       {

 

              e.HasMorePages = true;

 

              return;

 

       }

 

       //

 

 

 

 

       //// 正文第二行

 

       string r2 = a3[ 2 ] as String;

 

       if( !this.JudgeString( r2 ) )

 

       {

 

              g.DrawString( r2, fText, Brushes.Black, left,  y  );

 

              this.AddPrintedSign( ref r2 );

 

              a3[ 2 ] = r2;

 

              y += textHeight + textMargin; // 4

 

       }

 

 

 

 

       //

 

       if( y >= e.PageBounds.Height - oneHeight  )

 

       {

 

              e.HasMorePages = true;

 

              return;

 

       }

 

       //

 

 

 

 

       ////内容

 

       string column = a3[ 3 ] as string;

 

       if( !this.JudgeString( column ) )

 

       {

 

              string[] c = column.Split( new char[] { '*' } );

 

              Rectangle R1 = new Rectangle( left, y, oneWidth,  oneHeight );

 

              g.DrawRectangle( Pens.Black, R1 );

 

              g.DrawLine( Pens.Black, new Point( R1.X + R1.Width * 4 / 16, R1.Y ), new Point( R1.X + R1.Width * 4 / 16, R1.Y + R1.Height ) );

 

              g.DrawLine( Pens.Black, new Point( R1.X + R1.Width * 10 / 16, R1.Y ), new Point( R1.X + R1.Width * 10 / 16, R1.Y + R1.Height ) );

 

              g.DrawLine( Pens.Black, new Point( R1.X + R1.Width * 12 / 16, R1.Y ), new Point( R1.X + R1.Width * 12 / 16, R1.Y + R1.Height ) );

 

              g.DrawLine( Pens.Black, new Point( R1.X + R1.Width * 14 / 16, R1.Y ), new Point( R1.X + R1.Width * 14 / 16, R1.Y + R1.Height ) );

 

 

 

 

              g.DrawString( c[ 0 ],  fText, Brushes.Black, R1.X + rectangleMargin, R1.Y + rectangleMargin );  // 项目编号 //a4.1

 

              g.DrawString( c[ 1 ],  fText, Brushes.Black, R1.X + rectangleMargin + R1.Width * 4 / 16, R1.Y + rectangleMargin );// 项目名称 // a4.2

 

              g.DrawString( c[ 2 ],  fText, Brushes.Black, R1.X + rectangleMargin + R1.Width * 10 / 16, R1.Y + rectangleMargin );//单价 //a4.3

 

              g.DrawString( c[ 3 ],  fText, Brushes.Black, R1.X + rectangleMargin + R1.Width * 12 / 16, R1.Y + rectangleMargin ); //数量 //a4.4

 

              g.DrawString( c[ 4 ],  fText, Brushes.Black, R1.X + rectangleMargin + R1.Width * 14 / 16, R1.Y + rectangleMargin );//单位 //a4.5

 

 

 

 

 

 

 

              this.AddPrintedSign( ref column );

 

              a3[ 3 ] = column;

 

              y += oneHeight;

 

       }

 

       //

 

       if( y >= e.PageBounds.Height - oneHeight  )

 

       {

 

              e.HasMorePages = true;

 

              return;

posted @ 2017-03-11 16:02  赤狐(zcm123)  阅读(378)  评论(0编辑  收藏  举报