Giri
Giri

Reputation: 941

How to create password protected pdf using itextsharp?

I m creating pdf dynamically.So while creating itself i need to protect it with password. I m using asp.net,c#,and for pdf itextsharp.

This is my code

using (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");
                        }     

Thanks..

Upvotes: 1

Views: 8089

Answers (1)

David Raijmakers
David Raijmakers

Reputation: 1379

I searched for other topics and this seems to be working for this guy:

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

Hope it helps you.

Upvotes: 3

Related Questions