Search code examples
c#.netpdfms-wordoffice-interop

How to hide pdf importer popup in word automation


When I open a PDF file with Word automation, it show a dialog that ask me to confirm the convertion (With a "do not show again" checkbox). enter image description here

Word will now convert your PDF to an editable Word document. This may take a while. The resulting Word document will be optimized to allow you to edit the text, so it might not look exactly like the original PDF, especially if the file contained lots of graphics.

How to hide this dialog ?

var application = new Microsoft.Office.Interop.Word.Application();
application.Visible = false;

try { application.ShowStartupDialog = false; }
catch { }
try { application.DisplayAlerts = WdAlertLevel.wdAlertsNone; }
catch { }

var doc = application.Documents.Open(
    inputFilePath,
    ConfirmConversions: false,
    ReadOnly: true,
    AddToRecentFiles: false,
    Revert: true,
    NoEncodingDialog: true);

PS : ConfirmConversions:true add an other dialog.


Solution

  • The solution is to edit office registry. Software\Microsoft\Office\{version}\Word\Options : DisableConvertPdfWarning

    The code below add or edit the registry key and restore state when disposing.

    public class WordPdfImportWarningRemover : IDisposable
    {
        private const string RegistryDirectoryFormat = @"Software\Microsoft\Office\{0}\Word\Options";
        private const string RegistringKeyName = "DisableConvertPdfWarning";
        private object _oldValue;
        private RegistryValueKind _oldValueKind;
        private bool _keyExists;
        private bool _registryExists;
    
        public WordPdfImportWarningRemover()
        {
            EditRegistry("16.0");
        }
    
        public WordPdfImportWarningRemover(string version)
        {
            if(version == null)
                throw new ArgumentNullException(nameof(version));
    
            EditRegistry(version);
        }
    
        private void EditRegistry(string version)
        {
            RegistryKey officeOptions = Registry.CurrentUser.OpenSubKey(string.Format(RegistryDirectoryFormat, version), true);
            if (officeOptions != null)
            {
                using (officeOptions)
                {
                    _registryExists = true;
                    var keys = officeOptions.GetValueNames();
                    if (keys.Contains(RegistringKeyName))
                    {
                        _keyExists = true;
                        _oldValue = officeOptions.GetValue(RegistringKeyName);
                        _oldValueKind = officeOptions.GetValueKind(RegistringKeyName);
                    }
                    else
                    {
                        _keyExists = false;
                    }
                    officeOptions.SetValue(RegistringKeyName, 1, RegistryValueKind.DWord);
                    officeOptions.Close();
                }
            }
            else
            {
                _registryExists = false;
            }
        }
    
        public void Dispose()
        {
            if (_registryExists)
            {
                RegistryKey officeOptions = Registry.CurrentUser.OpenSubKey(string.Format(RegistryDirectoryFormat, "16.0"), true);
                if (officeOptions != null)
                {
                    using (officeOptions)
                    {
                        if (_keyExists)
                        {
                            officeOptions.SetValue(RegistringKeyName, _oldValue, _oldValueKind);
                        }
                        else
                        {
                            officeOptions.DeleteValue(RegistringKeyName, false);
                        }
    
                        officeOptions.Close();
                    }
                }
            }
        }
    }
    

    The office version can be found with this function

    private static string FindWordVersion()
    {
        var application = new Microsoft.Office.Interop.Word.Application();
        try
        {
            string version = application.Version;
            return version;
        }
        finally
        {
            application.Quit(SaveChanges: false);
        }
    }