RDLC报表(六)


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

<DeviceInfo> 
    
<OutputFormat>EMF</OutputFormat> 
    
<PageWidth>21cm</PageWidth> 
    
<PageHeight>29.70cm</PageHeight> 
    
<MarginTop>2cm</MarginTop> 
    
<MarginLeft>2cm</MarginLeft> 
    
<MarginRight>2cm</MarginRight> 
    
<MarginBottom>2cm</MarginBottom> 
</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结构使用的类:

using System;
using System.Collections.Generic;
using System.Text;

namespace RDLCReport
{
    
public class EMFDeviceInfo
    
{
        
private bool m_Landscape = false;

        
public bool Landscape
        
{
            
get
            
{
                
return this.m_Landscape;
            }

            
set
            
{
                
this.m_Landscape = value;
            }

        }


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

        
private int m_ColorDepth = 24;

        
public int ColorDepth
        
{
            
get
            
{
                
return this.m_ColorDepth;
            }

        }



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

        
private int m_Columns = 0;

        
public int Columns
        
{
            
get
            
{
                
return this.m_Columns;
            }

            
set
            
{
                
this.m_Columns = value;
            }

        }



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

        
private int m_ColumnSpacing = 0;

        
public int ColumnSpacing
        
{
            
get
            
{
                
return this.m_ColumnSpacing;
            }

            
set
            
{
                
this.m_ColumnSpacing = value;
            }

        }


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

        
private int m_DpiX = 96;

        
public int DpiX
        
{
            
get
            
{
                
return this.m_DpiX;
            }

            
set
            
{
                
this.m_DpiX = value;
            }

        }



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

        
private int m_DpiY = 96;

        
public int DpiY
        
{
            
get
            
{
                
return this.m_DpiY;
            }

            
set
            
{
                
this.m_DpiY = value;
            }

        }



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

        
private int m_EndPage = 0;

        
public int EndPage
        
{
            
get
            
{
                
return this.m_EndPage;
            }

            
set
            
{
                
this.m_EndPage = value;
            }

        }



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

        
private int m_StartPage = 1;

        
public int StartPage
        
{
            
get
            
{
                
return this.m_StartPage;
            }

            
set
            
{
                
this.m_StartPage = value;
            }

        }


        
/*
         * 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.
         * 
         * 底部边距,必须加上单位如"in"
         * 
         
*/

        
private decimal m_MarginBottom = 0;

        
public decimal MarginBottom
        
{
            
get
            
{
                
return this.m_MarginBottom;
            }

            
set
            
{
                
this.m_MarginBottom = value;
            }

        }



        
/*
         * 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.
         * 
         * 顶部边距,必须加上单位如"in"
         * 
         
*/

        
private decimal m_MarginTop = 0;

        
public decimal MarginTop
        
{
            
get
            
{
                
return this.m_MarginTop;
            }

            
set
            
{
                
this.m_MarginTop = value;
            }

        }



        
/*
         * 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.
         * 
         * 左边距,必须加上单位如"in"
         * 
         
*/

        
private decimal m_MarginLeft = 0;

        
public decimal MarginLeft
        
{
            
get
            
{
                
return this.m_MarginLeft;
            }

            
set
            
{
                
this.m_MarginLeft = value;
            }

        }



        
/*
         * 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.
         * 
         * 右边距,必须加上单位如"in"
         * 
         
*/

        
private decimal m_MarginRight = 0;

        
public decimal MarginRight
        
{
            
get
            
{
                
return this.m_MarginRight;
            }

            
set
            
{
                
this.m_MarginRight = value;
            }

        }


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

        
private string m_OutputFormat = "EMF";

        
public string OutputFormat
        
{
            
get
            
{
                
return this.m_OutputFormat;
            }

            
set
            
{
                
this.m_OutputFormat = value;
            }

        }


        
/*
         * 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.
         * 
         * 页面高度,必须加上单位如"in"
         * 
         
*/

        
private decimal m_PageHeight = 0;

        
public decimal PageHeight
        
{
            
get
            
{
                
return this.m_PageHeight;
            }

            
set
            
{
                
this.m_PageHeight = value;
            }

        }



        
/*
         * 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.
         * 
         * 页面宽度,必须加上单位如"in"
         * 
        
*/

        
private decimal m_PageWidth = 0;

        
public decimal PageWidth
        
{
            
get
            
{
                
return this.m_PageWidth;
            }

            
set
            
{
                
this.m_PageWidth = value;
            }

        }


        
/// <summary>
        
/// 返回包含DeviceInfo的字符串
        
/// </summary>

        public string DeviceInfoString
        
{
            
get
            
{
                
string strRet = string.Empty;

                strRet 
+= "<DeviceInfo>" +
                    
"  <OutputFormat>" + this.m_OutputFormat + "</OutputFormat>";

                
if (this.m_Landscape)
                    strRet 
+=
                        
"  <PageWidth>" + this.m_PageHeight.ToString() + "cm</PageWidth>" +
                        
"  <PageHeight>" + this.m_PageWidth.ToString() + "cm</PageHeight>";
                
else
                    strRet 
+=
                        
"  <PageWidth>" + this.m_PageWidth.ToString() + "cm</PageWidth>" +
                        
"  <PageHeight>" + this.m_PageHeight.ToString() + "cm</PageHeight>";

                strRet 
+=
                        
"  <MarginTop>" + this.m_MarginTop.ToString() + "cm</MarginTop>" +
                        
"  <MarginLeft>" + this.m_MarginLeft.ToString() + "cm</MarginLeft>" +
                        
"  <MarginRight>" + this.m_MarginRight.ToString() + "cm</MarginRight>" +
                        
"  <MarginBottom>" + this.m_MarginBottom.ToString() + "cm</MarginBottom>";

                strRet 
+= "</DeviceInfo>";

                
return strRet;

            }


        }
   
    }

}

        好了,解决了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报表(五)

       


posted @ 2006-03-04 12:06  蜡人张  阅读(29618)  评论(5编辑  收藏  举报