本文介绍了将Word文档的副本保存到受密码保护的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Microsoft.Office.Interop.Word的设备将现有的word文档另存为受密码保护的PDF文档。我可以创建文档,但无法解决如何密码保护它。任何想法?



我已经尝试过Document.SaveAs,Document.SaveAs2和Document.ExportAsFixedFormat方法, - 创建了PDF,但没有密码保护。



我尝试在手动移动时在Word中录制宏 - 您可以在其中一个选项屏幕中指定密码,但是当您查看宏时,密码位会不会出现在它里面。



下面的代码成功保存了现有文档的PDF副本,但即使我指定了密码,它也会被忽略



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

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();

推荐答案

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


这篇关于将Word文档的副本保存到受密码保护的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 12:46
查看更多