在C#(ASP MVC)中为RDLC PDF或Excel添加密码

本文关键字:PDF Excel 添加 密码 RDLC 中为 ASP MVC | 更新日期: 2023-09-27 17:58:54

我使用RDLC进行基本报告的时间很短,方法是将我的标准C#模型(POCO)绑定到数据集,并将它们推送到报告中以表示数据。因此,在我的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时,应该有一个窗口提示他们输入密码以访问信息

我到处寻找可能的解决方案,发现有几个建议使用iText Sharp和其他第三方工具,这些工具往往有一定的许可限制。

为了保护PDF的安全,我可能会遗漏什么吗?

在C#(ASP MVC)中为RDLC PDF或Excel添加密码

使用PDFSharp:)

找到了此类问题的修复程序