How to convert .doc to .pdf file in C#.


To use this code word component must be installed on the system on which application will run.
Add reference to word Interop in the application. Import the word Interop in application.

using Microsoft.Office.Interop.Word;
private void ExportDocToPDF(string DocPath, string PDFPath)
{
    object sourceFilePath = DocPath;
    object destinationFilePath = PDFPath;
    Application wordApp = new Application();
    try
    {
        object missing = System.Type.Missing;
        Document doc = wordApp.Documents.Open(ref sourceFilePath, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
            ref missing, ref missing, ref missing);

        // formt to save the file, this case PDF
        object pdfformat = WdSaveFormat.wdFormatPDF;

        // changes in paper size
        doc.PageSetup.PaperSize = WdPaperSize.wdPaperA4;

        // changes orietation paper
        doc.PageSetup.Orientation = WdOrientation.wdOrientPortrait;

        // other changes
        doc.PageSetup.LeftMargin = 20;
        doc.PageSetup.RightMargin = 0;

        // save file
        doc.SaveAs(ref destinationFilePath, ref pdfformat, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

        doc.Close(ref missing, ref missing, ref missing);
        wordApp.Quit(ref missing, ref missing, ref missing);
    }
    catch (Exception ex)
    {
       throw ex;
    }
    finally
    {
        wordApp = null;
    }
}