使用Microsoft.Office.Interop.Word在SharePoint 2013中将word文档转换为PD

本文关键字:word 中将 文档 转换 PD 2013 SharePoint Office Microsoft Interop Word | 更新日期: 2023-09-27 18:11:26

你好朋友,我创建了一个可视化Web部件沙盒解决方案,用于将word文档转换为PDF并将其存储到文件资源管理器中。所以我在我的解决方案中添加了Microsoft.Office.Interop.Word Version 15.0.0.0的参考。下面的代码是设计文件。

    <script type ="text/javascript">
    function validate() {
        var uploadcontrol = document.getElementById('<%=FileUpload1.ClientID %>').value;
        var reg = /^(([a-zA-Z]:)|(''{2}'w+)'$?)(''(w['w].*))+(.doc| .docx | .DOC |.DOCX)$/;
        if (uploadcontrol.length > 0)
        {
            if (reg.test(uploadcontrol)) {
                return true;
            }
            else {
                alert("Only .doc,docx files are allowed!");
                uploadcontrol.focus();
                return false;
            }
        }
        else {
            alert('Please Select a file to Upload');
            uploadcontrol.focus();
            return false;
        }
    }
</script>
<asp:FileUpload ID="FileUpload1" runat="server" />
<p>
    <asp:Button ID="Button1" runat="server" Height="27px" Text="Upload"
        width="122px" OnClick="Button1_Click" OnClientClick="return validate();" />
</p>

在这里我采取一个控制文件上传,所以客户端可以上传。doc或。docx文件,当它点击上传按钮word文件将转换成PDF并存储在他的本地机器,这是现在硬代码。在Visual Studio解决方案中成功构建和部署。我已经创建了一个WepPart页面,并在页面中添加了自定义web部件,现在当客户端上传Word文档并单击上传按钮时,出现了下面的错误。

"ConvertToPDF - VisualWebPart1 .Web部件错误:在部分信任应用程序域中,沙箱代码包装器的Execute方法抛出了未处理的异常:无法加载类型"Microsoft.Office.Interop.Word"。_Application' from assembly 'ConvertToPDF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ee035cf3e0b5c7ba'。该类型被标记为符合类型等效性,但包含程序集未被加载为完全受信任。关联ID: 989aa09c-e86e-d0df-a6bb-4f3c556079a9。"

下面是

代码
protected void Button1_Click(object sender, EventArgs e)
{
            if (FileUpload1.PostedFile != null)
            { 
                // Create a new Microsoft Word application object
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                // C# doesn't have optional arguments so we'll need a dummy value
                object oMissing = System.Reflection.Missing.Value;
                // Get a Word file
                FileInfo wordFile = new FileInfo("D:''Projects''mydoc.doc");

                word.Visible = false;
                word.ScreenUpdating = false;
                // Cast as Object for word Open method
                Object filename = (Object)wordFile.FullName;
                // Use the dummy value as a placeholder for optional arguments
                Document doc = word.Documents.Open(ref filename, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                doc.Activate();
                string ext = Path.GetExtension(wordFile.FullName);
                object outputFileName = new object();
                if (ext == "doc")
                {
                    outputFileName = "D:''WPF''DemoApp''WordToExcel''demo.pdf";
                }
                else
                {
                    outputFileName = "D:''WPF''DemoApp''WordToExcel''demo.pdf";
                }

                object fileFormat = WdSaveFormat.wdFormatPDF;
                // Save document into PDF Format
                doc.SaveAs(ref outputFileName,
                    ref fileFormat, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing, ref oMissing, ref oMissing);
               // string destUrl = SPContext.Current.Site.Url + DocumentLibName + outputFileName;

                // Close the Word document, but leave the Word application open.
                // doc has to be cast to type _Document so that it will find the
                // correct Close method.                
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                doc = null;
                // word has to be cast to type _Application so that it will find
                // the correct Quit method.
                ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
                word = null;  
    }
} 

注意:我也创建了控制台应用程序,这是很好的工作,并给出正确的结果意味着我可以使用它将Word转换为PDF,但只在SharePoint中得到错误。

使用Microsoft.Office.Interop.Word在SharePoint 2013中将word文档转换为PD

尝试将PDF程序集和MS Interop程序集添加到safcontrols集合。