简体   繁体   中英

how to print asp.net c# crystal report from client's printer using client's computer

i have an asp.net website that will deployed to a server. on one of the page, i have a page that load crystal report viewer on pageLoad() and i want when the user click on a button there, it will print the report to their printer.

this is my asp page that contains the crystal report viewer and the button:

<body>
<link href="style.css" rel="stylesheet" type="text/css" />
<form id="form1" runat="server">


    <table align="center">
        <tr>
            <td align=left>
                <asp:LinkButton ID="toHome_LinkButton1" runat="server" onclick="toHome_LinkButton1_Click"><<< Home</asp:LinkButton>
            </td>
            <td align=right>
                <asp:Button ID="print" runat="server" Text="Print" CssClass="css_button" OnClick="print_Click" />
            </td>
        </tr>
        <tr>
            <td colspan=2 align=center>
                <CR:CrystalReportViewer ID="crv1" runat="server" 
                    EnableDatabaseLogonPrompt="False" EnableParameterPrompt="False" 
                    ToolPanelView="None" GroupTreeStyle-ShowLines="False" HasCrystalLogo="False" 
                    HasDrilldownTabs="False" HasDrillUpButton="False" HasExportButton="False" 
                    HasGotoPageButton="False" HasPageNavigationButtons="False" 
                    HasPrintButton="False" HasSearchButton="False" HasToggleGroupTreeButton="False" 
                    HasToggleParameterPanelButton="False" HasZoomFactorList="False" 
                    PrintMode="ActiveX" />
            </td>
        </tr>
    </table>
</form>

i've already try using PrinToPrinter() method, but as far as i know those method only for server side printing, because we have to declare the printer's name, am i right ?

and this is my button onClick: (note that i still use PrintToPrinter method that i think is server side printing)

protected void print_Click(object sender, EventArgs e)
    {
        string url = Request.ServerVariables["QUERY_STRING"];
        string[] kodeKwitansi = url.Split('=');
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);

        SqlCommand select = con.CreateCommand();
        select.CommandText = "SELECT k.no_kwitansi, k.kode_vendor, k.nama_vendor, u.nama_pekerja, k.nama_pekerja_penanggungjawab, k.satuan, k.jumlah, k.jenis_pembayaran, k.tanggal FROM tb_kwitansi k, tb_user u WHERE k.no_kwitansi = '" + kodeKwitansi[1] + "' AND (k.nomor_pekerja = u.nomor_pekerja)";
        con.Open();
        SqlDataReader reader = select.ExecuteReader();
        reader.Read();
        noKwitansi = reader["no_kwitansi"].ToString();
        kode_vendor = reader["kode_vendor"].ToString();
        namaVendor = reader["nama_vendor"].ToString();
        namaPekerja = reader["nama_pekerja"].ToString();
        namaPJ = reader["nama_pekerja_penanggungjawab"].ToString();
        satuan = reader["satuan"].ToString();
        nominal = reader["jumlah"].ToString();
        jenisPembayaran = reader["jenis_pembayaran"].ToString();
        con.Close();

        rd.Load(MapPath("Kwitansi.rpt"));
        TextObject to = (TextObject)rd.ReportDefinition.ReportObjects["Text7"];
        to.Text = noKwitansi;
        to = (TextObject)rd.ReportDefinition.ReportObjects["Text10"];
        to.Text = kode_vendor;
        to = (TextObject)rd.ReportDefinition.ReportObjects["Text11"];
        string[] val = nominal.Split('.');
        string val2 = "";
        int n = val[0].Length;
        int count = 0;
        int tan = 0;
        for (int i = n - 1; i >= 0; i--)
        {
            if (val[0][i] == '.')
            {
                val2 = val[0][i] + val2;
                tan = 1;
            }
            else if (val[0][i] >= '0' && val[0][i] <= '9')
            {
                val2 = val[0][i] + val2;
                count++;
                if (count == 3 && i != 0 && tan == 0)
                {
                    val2 = "," + val2;
                    count = 0;
                }
            }
        }
        if (val.Count() == 2)
        {
            val2 = val2 + "." + val[1];
        }
        to.Text = val2;
        to = (TextObject)rd.ReportDefinition.ReportObjects["Text12"];
        if (satuan.Equals("$"))
        {
            to.Text = terbilang(nominal);
        }
        else
        {
            to.Text = terbilang(nominal);
        }
        to = (TextObject)rd.ReportDefinition.ReportObjects["Text13"];
        to.Text = jenisPembayaran;
        to = (TextObject)rd.ReportDefinition.ReportObjects["Text15"];
        to.Text = namaPJ;
        to = (TextObject)rd.ReportDefinition.ReportObjects["Text8"];
        to.Text = namaVendor;
        rd.PrintOptions.PrinterName = "Canon Inkjet iP3600 series";
        rd.PrintToPrinter(1, true, 0, 0);

        rd.Close();
        rd.Dispose();
        Response.Redirect("LihatKwitansi.aspx?"+url);
    }

and i read that we can use javascript's windows.Print(), but again as far as i know when we use window.Print() it will print the whole page, while i want only the report to be printed.

so can anyone help me to do this ? what i need is when the user access my page from their computer, then they click on the button, it will print the report directly to printer that connected to their computer.

any help would be appreciated thanks in advance :)

The Crystal Report viewer control has this feature built in, but you have disabled it, with this option:

HasPrintButton="False"

Is there a particular reason you don't want to use the built in feature? If not, then simply set this option to True.

What you have to do to print to the client's printer is to give access to the crystal client side printing api by setting PrintMode="ActiveX" in the report viewer control, or use a hidden PDF.

Since I wanted to simplify what the users had to install on each client I went with the hidden pdf option and a separate button to print to client.

On the aspx page I have an asp literal that I populate with the pdf embeded object at 1px x 1px so it isn't visible to the user. Then on pageload call the printToPrinter method.

// On server side
// Export to PDF
Guid imageGuid = Guid.NewGuid();
string _pdfName = String.Format(@"{0}{1}{2}.pdf", _pdfPath, _reportName, imageGuid);
// expport to unique filename
// ...
// Display the pdf object 
_sb.AppendFormat("<object ID=\"pdfObject\" type=\"application/pdf\" data=\"{0}\" src=\"{0}\" style=\"width: {1}; height: {2}; ", _pdf2Name, _width, _height);
_sb.AppendLine("z-index:1; display: block; border: 1px solid #cccccc; top: 0; left: 0; position: absolute;-+ \">");
_sb.Append("</object>");
pdfLiteral.Text = _sb.ToString();
pdfLiteral.Visible = true;

// javascript
// on document read call the printWithDialog function
 var code = function(){
 try
     {
        var pdf = $get('pdfObject');
        if (pdf == null)
            return;
        try {
            pdf.printWithDialog();
        }
        catch (err) {
            alert('Please Install Adobe Acrobat reader to use this feature');
        } 
     }
     catch(err)
     {
     }
  };
window.setTimeout(code, 1000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM