How to Print from ASP.NET CrystalReport on client side printer

前端 未结 1 1600
谎友^
谎友^ 2021-01-17 01:50

How do I print report from CrystalReport (ASP.NET) on a client side printer.

相关标签:
1条回答
  • 2021-01-17 02:48

    You have two options:

    1. Set the PrintMode to ActiveX or PDF, and leave the Crystal report viewer toolbar to handle it.
    2. Create a pdf in an iFrame, and trigger the print command with JavaScript.

    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 load 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);
    
    0 讨论(0)
提交回复
热议问题