c# - 使用密码 C# 保护 pdf 文件

标签 c# asp.net pdf encryption passwords

connetionString = ConfigurationManager.ConnectionStrings["conString"].ToString();
sql = "select Id,employeeName,employeePosition from Employee";
connection = new SqlConnection(connetionString);
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
connection.Close();

PdfDocument pdf = new PdfDocument();
pdf.Info.Title = "Database to PDF";
PdfPage pdfPage = pdf.AddPage();
XGraphics graph = XGraphics.FromPdfPage(pdfPage);
XFont font = new XFont("Verdana", 20, XFontStyle.Regular);

yPoint = yPoint + 100;

for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
    pubname = ds.Tables[0].Rows[i].ItemArray[0].ToString();
    city = ds.Tables[0].Rows[i].ItemArray[1].ToString();
    state = ds.Tables[0].Rows[i].ItemArray[2].ToString();
    graph.DrawString(pubname, font, XBrushes.Black, new XRect(40, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
    graph.DrawString(city, font, XBrushes.Black, new XRect(120, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
    graph.DrawString(state, font, XBrushes.Black, new XRect(400, yPoint, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
    yPoint = yPoint + 40;
}

string pdfFilename = "dbtopdf.pdf";
pdf.Save(pdfFilename);

我直接从数据库创建了一个pdf文件。我需要用密码保护 pdf 文件。

using (MemoryStream ObjememoryStream = new MemoryStream())
{
    PdfWriter.GetInstance(pdfDoc, ObjememoryStream);
    pdfDoc.Open();
    htmlworker.Parse(sr);
    pdfDoc.Close();
    byte[] Filebytes = ObjememoryStream.ToArray();
    ObjememoryStream.Close();
    using (MemoryStream inputData = new MemoryStream(Filebytes))
    {
        using (MemoryStream outputData = new MemoryStream())
        {
            string PDFFileword = txtPassword.Text;//you can also generate Dynamic word  
            PdfReader reader = new PdfReader(inputData);
            PdfEncryptor.Encrypt(reader, outputData, true, PDFFileword, PDFFileword, PdfWriter.ALLOW_SCREENREADERS);
            Filebytes = outputData.ToArray();
            File.WriteAllBytes(destPath, Filebytes);
            //Response.ContentType = "application/pdf";
            //Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //Response.BinaryWrite(Filebytes);
            //Response.End();
            GridView1.AllowPaging = true;
            GridView1.DataBind();
        }
    }
}

我设法用上面的代码用密码保护了 pdf 文件,但它只适用于从 gridview 生成的 pdf 文件。有人可以告诉我如何使用类似于我的第二个代码的方法使用第一种方法生成的密码来保护 pdf 文件吗?

最佳答案

SecuritySettings中设置用户密码

pdf.SecuritySettings.UserPassword = "your password";

关于c# - 使用密码 C# 保护 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44558764/

相关文章:

c# - 添加对象不起作用

c# - 如何暂停代码直到我想要? [类似于 Thread.Sleep(int);]

c# - FileUpload 控件生成空文件

c# - 是否可以从默认表 AspNetUsers 中删除行?如果是,请解释一下

c# - "too much"什么时候异步等待?所有方法都应该返回 Task 吗?

asp.net - 角色动态授权 asp.net core

java - 如何使用内部存储器中的其他应用程序读取 pdf 文件?

android - 通过谷歌文档在 WebView 中查看 pdf 文件达到带宽限制

javascript - 以编程方式捕获 .PDF 文件中所有嵌入文本项的位置和内容?

c# - 如何防止修改继承的属性?