如何使用itextsharp创建受密码保护的pdf?

时间:2012-09-17 06:48:07

标签: asp.net

我动态创建pdf。所以在创建自己时我需要用密码保护它。 我使用asp.net,c#和pdf itextsharp。

这是我的代码

使用(Document doc = new Document(PageSize.A4,0f,0f,0f,0f))             {

            string path = Server.MapPath("Pages");

            FileStream pdffile= new FileStream(path+"/Pdf/tes.pdf", FileMode.Create);
            PdfWriter writer=PdfWriter.GetInstance(doc,pdffile);
            doc.Open();
            var titlefont = FontFactory.GetFont("Arial", 16, Font.BOLD);   
            doc.Add(new Paragraph("My Pdf",titlefont));
            doc.Close(); 

            Response.Clear();
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=tes.pdf"); 
            Response.TransmitFile(path+"/Pdf/tes.pdf");                  
            Response.Flush();
            File.Delete(path + "/Pdf/tes.pdf");
                        }     

谢谢..

1 个答案:

答案 0 :(得分:3)

我搜索了其他主题,这似乎适用于这个人:

https://stackoverflow.com/a/6586551/1648976

希望它对你有所帮助。