C#Winform中打印预览时设置横向打印


因项目需要,在做一个Screen的打印时,因为Screen中的DataGridView的表格很长,需要横向排列才能完整的打印,因此设置PrintDocument.PrinterSettings.DefaultPageSettings.Landscape = true。但是在预览的时候仍然是纵向排列。伤脑筋。。。
花了一些时间做其他的设置均未有效。用Reflector查看PrintController的PrintLoop方法,PrintDocument的打印相关的方法被调用的顺序是这样的:
QueryPageSettings
StartPage
PrintPage
EndPage
目前只处理了StartPage和PrintPage。
于是加上了QueryPageSettings事件的处理,发现每次执行打印时QueryPageSettingsEventArgs.PageSettings.Landscape仍然为false,虽然前面设置了DefaultpageSettings.Landscape=true。
于是在此事件中将其指定为true,再次预览时,终于可以横向看到排列整齐的报表了。
private  void  button2_Click( object  sender, EventArgs e)
{
     PrintPreviewDialog pd =  new  PrintPreviewDialog();
     pd.Document =  new  PrintDocument();
     pd.Document.PrintPage +=  new  PrintPageEventHandler(Document_PrintPage);
     pd.Document.QueryPageSettings +=  new  QueryPageSettingsEventHandler(Document_QueryPageSettings);
     pd.Document.BeginPrint +=  new  PrintEventHandler(Document_BeginPrint);
     if  (pd.ShowDialog( this ) == DialogResult.OK)
     {
     }
}
  
void  Document_QueryPageSettings( object  sender, QueryPageSettingsEventArgs e)
{
     e.PageSettings.Landscape =  true ;
     int  index = -1;
     for  ( int  i=0;i<e.PageSettings.PrinterSettings.PaperSizes.Count;i++)
     {
         if  (e.PageSettings.PrinterSettings.PaperSizes[i].PaperName==  "A4" )
         {
             index=i;
             break ;
         }
     }
     if  (index != -1)
     {
         e.PageSettings.PaperSize = e.PageSettings.PrinterSettings.PaperSizes[index];
     }
}
  
void  Document_BeginPrint( object  sender, PrintEventArgs e)
{
}
  
void  Document_PrintPage( object  sender, PrintPageEventArgs e)
{
     using  (Bitmap bit =  new  Bitmap( this .listBox.Width,  this .listBox.Height))
     {
         this .listBox.DrawToBitmap(bit,  this .listBox.ClientRectangle);
         e.Graphics.DrawImage(bit,  new  Point(0, 0));
     }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM