您好朋友我创建了一个Visual Web Part Sandbox解决方案,用于将word文档转换为PDF并将其存储到文件资源管理器中。 所以我在我的解决方案中添加了Microsoft.Office.Interop.Word版本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文档并单击上传按钮然后得到以下错误。
&#34; ConvertToPDF - VisualWebPart1 Web部件错误:部分信任应用程序域中沙盒代码包装程序的Execute方法抛出了未处理的异常:无法加载类型&#39; Microsoft.Office.Interop.Word._Application&#39;来自assembly&#39; ConvertToPDF,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = ee035cf3e0b5c7ba&#39;。该类型被标记为有资格进行类型等效,但包含的程序集未加载为完全受信任。相关ID:989aa09c-e86e-d0df-a6bb-4f3c556079a9。 &#34;
以下是
的代码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中获取错误。
任何帮助都是高度赞赏的。
答案 0 :(得分:0)
尝试将PDF程序集和MS Interop程序集添加到SafeControls集合。