Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show print dialog in Crystal Report?

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.

like image 225
barsan Avatar asked Nov 02 '22 19:11

barsan


1 Answers

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.

like image 104
David - Avatar answered Nov 08 '22 03:11

David -