Crystal Reports打印客户端C#

时间:2014-08-05 14:04:49

标签: c# asp.net printing crystal-reports

我有一个报告,我想根据GridView中的按钮触发。这将生成一个标签并将其发送到连接的(本地)Zebra打印机。当我在本地运行它,它工作正常。打印机甚至不必是默认设置。当我将文件复制到服务器,然后单击打印按钮时没有任何反应。

        CrystalReportSource CrystalReportSource1 = new CrystalReportSource();
        CrystalReportViewer CrystalReportViewer1 = new CrystalReportViewer();
        CrystalReportViewer1.HasPrintButton = true;
        // CrystalReportViewer1.PrintMode = "ActiveX";

        CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.EnableParameterPrompt = false;

        CrystalReportSource1.Report.FileName = "BinLocation2.rpt";
        TableLogOnInfo logOnInfo = new TableLogOnInfo();

        CrystalReportSource1.ReportDocument.SetParameterValue(0, Item);
        CrystalReportSource1.ReportDocument.SetParameterValue(1, binlocation);
        CrystalReportSource1.ReportDocument.SetParameterValue(2, Lot);
        CrystalReportSource1.ReportDocument.SetParameterValue(3, expiredate);
        CrystalReportSource1.ReportDocument.SetParameterValue(4, NDC);


        logOnInfo.ConnectionInfo.ServerName = ConfigurationManager.AppSettings["SalesReportServerName"];
        logOnInfo.ConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["SalesReportDatabaseName"];
        logOnInfo.ConnectionInfo.UserID = ConfigurationManager.AppSettings["SalesReportUserID"];
        logOnInfo.ConnectionInfo.Password = ConfigurationManager.AppSettings["SalesReportPassword"];

        TableLogOnInfos infos = new TableLogOnInfos();
        infos.Add(logOnInfo);
        CrystalReportViewer1.LogOnInfo = infos;

        try
        {

            CrystalReportSource1.ReportDocument.PrintToPrinter(1, false, 0, 0);
        }

我安装了Visual Studio的redist包。比较web.config文件。打印机没有安装在服务器上,但我希望在客户端完成此操作(没有提示打印)。有没有更好的方法来做这个?我错过了什么?

1 个答案:

答案 0 :(得分:0)

首先,这就是我实现此功能的方式(接下来会有解释):

// get the user selected printer
var printerName = Settings.Instance[profile].DirectPrinter;
// enumerate the printers visible to the application
var allPrinters = PrinterSettings.InstalledPrinters.OfType<string>().OrderBy(p => p).ToList();
// Strip out PDF, XPS and any file based printers. These cause havoc on a server based print
var acceptablePrinters = allPrinters.Except(allPrinters
                                                .Where(s => Settings.Instance[profile].PrintersToIgnore
                                                        .Any(u => s.ToLower().Contains(u)))).ToList();
if (acceptablePrinters.Contains(printerName))
{
    report.PrintOptions.PrinterName = printerName;
    report.PrintToPrinter(copies, collate, 0, 0);
}
else
{
   // Log the reason why the printer was rejected
   // Or you may choose to just print to default
}

解释

PrintToPrinter方法在服务器端运行,因此它只适用于服务器和IIS应用程序池标识可以“查看”的打印机。

大多数Zebra打印机都将联网,因此可以在整个网络中显示配置。但是,您需要允许在asp.net应用程序中进行配置以在运行时设置打印机名称(即使它始终相同),然后您可以调用将打印到联网打印机的PrintToPrinter。

在没有提示的情况下打印到客户实际上是不可能的,并且不明智。为此,我允许用户选择打印到客户端,然后导出为PDF,标准PDF打印对话框提示用户输入打印机设置。添加此项是为了允许打印到非联网打印机。