简体   繁体   English

在 ASP.NET C# 中在客户端打印 Crystal Report

[英]Printing Crystal Report on Client Side in ASP.NET C#

I created an ASP.NET application where I print a Crystal Report report.我创建了一个 ASP.NET 应用程序,用于打印 Crystal Report 报表。 The problem is that the report is printed at the Server Printer, and as it is a Web Application I need it to get printed at the client machine.问题是报告是在服务器打印机上打印的,因为它是一个 Web 应用程序,我需要它在客户端机器上打印。

I am using the method PrintToPrinter(1, false, 0, 0) in order to print it without a crystal report viewer.我正在使用方法PrintToPrinter(1, false, 0, 0)以便在没有水晶报告查看器的情况下打印它。

Does anybody knows if there is a way to have it printed on the client side?有谁知道是否有办法在客户端打印它? If not;如果不; what do you recommend to generate reports on the client side for ASP.Net applications?您建议在客户端为 ASP.Net 应用程序生成报告吗?

The Crystal report viewer is a server side control, and it doesn't really provide an easy way to print to client. Crystal 报表查看器是一个服务器端控件,它并没有真正提供打印到客户端的简单方法。 I have been able to achieve this in the past by exporting the report to PDF, then with a combination of an embedded PDF viewer and some JavaScript, print the PDF.过去,我通过将报告导出为 PDF,然后结合使用嵌入式 PDF 查看器和一些 JavaScript 来打印 PDF 来实现这一点。

// 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
report.ExportToDisk(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, pdfName);

// Display the pdf object in a literal control (mine's called pdfLiteral)
sb.AppendFormat("<object ID=\"pdfObject\" type=\"application/pdf\" data=\"{0}\" src=\"{0}\" style=\"width: 2px; height: 2px; ", pdfName);
sb.AppendLine("z-index:1; display: block; top: 0; left: 0; position: absolute; \">");
sb.Append("</object>");
pdfLiteral.Text = sb.ToString();
pdfLiteral.Visible = true;

// client side
// on document load call the printWithDialog function
 var code = function(){
    var pdf = document.getElementById('pdfObject');
    if (pdf == null)
        return;
    try {
        pdf.printWithDialog();
    }
    catch (err) {
        alert('Please Install Adobe Acrobat reader to use this feature');
    }
  };
// window onload, with delay
window.setTimeout(code, 1000);

See: https://stackoverflow.com/a/25994086/474702参见: https : //stackoverflow.com/a/25994086/474702

Note: although this works well in Chrome, it only works in IE if the client has Acrobat reader installed as the default PDF viewer.注意:虽然这在 Chrome 中运行良好,但只有在客户端将 Acrobat 阅读器安装为默认 PDF 查看器时才能在 IE 中运行。

最好的方法是设计一个页面的“HTML 可打印版本”,并带有一个链接调用:

javascript:window.print(); 

here is what you will need to do / try to get the report to print on the Client Machine这是您需要做的/尝试在客户端机器上打印报告

Below line opens up print dialog box to print without showing print preview下面一行打开打印对话框打印而不显示打印预览

crystalReportViewer1.PrintReport();

Below line directly sends reportdocument to default printer.下面一行直接将报告文档发送到默认打印机。

oReportDocument.PrintToPrinter(1,true,0,0); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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