First do this
A a reference in your project to MS Word. This is done from Solution Explorer, right click and select “add reference” then on the COM tab find your Word library. Mine is called Microsoft Word 11 Object Library, but different versions of Word will be different.
And add <identity impersonate=“true“/> to web.config
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
// Upload the file to a temporary folder…
string folder_to_save_in = @”c:\”;
string filePath = folder_to_save_in + FileUpload1.FileName;
FileUpload1.SaveAs(filePath);
Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
// Set up an object to hold a missing value…
object o_nullobject = System.Reflection.Missing.Value;
object o_filePath = filePath;
Microsoft.Office.Interop.Word.Document doc = wordApplication.Documents.Open(ref o_filePath,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject);
string newfilename = folder_to_save_in + FileUpload1.FileName.Replace(“.doc”, “.pdf”);
object o_newfilename = newfilename;
object o_null = null;
object o_format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
object o_encoding = null; // Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
object o_endings = null; // Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
// SaveAs requires lots of parameters, but we can leave most of them empty:
wordApplication.ActiveDocument.SaveAs(ref o_newfilename, ref o_format, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject, ref o_nullobject,
ref o_nullobject, ref o_nullobject, ref o_encoding, ref o_nullobject,
ref o_nullobject, ref o_endings, ref o_nullobject);
doc.Close(ref o_null, ref o_null, ref o_null);
wordApplication.Quit(ref o_null, ref o_null, ref o_null);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplication);
}
}