现在的位置: 首页 > 综合 > 正文

Automatically Printing Crystal Reports in ASP.NET

2012年12月04日 ⁄ 综合 ⁄ 共 3241字 ⁄ 字号 评论关闭

This server-based method is documented in the Visual Studio help files.  Open the Help Index, and enter PrintToPrinter in the "Look for:" box.  The syntax for this method is:

 Report.PrintToPrinter(<copies as int>, <collated as True/False>, <startpage as int>, <endpage as int>)

 

'Collated' in this context has nothing to do with database collation.  Some advanced printers (like copier/printers) will sort each document into its own shelf.  Not every printer supports this functionality, so check the printer dialog before setting to true.  To print the entire report, set startpage and endpage each to 0. 

An example in use might look like

MyReport.PrintToPrinter(1,False,0,0)

 

One limitation of this method is that a printer name must be specified.  You can set the default printer at design time in the report, and you can change the printer name at run time by setting the ReportDocument.PrintOptions.PrinterName property (the PrintOptions are also where you can assign page margins, portrait/landscape, etc.).  Keep in mind that this method prints from the server itself, not from the client machine.  This means that any printer you wish to use must be accessible from the server.  You cannot print to a client's desktop printer using this method unless that printer is shared on the network and mapped to the server.

If your LAN has networked printers, you can make some guesses as to which printer to assign as the default in each report.  For instance, if the Accounting Department has a departmental printer, that would be a good choice to use for the default in an accounting report.  You can provide users the option to choose a networked printer by enumerating the printers mapped to the server and populating a drop-down list.  The PrinterSettings.InstalledPrinters property returns a collection of installed printers, and can be bound to a DropDownList as below.

DropDownList1.DataSource = System.Drawing.Printing.PrinterSettings.InstalledPrinters
DropDownList1.DataBind()

 

Again, since this code is executing on the server, this will only enumerate the printers on the server mapped to the System user.  If a printer does not appear in the drop-down list, you need to ensure that it is properly mapped to the System user (see below).

All printers and the .NET Framework need to be mapped to the System user in order for this to work.  This is not a trivial task to configure.  Printers can be mapped to the system account only by editing the registry, and since the Framework is mapped to the System user, you may need to reassign security permissions on files and folders.  This process is outlined in the "Printing Web-Based Reports From the Server" article in the VS .NET help files (look up "Crystal Reports, printing" in the VS help index to link to the articles.) and also in Reference 3 (at the end of this article).  The process is a little too intricate to cover here (leave comments if help is needed, and I can amend the article later).

 

评:这文章写得有点过长,水晶报表打印有两种模式,即服务端打印和客户端打印。

上文主要叙述了服务端打印的方法及主意的问题,概括起来如下:

1、打印的方法:

主要用到的是:Report.PrintToPrinter(<copies as int>, <collated as True/False>, <startpage as int>, <endpage as int>)

for instance:

 

  CrystalDecisions.CrystalReports.Engine.ReportDocument reportDoc = new ReportDocument();

/// <summary>
    ///发送到打印机打印
    /// </summary>
    /// <param name="printerName">打印机</param>
    public void SendToPrinter(string printerName,int startPage,int endPage)
    {
        reportDoc.PrintOptions.PrinterName = printerName;
        reportDoc.PrintToPrinter(1, true, 1, endPage);      
            JavaScript.Run("window.close();");   
    }

2、获取打印机的方法:

DropDownList1.DataSource = System.Drawing.Printing.PrinterSettings.InstalledPrinters
DropDownList1.DataBind();

3、获取打印机主意的问题:

打印机在当前网络必须可用,系统必须可以访问到,要设为共享.

抱歉!评论已关闭.