RDLC报表(六)
??????? 你可能已經注意到了在調用LocalReport的Render方法時用到了一個XML格式的DeviceInfo結構,在SQL Server 2005 Report Services中,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報表(五)
???????
總結
- 上一篇: Apache2 之虚拟主机设置指南
- 下一篇: 在VB.NET中应用SQLDMO