Without using external components (such as Aspose.Words , which is paid), can be made from following, using Word interop objects:
private Microsoft.Office.Interop.Word.ApplicationClass MSdoc;
object Unknown = Type.Missing;
private void word2PDF(object Source, object Target)
{
if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();
try
{
MSdoc.Visible = false;
MSdoc.Documents.Open(ref Source, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
MSdoc.Application.Visible = false;
MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if (MSdoc != null)
{
MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
//WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
}
WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown);
}
}
I pulled it out .
The problem is that this requires Word installed on the server, which is not good for ASP.NET MVC applications.
I want to improve this answer as soon as I can get a good free alternative to do this conversion.