RDLC报表(六)


        你可能已经注意到了在调用LocalReport的Render方法时用到了一个XML格式的DeviceInfo结构,在SQL Server 2005 Report Services中,DeviceInfo结构是为了给特定的呈现格式传递参数。来看一个简单的DeviceInfo结构:

None.gif < DeviceInfo >  
None.gif    
< OutputFormat > EMF </ OutputFormat >  
None.gif    
< PageWidth > 21cm </ PageWidth >  
None.gif    
< PageHeight > 29.70cm </ PageHeight >  
None.gif    
< MarginTop > 2cm </ MarginTop >  
None.gif    
< MarginLeft > 2cm </ MarginLeft >  
None.gif    
< MarginRight > 2cm </ MarginRight >  
None.gif    
< MarginBottom > 2cm </ MarginBottom >  
None.gif
</ DeviceInfo >

        这个简单的DeviceInfo结构至少为LocalReport的Render方法指定了输出格式、页宽、页高、左边距、右边距、下边距信息,在我们使用PrintPage的方法将LocalReport呈现为EMF图片时,EMF图片在页面上显示的大小、边距就是由这个DeviceInfo结构来决定的,如果为DeviceInfo结构和PrintDocumnt设置不匹配的页面大小或边距,那么在PrintPage事件中使用DrawImage方法画出的图片将出现放大或缩小的情况,这是我们不愿意看到的结果。也就是说,在使用自定义纸张进行单据打印时,我们不仅要为PrintDocument设置页面大小和边距,还要为LocalReport设置与PrintDocument相同的页面大小和边距。关于DeviceInfo的结构,可以参考http://msdn2.microsoft.com/zh-cn/library/ms155373.aspx

        下面是我封装的一个为生成DeviceInfo结构使用的类:

None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  RDLCReport
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class EMFDeviceInfo
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private bool m_Landscape = false;
InBlock.gif
InBlock.gif        
public bool Landscape
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_Landscape;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_Landscape = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The pixel depth of the color range supported by the image output. 
InBlock.gif         * Valid values are 1, 4, 8, 24, and 32. 
InBlock.gif         * The default value is 24. 
InBlock.gif         * ColorDepth is only supported for TIFF rendering and is otherwise ignored by the report server for other image output formats. 
InBlock.gif         * Note: 
InBlock.gif         * For this release of SQL Server, the value of this setting is ignored, and the TIFF image is always rendered as 24-bit.
InBlock.gif         * 
InBlock.gif         * 默认值为24,且只有当输出格式为TIFF时才该项设置才起作用
InBlock.gif         * 
ExpandedSubBlockEnd.gif        
*/

InBlock.gif        
private int m_ColorDepth = 24;
InBlock.gif
InBlock.gif        
public int ColorDepth
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_ColorDepth;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The number of columns to set for the report. This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 未用到此项设置
InBlock.gif         * 
ExpandedSubBlockEnd.gif        
*/

InBlock.gif        
private int m_Columns = 0;
InBlock.gif
InBlock.gif        
public int Columns
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_Columns;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_Columns = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The column spacing to set for the report. This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 未用到此项设置
InBlock.gif         * 
ExpandedSubBlockEnd.gif        
*/

InBlock.gif        
private int m_ColumnSpacing = 0;
InBlock.gif
InBlock.gif        
public int ColumnSpacing
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_ColumnSpacing;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_ColumnSpacing = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The resolution of the output device in x-direction. The default value is 96.
InBlock.gif         * 
InBlock.gif         * 解析度,默认值为96
InBlock.gif         * 
ExpandedSubBlockEnd.gif        
*/

InBlock.gif        
private int m_DpiX = 96;
InBlock.gif
InBlock.gif        
public int DpiX
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_DpiX;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_DpiX = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The resolution of the output device in y-direction. The default value is 96.
InBlock.gif         * 
InBlock.gif         * 解析度,默认值为96
InBlock.gif         * 
ExpandedSubBlockEnd.gif        
*/

InBlock.gif        
private int m_DpiY = 96;
InBlock.gif
InBlock.gif        
public int DpiY
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_DpiY;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_DpiY = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The last page of the report to render. The default value is the value for StartPage.
InBlock.gif         * 
InBlock.gif         * 要输出的报表的最后一页
InBlock.gif         * 
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private int m_EndPage = 0;
InBlock.gif
InBlock.gif        
public int EndPage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_EndPage;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_EndPage = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The first page of the report to render. A value of 0 indicates that all pages are rendered. The default value is 1.
InBlock.gif         * 
InBlock.gif         * 起始页,0代表所有页面都将输出,默认值为1。
InBlock.gif         * 
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private int m_StartPage = 1;
InBlock.gif
InBlock.gif        
public int StartPage
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_StartPage;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_StartPage = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The bottom margin value, in inches, to set for the report. You must include an integer or decimal value followed by "in" (for example, 1in). This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 底部边距,必须加上单位如"in"
InBlock.gif         * 
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private decimal m_MarginBottom = 0;
InBlock.gif
InBlock.gif        
public decimal MarginBottom
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_MarginBottom;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_MarginBottom = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The top margin value, in inches, to set for the report. You must include an integer or decimal value followed by "in" (for example, 1in). This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 顶部边距,必须加上单位如"in"
InBlock.gif         * 
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private decimal m_MarginTop = 0;
InBlock.gif
InBlock.gif        
public decimal MarginTop
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_MarginTop;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_MarginTop = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The left margin value, in inches, to set for the report. You must include an integer or decimal value followed by "in" (for example, 1in). This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 左边距,必须加上单位如"in"
InBlock.gif         * 
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private decimal m_MarginLeft = 0;
InBlock.gif
InBlock.gif        
public decimal MarginLeft
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_MarginLeft;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_MarginLeft = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The right margin value, in inches, to set for the report. You must include an integer or decimal value followed by "in" (for example, 1in). This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 右边距,必须加上单位如"in"
InBlock.gif         * 
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private decimal m_MarginRight = 0;
InBlock.gif
InBlock.gif        
public decimal MarginRight
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_MarginRight;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_MarginRight = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * One of the Graphics Device Interface (GDI) supported output formats: BMP, EMF, GIF, JPEG, PNG, or TIFF.
InBlock.gif         * 
InBlock.gif         * 图形设备接口(GDI)支持的一种输出格式,可以是BMP, EMF, GIF, JPEG, PNG, 或 TIFF.
InBlock.gif         * 此处使用EMF
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private string m_OutputFormat = "EMF";
InBlock.gif
InBlock.gif        
public string OutputFormat
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_OutputFormat;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_OutputFormat = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The page height, in inches, to set for the report. You must include an integer or decimal value followed by "in" (for example, 11in). This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 页面高度,必须加上单位如"in"
InBlock.gif         * 
ExpandedSubBlockEnd.gif         
*/

InBlock.gif        
private decimal m_PageHeight = 0;
InBlock.gif
InBlock.gif        
public decimal PageHeight
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_PageHeight;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_PageHeight = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//*
InBlock.gif         * The page width, in inches, to set for the report. You must include an integer or decimal value followed by "in" (for example, 8.5in). This value overrides the report's original settings.
InBlock.gif         * 
InBlock.gif         * 页面宽度,必须加上单位如"in"
InBlock.gif         * 
ExpandedSubBlockEnd.gif        
*/

InBlock.gif        
private decimal m_PageWidth = 0;
InBlock.gif
InBlock.gif        
public decimal PageWidth
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return this.m_PageWidth;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.m_PageWidth = value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回包含DeviceInfo的字符串
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public string DeviceInfoString
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string strRet = string.Empty;
InBlock.gif
InBlock.gif                strRet 
+= "<DeviceInfo>" +
InBlock.gif                    
"  <OutputFormat>" + this.m_OutputFormat + "</OutputFormat>";
InBlock.gif
InBlock.gif                
if (this.m_Landscape)
InBlock.gif                    strRet 
+=
InBlock.gif                        
"  <PageWidth>" + this.m_PageHeight.ToString() + "cm</PageWidth>" +
InBlock.gif                        
"  <PageHeight>" + this.m_PageWidth.ToString() + "cm</PageHeight>";
InBlock.gif                
else
InBlock.gif                    strRet 
+=
InBlock.gif                        
"  <PageWidth>" + this.m_PageWidth.ToString() + "cm</PageWidth>" +
InBlock.gif                        
"  <PageHeight>" + this.m_PageHeight.ToString() + "cm</PageHeight>";
InBlock.gif
InBlock.gif                strRet 
+=
InBlock.gif                        
"  <MarginTop>" + this.m_MarginTop.ToString() + "cm</MarginTop>" +
InBlock.gif                        
"  <MarginLeft>" + this.m_MarginLeft.ToString() + "cm</MarginLeft>" +
InBlock.gif                        
"  <MarginRight>" + this.m_MarginRight.ToString() + "cm</MarginRight>" +
InBlock.gif                        
"  <MarginBottom>" + this.m_MarginBottom.ToString() + "cm</MarginBottom>";
InBlock.gif
InBlock.gif                strRet 
+= "</DeviceInfo>";
InBlock.gif
InBlock.gif                
return strRet;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }
   
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

        好了,解决了DeviceInfo,现在来看一下如何在PrintDocument的PrintPage事件中向打印机输出由LocalReport呈现的EMF图片。使用的方法基本上就是在GotReportViewer的例程Print a report from a console app中使用的方法,但是需要指出的一点是例程中使用事件参数System.Drawing.Printing.PrintPageEventArgs类的Graphics属性的DrawImage方法向打印机输出EMF图片,在实际的应用中,发现DrawImage方法绘出的图片会出现放大或缩小的情况,即使为DrawImage方法指定了看起来正确的参数ev.Graphics.DrawImageUnscaledAndClipped(this.m_PageImage, ev.PageBounds);,我使用的方法是DrawImageUnscaledAndClipped,在为DeviceInfo结构和PrintDocument指定好适当且匹配的页面设置时,输出的结果是比较好的。

        待续……

        相关随笔:
                RDLC报表(一)
                RDLC报表(二)
                RDLC报表(三)
                RDLC报表(四)
                RDLC报表(五)

        14.gif


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值