2016-07-26 30 views
0

我一直在使用RDLC進行基本報告,將標準C#模型(POCO)綁定到數據集並將它們推送到報告代表數據。所以在我的MVC Controller中提供了一個動作結果,允許用戶在成功調用RESTFUL API後下載PDF,然後將數據綁定到報表。將密碼添加到C#中的RDLC PDF或Excel

public FileContentResult GenerateCensusReport(PersonReportModel model) 
{ 
    Warning[] warnings; 
    string[] streams; 
    string mimeType; 
    byte[] renderedBytes; 
    string encoding; 
    string fileNameExtension; 
    var resultModel = new PersonReportModel(); 
    var inputModel = new List<PersonReportModel>(); 
    var localReport = new LocalReport { ReportPath = Server.MapPath("~/Reports/statsReportTemplate.rdlc") }; 

    //call api 
    var tokenString = HttpContext.Items["tokenValue"]; 
    ServiceClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", 
     tokenString.ToString()); 

    DataObject.Data = model; 
    var data = JsonConvert.SerializeObject(DataObject.Data); 
    var content = new StringContent(data, Encoding.Default, "application/json"); 
    var response = ServiceClient.PostAsync(ServiceUrl + "Report/ReportStats", content).Result; 

    if (response.IsSuccessStatusCode) 
    { 
     var responseResult = response.Content.ReadAsAsync<JsonResponseObject>().Result; 
     resultModel = JsonConvert.DeserializeObject<PersonReportModel>(responseResult.Data.ToString()); 
     inputModel.Add(resultModel); 
    } 

    var reportDataSourceOne = new ReportDataSource 
    { 
     Name = "DataSetPersonalStats", 
     Value = inputModel 
    }; 

    localReport.DataSources.Add(reportDataSourceOne); 

    localReport.Refresh(); 

    var deviceInfo = "<DeviceInfo>" + "<OutputFormat>PDF</OutputFormat>" + "</DeviceInfo>"; 

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

    var fileName = "Census_Report_" + DateTime.Now + "_.pdf"; 

    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); 

    return new FileContentResult(renderedBytes, mimeType); 
} 

但是基於客戶機請求我已要求設定文檔上的密碼(或加密文件輸出)當客戶端打開PDF將限制訪問存在於document.In短數據它應該有一個窗口提示他們輸入密碼來訪問信息

我到處搜索可能的解決方案,並且遇到了幾個建議使用iText Sharp和其他第三方工具,傾向於有一定的許可限制。

爲了確保PDF文件可能丟失了一些東西嗎?

回答

1

找到了解決這個問題的方法,使用PDFSharp :)