I am coding a website on witch users have to add reports (Word document's) and to make possible to view them I convert *.doc to *.pdf, and then displaying them throught pdf.js. For converting i use Microsoft.Office.Interop.Word. Code looks like
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);
}
}
- Is that safe?
- And how many users will it handle?
- What resources it needs?
- Should I install MS Office on the server to run the website?
- Is that actually a good way to do that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…