天天看點

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;

轉載于:https://www.cnblogs.com/zcm123/p/6535172.html

c#