我在渲染 SSRS 服务器报告并从代码中打印它时遇到了困难。
尽管渲染成功并打印了报告,但图形质量的结果很差。该报告本身具有用于徽标的高分辨率 6kx1k GIF 图像。
我制作了下面的图片来说明使用 EMF 与 PDF 进行渲染之间的区别:
如您所见,字体看起来很棒(没有像素化),并且在 PDF 徽标中呈现时看起来不错。但是 EMF 版本的质量很差,正如您在徽标边缘看到的那样。
有人遇到过这个并解决了吗?
我正在运行 SQL Server 2008R2 SP2 (10.50.4000)
使用 EMF 呈现报告的代码
public List<byte[]> Render()
{
// Setting credentials and reporting services uri
ReportExecutionService reportService = new ReportExecutionService
{
Credentials = _credentials,
Url = Settings.Default.ReportingService
};
// Prepare report parameter.
ExecutionHeader execHeader = new ExecutionHeader();
reportService.ExecutionHeaderValue = execHeader;
reportService.LoadReport(_report, null);
reportService.SetExecutionParameters(_parameters.ToArray(), "en-us");
// Render
bool startup = true;
List<byte[]> streams = new List<byte[]>();
int numberOfPages = 0;
byte[] result = null;
while (startup || result.Length > 0)
{
startup = false;
string devInfo = String.Format(@"<DeviceInfo><OutputFormat>EMF</OutputFormat><PrintDpiX>150</PrintDpiX><PrintDpiY>150</PrintDpiY><StartPage>{0}</StartPage></DeviceInfo>", numberOfPages + 1);
string encoding, mimeType, extension;
string[] streamIDs = null;
Warning[] warnings = null;
result = reportService.Render("IMAGE", devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
if (result.Length > 0)
{
streams.Add(result);
}
numberOfPages++;
}
return streams;
}
这是打印文档的实现
public class PrintReport : PrintDocument
{
private int _currentPage;
private List<byte[]> _pages;
public PrintReport()
{
}
public List<byte[]> Pages
{
get { return _pages; }
set { _pages = value; }
}
protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
_currentPage = 0;
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
Stream pageToPrint = new MemoryStream(_pages[_currentPage]);
pageToPrint.Position = 0;
// Load each page into a Metafile to draw it.
using (Metafile pageMetaFile = new Metafile(pageToPrint))
{
Rectangle adjustedRect = new Rectangle(
e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
e.PageBounds.Width,
e.PageBounds.Height);
// Draw a white background for the report
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
e.Graphics.DrawImage(pageMetaFile, adjustedRect);
// Prepare for next page. Make sure we haven't hit the end.
_currentPage++;
e.HasMorePages = _currentPage < _pages.Count;
}
}
protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
{
e.PageSettings = (PageSettings)PrinterSettings.DefaultPageSettings.Clone();
}
}