打印rdlc文件WinForms时抛出异常

时间:2013-10-31 10:28:00

标签: c# exception reportviewer rdlc

我的rdlc报告存在问题。当我开始打印时,抛出了异常。我不知道它的类型是什么,但我有堆栈跟踪。

enter image description here

报告的路径是正确的。

这是我的代码:

ThreadPool.QueueUserWorkItem(o =>
        {
            var patientReportPresenter = PatientReportPresenter.Convert(patient);
            using (var demo = new PrintComponent(patientReportPresenter, applicationSettings.PrinterName, "DataSet1", false))
            {
                try
                {
                    string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
                    string reportPath = Path.Combine(exeFolder, @"Template\Report1.rdlc");

                    var report = new LocalReport
                    {
                        ReportPath = reportPath
                    };
                    demo.Run(report);
                }
                catch (Exception ex)
                {
                    string exeFolder = Path.GetDirectoryName(Application.ExecutablePath);
                    string reportPath = Path.Combine(exeFolder, @"Template\Report1.rdlc");

                    MessageBox.Show(
                        ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + reportPath,
                        @"Print error", MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                    return;
                }
            }
        }, null);

这是我的Print类:

public class PrintComponent : IDisposable
{
    private readonly string _printerName;
    private int m_currentPageIndex;
    private IList<Stream> m_streams;
    private readonly string _dataSetName;
    private bool _landscape;
    private PatientReportPresenter _patientReportPresenter;

    public PrintComponent(PatientReportPresenter patientReportPresenter, string printerName, string dataSetName, bool landscape)
    {
        _patientReportPresenter = patientReportPresenter;
        _printerName = printerName;
        _dataSetName = dataSetName;
        _landscape = landscape;
    }

    private DataTable LoadSalesData()
    {
        var table = new DataTable();


        table.Columns.Add("Address", typeof(string));
        //...

        table.Rows.Add(_patientReportPresenter.Address, ...);

        return table;
    }

    private Stream CreateStream(string name,
                                string fileNameExtension, Encoding encoding,
                                string mimeType, bool willSeek)
    {
        Stream stream = new MemoryStream();
        m_streams.Add(stream);
        return stream;
    }

    private void Export(LocalReport report)
    {
        string deviceInfo;

        if (_landscape)
        {
            deviceInfo =
                @"<DeviceInfo>
            <OutputFormat>EMF</OutputFormat>
            <PageWidth>11.7in</PageWidth>
            <PageHeight>8.3in</PageHeight>
            <MarginTop>0in</MarginTop>
            <MarginLeft>0in</MarginLeft>
            <MarginRight>0in</MarginRight>
            <MarginBottom>0in</MarginBottom>
        </DeviceInfo>";
        }
        else
        {
            deviceInfo =
             @"<DeviceInfo>
                            <OutputFormat>EMF</OutputFormat>
                            <PageWidth>8.3in</PageWidth>
                            <PageHeight>11.7in</PageHeight>
                            <MarginTop>0.3in</MarginTop>
                            <MarginLeft>0in</MarginLeft>
                            <MarginRight>0in</MarginRight>
                            <MarginBottom>0in</MarginBottom>
                        </DeviceInfo>";
        }
        m_streams = new List<Stream>();

        Warning[] warnings;
        report.Render("Image", deviceInfo, CreateStream, out warnings);

        foreach (Stream stream in m_streams)
            stream.Position = 0;
    }

    private void PrintPage(object sender, PrintPageEventArgs ev)
    {
        var pageImage = new
            Metafile(m_streams[m_currentPageIndex]);

        var adjustedRect = new Rectangle(
            ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
            ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
            ev.PageBounds.Width,
            ev.PageBounds.Height);

        ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
        ev.Graphics.DrawImage(pageImage, adjustedRect);
    }

    private void Print()
    {
        if (m_streams == null || m_streams.Count == 0)
            throw new Exception("Error: no stream to print.");
        var printDoc = new PrintDocument {PrinterSettings = {PrinterName = _printerName}};
        if (_landscape)
        {
            printDoc.DefaultPageSettings.Landscape = true;
        }
        printDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
        if (!printDoc.PrinterSettings.IsValid)
        {
            throw new Exception("Error: cannot find the default printer.");
        }

        printDoc.PrintPage += PrintPage;
        m_currentPageIndex = 0;
        printDoc.Print();
    }

    public void Run(LocalReport report)
    {
        report.DataSources.Add(new ReportDataSource(_dataSetName, LoadSalesData()));
        Export(report);
        Print();
    }

    public void Dispose()
    {
        if (m_streams != null)
        {
            foreach (Stream stream in m_streams)
                stream.Close();
            m_streams = null;
        }
    }
}

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我通过安装REPORT VIEWER 2012 RUNTIME http://www.microsoft.com/en-us/download/details.aspx?id=35747

来解决这个问题