How to show print dialog in Crystal Report?

2019-04-09 17:30发布

I want to print my Crystal report direct to the printer. Currently I am having the export to PDF. But my client want this to go to Printer directly. How can I show the Print Dialog on click of Print Button to Print the report directly to Printer.

I would like to mention: I am using C# and asp.net for my project.

Thank you.

3条回答
趁早两清
2楼-- · 2019-04-09 18:00

Try the following code

    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();
    }

you will have to change the "username" and the "password" with the credentials of your database.

EDIT

This code can be used for server side printing only.

查看更多
萌系小妹纸
3楼-- · 2019-04-09 18:06

PrintButton_click Event and add following code as you ..

                         //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; }

查看更多
孤傲高冷的网名
4楼-- · 2019-04-09 18:12

No way; Cristal Report Viewer is intended for showing and browsing a report.
It never shows all report pages.
It has no buttons or methods for direct printing.

You could, instead, export directly the report in PDF so Report Viewer is never seen by users, and printing becomes 1-click operation.

查看更多
登录 后发表回答