It turned out to be a very subtle (and yet to be identified) class conflict issue. After moving the converter to a separate class library, it is working fine.
I am posting my code here, in case someone is interested.
public static void ConvertToPdf(string fileName, string outputFileName)
{
Application word = null;
try
{
word = new Application();
Object tempName = fileName;
// Cast as Object for word Open method
Object confirmConversions = false;
Object readOnly = true;
Object addToRecentFiles = false;
Object visible = false;
Object openAndRepair = true;
Object format = WdOpenFormat.wdOpenFormatXML;
object oMissing = Missing.Value;
// Use the dummy value as a placeholder for optional arguments
Document doc = word.Documents.Open(
ref tempName,
ref confirmConversions,
ref readOnly,
ref addToRecentFiles,
ref oMissing, //PasswordDocument
ref oMissing, //PasswordTemplate
ref oMissing, //Revert
ref oMissing, //WritePasswordDocument
ref oMissing, //WritePasswordTemplate
ref oMissing, //Format
ref oMissing, //Encoding
ref visible, //Visible
ref openAndRepair, //OpenAndRepair
ref oMissing, //DocumentDirection
ref oMissing, //NoEncodingDialog
ref oMissing //XmlTransform
);
doc.Activate();
const WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF;
const bool openAfterExport = false;
const WdExportOptimizeFor optimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
const WdExportRange range = WdExportRange.wdExportAllDocument;
const WdExportItem item = WdExportItem.wdExportDocumentWithMarkup;
const bool includeDocProps = false;
const bool keepIrm = false;
const WdExportCreateBookmarks createBookmarks = WdExportCreateBookmarks.wdExportCreateHeadingBookmarks;
const bool docStructureTags = false;
const bool bitmapMissingFonts = false;
const bool useIso190051 = false;
doc.ExportAsFixedFormat(
outputFileName,
exportFormat,
openAfterExport,
optimizeFor,
range,
0,
0,
item,
includeDocProps,
keepIrm,
createBookmarks,
docStructureTags,
bitmapMissingFonts,
useIso190051
);
// Close the Word document, but leave the Word application open.
object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
((_Document) doc).Close(ref saveChanges, ref oMissing, ref oMissing);
doc = null;
}
catch (Exception ex)
{
Logger.Fatal(ex);
}
finally
{
if (word != null)
{
((_Application) word).Quit();
word = null;
}
}
}