Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to use the Microsoft.Office.Interop.Word facilitie sto save an existing word document as a password protected PDF document. I can create the document, but cannot work out how to password protect it. Any ideas?

I have tried the Document.SaveAs, Document.SaveAs2 and Document.ExportAsFixedFormat methods, - the PDF is created, but not password protected.

I have tried recording a macro in Word when douing it manually - you can specify the password in one of the option screens, but when you look at the macro, the password bit does not appear in it.

The code below successfully saves a PDF copy of an existing document, but even though I specify a password, itt is ignored

C#
string strFilename = this.txtFilename.Text;
           string strSaveAs = strFilename.Replace(Path.GetExtension(strFilename), ".pdf");
           Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
           Microsoft.Office.Interop.Word.Document wordDocument = appWord.Documents.Open(this.txtFilename.Text);
           wordDocument.SaveAs(strSaveAs, WdSaveFormat.wdFormatPDF, false, strRandom);
           wordDocument.Close(WdSaveOptions.wdDoNotSaveChanges);
           appWord.Quit();
Posted

1 solution

hey Chris - i am just telling you about to save data from grid view to Pdf with password protected, you check this for your references--


C#
protected void ExportToPDF(object sender, EventArgs e)
{
    using (StringWriter sw = new StringWriter())
    {
        using (HtmlTextWriter hw = new HtmlTextWriter(sw))
        {
            //To Export all pages
            GridView1.AllowPaging = false;
            this.BindGrid();
 
            GridView1.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfWriter.GetInstance(pdfDoc, memoryStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();
                using (MemoryStream input = new MemoryStream(bytes))
                {
                    using (MemoryStream output = new MemoryStream())
                    {
                        string password = "pass@123";
                        PdfReader reader = new PdfReader(input);
                        PdfEncryptor.Encrypt(reader, output, true, password, password, PdfWriter.ALLOW_SCREENREADERS);
                        bytes = output.ToArray();
                        Response.ContentType = "application/pdf";
                        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.pdf");
                        Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        Response.BinaryWrite(bytes);
                        Response.End();
                    }
                }
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Chris Quinn 7-May-14 10:51am    
I presume the PdfWriter is an external component?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900