在 C# (ASP MVC) 中向 RDLC PDF 或 Excel 添加密码
Adding a password to an RDLC PDF or Excel in C# (ASP MVC)
通过将我的标准 C# 模型 (POCO) 绑定到数据集并将它们推送到报告中以表示数据,我已经使用 RDLC 进行了短时间的基本报告。因此,在我的 MVC 控制器中提供了一个操作结果,允许用户在成功调用 RESTFUL API 后下载 PDF,然后将数据绑定到报告。
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 中存在的数据的访问应该有一个 window 提示他们输入密码以访问信息
我到处搜索可能的解决方案,并遇到了几个建议使用 iText Sharp 和其他第三方工具的建议,这些工具往往具有某些许可限制。
为了确保 PDF 的安全,我可能遗漏了什么?
使用 PDFSharp 找到了解决此类问题的方法:)
通过将我的标准 C# 模型 (POCO) 绑定到数据集并将它们推送到报告中以表示数据,我已经使用 RDLC 进行了短时间的基本报告。因此,在我的 MVC 控制器中提供了一个操作结果,允许用户在成功调用 RESTFUL API 后下载 PDF,然后将数据绑定到报告。
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 中存在的数据的访问应该有一个 window 提示他们输入密码以访问信息
我到处搜索可能的解决方案,并遇到了几个建议使用 iText Sharp 和其他第三方工具的建议,这些工具往往具有某些许可限制。
为了确保 PDF 的安全,我可能遗漏了什么?
使用 PDFSharp 找到了解决此类问题的方法:)