我正在尝试使用C#打印服务器报告而不进行预览。该报告在WidowsXP中正常打印,但它在Windows 7和Windows 10的缩放视图中打印。以下是我的代码。
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;
namespace Dahlawi.ManPower.Recruiting.Forms
{
public partial class FrmReportViewer1 : Form
{
List<Metafile> m_ReportPages = new List<Metafile>();
int m_ReportPageIndex = 0;
public FrmReportViewer1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PrintDocument objPrintDoc = new PrintDocument();
objPrintDoc.PrintPage += PrintPageHandler;
PrintDialog objPrintDiag = new PrintDialog();
objPrintDiag.Document = objPrintDoc;
DialogResult objDiagResult = default(DialogResult);
objDiagResult = objPrintDiag.ShowDialog();
if (objDiagResult == DialogResult.OK)
{
objPrintDoc.PrinterSettings = objPrintDiag.PrinterSettings;
this.m_ReportPages = new List<Metafile>();
this.m_ReportPageIndex = 0;
this.reportViewer.ShowCredentialPrompts = false;
this.reportViewer.ServerReport.ReportServerCredentials.NetworkCredentials = new System.Net.NetworkCredential("USER", "PASSWORD", "DOMAIN");
this.reportViewer.ServerReport.ReportServerUrl = new Uri("http://SERVER/ReportServer");
this.reportViewer.ServerReport.ReportPath = "/DMR/Candidate_Information";
ReportParameter[] reportParameters = new ReportParameter[] { new ReportParameter("CandidateId", "16744") };
reportParameters[0].Visible = false;
this.reportViewer.ServerReport.SetParameters(reportParameters);
this.reportViewer.RefreshReport();
string deviceInfo = "<DeviceInfo>" + "<OutputFormat>emf</OutputFormat>" + " <PageWidth>8.5in</PageWidth>" + " <PageHeight>11in</PageHeight>" + " <MarginTop>0.2in</MarginTop>" + " <MarginLeft>0.5in</MarginLeft>" + " <MarginRight>0.5in</MarginRight>" + " <MarginBottom>0.2in</MarginBottom>" + "</DeviceInfo>";
Warning[] objWarnings = null;
string[] sStreamIDs = null;
string sMimeType = "";
string sEncoding = "";
string sFilenameExtension = "";
byte[] renderedPage = null;
renderedPage = this.reportViewer.ServerReport.Render("Image", deviceInfo, out sMimeType, out sEncoding, out sFilenameExtension, out sStreamIDs, out objWarnings);
this.m_ReportPages.Add(new Metafile(new MemoryStream(renderedPage)));
foreach (string sStreamId in sStreamIDs)
{
renderedPage = this.reportViewer.ServerReport.RenderStream("Image", sStreamId, deviceInfo, out sMimeType, out sEncoding);
this.m_ReportPages.Add(new Metafile(new MemoryStream(renderedPage)));
}
objPrintDoc.Print();
objPrintDoc = null;
}
}
private void PrintPageHandler(object sender, PrintPageEventArgs e)
{
if (this.m_ReportPageIndex < this.m_ReportPages.Count)
{
Metafile imgPage = this.m_ReportPages[this.m_ReportPageIndex];
this.m_ReportPageIndex += 1;
e.Graphics.DrawImage(imgPage, 0, 0, imgPage.Width, imgPage.Height);
e.HasMorePages = this.m_ReportPageIndex < this.m_ReportPages.Count;
}
}
}
}
当我在WindowsXP中使用上述代码打印报告时,它会打印出这样的Proper Print
但是,当我在Windows 7或Windows 10中运行相同的代码时,它会打印出像Zoomed Print
这样的代码请帮我在Windows 7 / Windows 10上运行此代码。如果需要,我可以提供更多详细信息。
答案 0 :(得分:0)
我发现了这个问题并且它是&#34; emf&#34;输出格式导致Windows 7和Windows 10上的问题,但在Windows XP / Windows 2003上正常工作。我在&#34; deviceInfo&#34;中更改了输出格式。来自&#34; emf&#34;的字符串到&#34; jpg&#34; (其他格式,例如&#34; png&#34;,&#34; jpeg&#34;,&#34; bmp&#34;也可以正常工作)然后我更改了&#34; m_ReportPages&#34;来自&#34; Metafile&#34;的列表对象to&#34; Image&#34;并解决了问题。