打印预览没有一个的ReportViewer [英] Print a ReportViewer Without Preview

查看:492
本文介绍了打印预览没有一个的ReportViewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Visual Studio 2010的C#Windows窗体应用程序+ MYSQL
我有一个工作的100%,报表查看器。中的ReportViewer充满了我的数据库的数据,它显示了我点击按钮,打印和它打印......可是,我的客户不希望点击这个按钮,他要自动打印。当我打电话给它的ReportViewer打印本身,而不需要点击一个按钮来做到这一点。谁能告诉我,我该怎么办?

I'm using Visual Studio 2010 C# Windows Forms Application + MySql I have a Report Viewer that is working 100% . The reportviewer is filled with data of my database, it shows up I click on the button to print and it prints... BUT, my client does not want to click on this button, he wants to print automatically. When I Call the ReportViewer it print by itself without need to click on a button to do that. Could anyone tell me how I do that ?

我tryed reportviewer1.print并从工具箱中的PrintDocument。但我不知道如何正确使用它们。

I tryed reportviewer1.print and the PrintDocument from the toolbox. But I do not know how to use these correctly.

感谢关注!

推荐答案

我刚刚同样的问题,这是我的代码使用,就像一个魅力!

I had just the same issue this is the code i use and works like a charm!

using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using Microsoft.Reporting.WinForms;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace NewLabelPrinter
{
/// <summary>
/// The ReportPrintDocument will print all of the pages of a ServerReport or LocalReport.
/// The pages are rendered when the print document is constructed.  Once constructed,
/// call Print() on this class to begin printing.
/// </summary>
class AutoPrintCls : PrintDocument
{
    private PageSettings m_pageSettings;
    private int m_currentPage;
    private List<Stream> m_pages = new List<Stream>();

    public AutoPrintCls(ServerReport serverReport)
        : this((Report)serverReport)
    {
        RenderAllServerReportPages(serverReport);
    }

    public AutoPrintCls(LocalReport localReport)
        : this((Report)localReport)
    {
        RenderAllLocalReportPages(localReport);
    }

    private AutoPrintCls(Report report)
    {
        // Set the page settings to the default defined in the report
        ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();

        // The page settings object will use the default printer unless
        // PageSettings.PrinterSettings is changed.  This assumes there
        // is a default printer.
        m_pageSettings = new PageSettings();
        m_pageSettings.PaperSize = reportPageSettings.PaperSize;
        m_pageSettings.Margins = reportPageSettings.Margins;
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        if (disposing)
        {
            foreach (Stream s in m_pages)
            {
                s.Dispose();
            }

            m_pages.Clear();
        }
    }

    protected override void OnBeginPrint(PrintEventArgs e)
    {
        base.OnBeginPrint(e);

        m_currentPage = 0;
    }

    protected override void OnPrintPage(PrintPageEventArgs e)
    {
        base.OnPrintPage(e);

        Stream pageToPrint = m_pages[m_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.
            m_currentPage++;
            e.HasMorePages = m_currentPage < m_pages.Count;
        }
    }

    protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
    {
        e.PageSettings = (PageSettings)m_pageSettings.Clone();
    }

    private void RenderAllServerReportPages(ServerReport serverReport)
    {
        try
        {
            string deviceInfo = CreateEMFDeviceInfo();

            // Generating Image renderer pages one at a time can be expensive.  In order
            // to generate page 2, the server would need to recalculate page 1 and throw it
            // away.  Using PersistStreams causes the server to generate all the pages in
            // the background but return as soon as page 1 is complete.
            NameValueCollection firstPageParameters = new NameValueCollection();
            firstPageParameters.Add("rs:PersistStreams", "True");

            // GetNextStream returns the next page in the sequence from the background process
            // started by PersistStreams.
            NameValueCollection nonFirstPageParameters = new NameValueCollection();
            nonFirstPageParameters.Add("rs:GetNextStream", "True");

            string mimeType;
            string fileExtension;


            Stream pageStream = serverReport.Render("IMAGE", deviceInfo, firstPageParameters, out mimeType, out fileExtension);



            // The server returns an empty stream when moving beyond the last page.
            while (pageStream.Length > 0)
            {
                m_pages.Add(pageStream);

                pageStream = serverReport.Render("IMAGE", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("possible missing information ::  " + e);
        }
    }

    private void RenderAllLocalReportPages(LocalReport localReport)
    {
        try
        {
            string deviceInfo = CreateEMFDeviceInfo();

            Warning[] warnings;

            localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings);
        }
        catch (Exception e)
        {
            MessageBox.Show("error :: " + e);
        }
    }

    private Stream LocalReportCreateStreamCallback(
        string name,
        string extension,
        Encoding encoding,
        string mimeType,
        bool willSeek)
    {
        MemoryStream stream = new MemoryStream();
        m_pages.Add(stream);

        return stream;
    }

    private string CreateEMFDeviceInfo()
    {
        PaperSize paperSize = m_pageSettings.PaperSize;
        Margins margins = m_pageSettings.Margins;

        // The device info string defines the page range to print as well as the size of the page.
        // A start and end page of 0 means generate all pages.
        return string.Format(
            CultureInfo.InvariantCulture,
            "<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
            ToInches(margins.Top),
            ToInches(margins.Left),
            ToInches(margins.Right),
            ToInches(margins.Bottom),
            ToInches(paperSize.Height),
            ToInches(paperSize.Width));
    }

    private static string ToInches(int hundrethsOfInch)
    {
        double inches = hundrethsOfInch / 100.0;
        return inches.ToString(CultureInfo.InvariantCulture) + "in";
    }
}



}

}

本类有建立完善为你所需要的,那么所有你需要做的是:

This class has the set up perfect for what you need then all you need to do is:

private void AutoPrint()
    {
        AutoPrintCls autoprintme = new AutoPrintCls(reportViewer1.LocalReport);
        autoprintme.Print();
    }

和变戏法它打印。 !只要在代码中附加此的方法(该报告负荷前后也许)和您的设置很好

and hey presto it prints. Just attach this to A method in your code(maybe after the report Loads.) and your setup nicely!

选项:(未测试)

由于看准了这一点都输出到默认打印机,改变你可以做以下打印机:

As spotted this prints out to the default printer, to change the printer you could do the following:

if (printDialog.ShowDialog() == DialogResult.OK) 
{
    m_pageSettings .PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
}



没有测试过,虽然我不再有任何的源代码测试了这一点

not tested though as i no longer have any source code to test this out

这篇关于打印预览没有一个的ReportViewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆