如何在Crystal Report中显示打印对话框?

5

我想直接将我的Crystal报表打印到打印机上。目前我已经导出为PDF。但我的客户希望直接将其发送到打印机。如何在单击“打印”按钮时显示打印对话框,以直接将报表打印到打印机上。

需要说明的是:我正在使用C#和asp.net进行项目开发。

谢谢。

3个回答

2

尝试以下代码

    private void Button1_Click(object sender, EventArgs e)
    {
        CrystalReport1 report1 = new CrystalReport1();
        PrintDialog dialog1 = new PrintDialog();

        report1.SetDatabaseLogon("username", "password");

        dialog1.AllowSomePages = true;
        dialog1.AllowPrintToFile = false;

        if (dialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            int copies = dialog1.PrinterSettings.Copies;
            int fromPage = dialog1.PrinterSettings.FromPage;
            int toPage = dialog1.PrinterSettings.ToPage;
            bool collate = dialog1.PrinterSettings.Collate;

            report1.PrintOptions.PrinterName = dialog1.PrinterSettings.PrinterName;
            report1.PrintToPrinter(copies, collate, fromPage, toPage);            
        }

        report1.Dispose();
        dialog1.Dispose();
    }

你需要更改"username"和"password",使用你的数据库凭据。

编辑

此代码仅适用于服务器端打印。


你好,我想告知当我将这段代码上传到服务器时,我遇到了以下错误:在非用户交互模式下显示模态对话框或表单是无效的操作。指定ServiceNotification或DefaultDesktopOnly样式以从服务应用程序中显示通知。请问现在我该怎么办? - barsan
此代码仅用于服务器端打印。如果您需要客户端解决方案,请查看此方法 http://aspalliance.com/509_Automatically_Printing_Crystal_Reports_in_ASPNET.3 - David -

0

在PrintButton_click事件中添加以下代码:

                         //show Print Dialog
                          PrintDialog printDialog = new PrintDialog();
                          DialogResult dr = printDialog.ShowDialog();
                          if (dr == DialogResult.OK)
                          {
                                    ReportDocument crReportDocument = (ReportDocument)CrystalReportViewer1.ReportSource;
                                   System.Drawing.Printing.PrintDocument printDocument1 = new System.Drawing.Printing.PrintDocument();
                                    //Get the Copy times
                                    int nCopy = printDocument1.PrinterSettings.Copies;
                                    //Get the number of Start Page
                                    int sPage = printDocument1.PrinterSettings.FromPage;
                                    //Get the number of End Page
                                    int ePage = printDocument1.PrinterSettings.ToPage;
                                    crReportDocument.PrintOptions.PrinterName =printDocument1.PrinterSettings.PrinterName;
                                    //Start the printing process.  Provide details of the print job
                                    crReportDocument.PrintToPrinter(nCopy, false, sPage, ePage);

// Form_Printerd = true; }

// 表单打印机已连接;

0

不可能;Crystal Report Viewer 的目的是用于显示和浏览报告。
它从来不会显示所有报告页面。
它没有直接打印的按钮或方法。

相反,您可以直接将报告导出为PDF格式,这样用户就看不到报告查看器,打印变成了一键操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接