/// <summary> /// Converts a Word document to PDF /// </summary> /// <param name="inputFile">The Word input file</param> /// <param name="outputFile">The PDF output file</param> /// <returns></returns> internal void Convert(string inputFile, string outputFile) { DeleteResiliencyKeys(); WordInterop.DocumentClass document = null; try { StartWord(); document = (WordInterop.DocumentClass)OpenDocument(inputFile, false); // Do not remove this line!! // This is yet another solution to a weird Office problem. Sometimes there // are Word documents with images in it that take some time to load. When // we remove the line below the ExportAsFixedFormat method will be called // before the images are loaded thus resulting in an un endless loop somewhere // in this method. // ReSharper disable once UnusedVariable var count = document.ComputeStatistics(WordInterop.WdStatistic.wdStatisticPages); Logger.WriteToLog($"Exporting document to PDF file '{outputFile}'"); document.ExportAsFixedFormat(outputFile, WordInterop.WdExportFormat.wdExportFormatPDF); Logger.WriteToLog("Document exported to PDF"); } catch (Exception) { StopWord(); throw; } finally { CloseDocument(document); } }
/// <summary> /// Converts a Word document to PDF /// </summary> /// <param name="inputFile">The Word input file</param> /// <param name="outputFile">The PDF output file</param> /// <returns></returns> internal static void Convert(string inputFile, string outputFile) { DeleteAutoRecoveryFiles(); WordInterop.ApplicationClass word = null; WordInterop.DocumentClass document = null; try { word = new WordInterop.ApplicationClass { ScreenUpdating = false, DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone, DisplayDocumentInformationPanel = false, DisplayRecentFiles = false, DisplayScrollBars = false, AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable }; word.Options.UpdateLinksAtOpen = false; word.Options.ConfirmConversions = false; word.Options.SaveInterval = 0; word.Options.SaveNormalPrompt = false; word.Options.SavePropertiesPrompt = false; word.Options.AllowReadingMode = false; word.Options.WarnBeforeSavingPrintingSendingMarkup = false; word.Options.UpdateFieldsAtPrint = false; word.Options.UpdateLinksAtOpen = false; word.Options.UpdateLinksAtPrint = false; document = (WordInterop.DocumentClass)Open(word, inputFile, false); // Do not remove this line!! // This is yet another solution to a weird Office problem. Sometimes there // are Word documents with images in it that take some time to load. When // we remove the line below the ExportAsFixedFormat method will be called // before the images are loaded thus resulting in an unendless loop somewhere // in this method. // ReSharper disable once UnusedVariable var count = document.ComputeStatistics(WordInterop.WdStatistic.wdStatisticPages); word.DisplayAutoCompleteTips = false; word.DisplayScreenTips = false; word.DisplayStatusBar = false; document.ExportAsFixedFormat(outputFile, WordInterop.WdExportFormat.wdExportFormatPDF); } finally { if (document != null) { document.Saved = true; document.Close(false); Marshal.ReleaseComObject(document); } if (word != null) { word.Quit(false); Marshal.ReleaseComObject(word); } } }