65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET MVC 3 中自动打印 RDLC 文件

starIconstarIconstarIconstarIconstarIcon

5.00/5 (6投票s)

2013年3月29日

CPOL

2分钟阅读

viewsIcon

84872

downloadIcon

4578

在 ASP.NET MVC3 中自动打印 RDLC 文件,通过将其转换为 PDF 并使用 Acrobat Reader 实现。

引言

在这里我将介绍一种我采用的小方法,用于在我的 ASP.NET MVC3 应用程序中自动打印 RDLC 文件。

背景

在我的 ASP.NET MVC 3 应用程序中,我遇到了报表部分的问题。我们使用 Reporting Service 的客户端定义(RDLC)来创建报表。但事情在一个场景中受阻,我们的客户需要通过指定呈现的特定打印机名称来自动打印报表。

经过大量的 Google 搜索,我意识到这是一个难题。最终,我们决定使用 acrobat reader 和 java script 来实现它,转换为 PDF。要求是您必须在浏览器中安装 acrobat reader 插件。

需求是存在一个打印机设置,我们通过提供系统的 IP 地址为特定的报表设置特定的打印机。根据这些设置,特定的报表必须通过设置表中提供的打印机进行打印。

使用代码

首先,我们使用 RDLC 设计报表。下一步是从控制器中的 action method 将 RDLC 转换为 PDF。此外,我们需要将 JavaScript 注入到 acrobat viewer 中,以自动打印报表。为此,借助 iTextSharp DLL,我们将 RDLC 文件转换为 PDF,同时将一些 JavaScript 注入到 PDF 中以进行自动打印。

您需要包含 iTextSharp DLL 中的以下命名空间

using iTextSharp.text.pdf;
using iTextSharp.text;

action method 如下

public ActionResult RecieptPrint() 
{ 
  LocalReport localReport = new LocalReport(); 
  localReport.ReportPath = @"Reprt1.rdlc";
  DataTable dt = DataSelect();

以下代码用于动态地将数据源设置为 RDLC。我已经在 这里 介绍了这个。

ReportDataSource reportDataSource = new ReportDataSource();    
reportDataSource.Value = dt;      
reportDataSource.Name = "DataSet1";   
localReport.SetParameters(param);
localReport.DataSources.Add(reportDataSource); 
string reportType = "PDF";  
string mimeType;  
string encoding;
string fileNameExtension = "pdf"; 
//The DeviceInfo settings should be changed based on the reportType 
string deviceInfo =@"<DeviceInfo>              
 <OutputFormat>PDF</OutputFormat>              
 <PageWidth>9.2in</PageWidth>              
 <PageHeight>12in</PageHeight>          
 <MarginTop>0.25in</MarginTop>          
 <MarginLeft>0.45in</MarginLeft>            
 <MarginRight>0.45in</MarginRight>       
 <MarginBottom>0.25in</MarginBottom></DeviceInfo>";
    Warning[] warnings; 
    string[] streams;
    byte[] renderedBytes;

现在我们将使用上述设备信息将 RDLC 转换为字节数组,以生成 PDF。要了解更多信息,点击这里

renderedBytes= localReport.Render(       
             reportType,deviceInfo, out mimeType, out encoding,out fileNameExtension,
             out  streams,out  warnings);

接下来是重要的部分。我们将此字节数组添加到我们的 PDF 文件中。同时,我们注入了执行自动打印的 JavaScript 到 PDF 中。

var doc = new Document();
var reader = new PdfReader(renderedBytes); 
using (FileStream fs = new FileStream(Server.MapPath("~/Summary"+ 
     Convert.ToString(Session["CurrentUserName"]) + ".pdf"), FileMode.Create)) 
{   
  PdfStamper stamper = new PdfStamper(reader, fs);
  string Printer = PrinterName(Convert.ToInt32(Session["localOutletID"]));
  // This is the script for automatically printing the pdf in acrobat viewer
  stamper.JavaScript= "var pp = getPrintParams();pp.interactive =
                   pp.constants.interactionLevel.automatic;pp.printerName = " +
                   Printer + ";print(pp);\r";
  stamper.Close();
}           
reader.Close();
FileStream fss = new FileStream(Server.MapPath("~/Report/Summary.pdf"), FileMode.Open);
            byte[] bytes = new byte[fss.Length];
            fss.Read(bytes, 0, Convert.ToInt32(fss.Length));
            fss.Close();
            System.IO.File.Delete(Server.MapPath("~/Report/Summary.pdf"));
//Here we returns the file result for view(PDF)
            return File(bytes, "application/pdf");
}
  

在视图中,我们可以像这样调用这个 action method。

$(document).ready(function () {
            $('#Print').click(function () {
            window.open("../Report/RecieptPrint");
        });
});
<input type="button" id="Print"  value="Print"/>

这将自动打印 PDF。要求是您必须在浏览器中安装 acrobat reader 插件。

关注点

如果我们保存了 PDF 而不是打印,在打开它时它将自动打印。

© . All rights reserved.