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).
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.
Double-click the file to view its details and contents in Windows Explorer. 3. Click the word folder, then click the embeddings folder. Embedded content (such as embedded PDF files) will be displayed as oleObject.
Go to File > Open. Find the PDF and open it (you might have to select Browse and find the PDF in a folder). Word tells you that it's going to make a copy of the PDF and convert its contents into a format that Word can display. The original PDF won't be changed at all.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With