use*_*812 2 c# asp.net security ms-word ms-office
我正在为有关女巫用户的网站编写代码,必须添加报告(Word文档的报告)并使其能够查看,我将* .doc转换为* .pdf,然后通过pdf.js显示它们。对于转换,我使用Microsoft.Office.Interop.Word。代码看起来像
public void ConvertDocument(string PATH)
{
FileInfo FILE = new FileInfo(PATH);
if (FILE.Extension.ToLower() == ".doc" || FILE.Extension.ToLower() == ".docx" || FILE.Extension.ToLower() == ".docm" || FILE.Extension.ToLower() == ".dotx" || FILE.Extension.ToLower() == ".dotm")
{
if (FILE.Length == 0)
{
return;
}
object oMissing = System.Reflection.Missing.Value;
Word.Application word = new Word.Application();
try
{
word.Visible = false;
word.ScreenUpdating = false;
Object filename = (Object)FILE.FullName;
Word.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);
try
{
doc.Activate();
object outputFileName = FILE.FullName.Replace(FILE.Extension, ".PDF");
doc.SaveAs(ref outputFileName, Word.WdSaveFormat.wdFormatPDF, 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);
}
finally
{
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
((Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
}
finally
{
((Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
word = null;
}
File.Delete(PATH);
}
}
Run Code Online (Sandbox Code Playgroud)
微软的答案是否定的:
Microsoft当前不建议也不支持任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office应用程序自动化,因为Office可能表现出不稳定的行为和/在此环境中运行Office时出现死锁或死锁。
根据经验,这是我们遇到的问题:
但是,还有其他方法可以执行此操作,如本StackOverflow问题和解答所述
您可以考虑对文档文档进行预转换-例如,在文档上传后是否可以同时创建PDF?这样,您的服务器就可以提供一个PDF文档,并且在处理请求方面几乎不需要做任何工作。
| 归档时间: |
|
| 查看次数: |
1954 次 |
| 最近记录: |