在 C# 中将 Word 转换为 PDF 时防止警报
Prevent alert while converting Word to PDF in C#
我正在使用以下代码将文件夹中的文档转换为 PDF
string[] filePaths = Directory.GetFiles(txtFolderPath.Text, "*.doc",
SearchOption.TopDirectoryOnly);
foreach (string path in filePaths)
{
Application app = new Application();
app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
app.Visible = true;
var objPresSet = app.Documents;
var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
var temppath = path;
var pdfFileName = Path.ChangeExtension(path, ".pdf");
var pdfPath = Path.Combine(Path.GetDirectoryName(path), pdfFileName);
try
{
objPres.ExportAsFixedFormat(
pdfPath,
WdExportFormat.wdExportFormatPDF,
false,
WdExportOptimizeFor.wdExportOptimizeForPrint,
WdExportRange.wdExportAllDocument
);
}
catch
{
pdfPath = null;
}
finally
{
objPres.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
}
但是对于每个文档,我都会在下面弹出弹出窗口,尽管我已将警报设置为 none。
由于文件数量巨大,我如何在 C# 中以编程方式停止此警报。
您必须将第二个参数设置为 MsoTriState.msoFalse
,如下所示:
var objPres = objPresSet.Open(
path,
MsoTriState.msoFalse /* ConfirmConversions */,
MsoTriState.msoTrue,
MsoTriState.msoFalse);
因为您正在查看“转换文件”对话框,而 ConfirmConversions 控制着该对话框是否出现在您面前:
True to display the Convert File dialog box if the file isn't in Microsoft Word format.
MSDN 上的 Documents.Open 规范中提到了这一点。
看起来并不是所有的 *.doc 文件都是实际的 Word 文档,因此会弹出“转换文件”对话框。我假设 Word 会抛出异常,如果它的梦想转换(在你的富文本格式的例子中)结果是错误的,也就是不是 RTF 文件格式。
我正在使用以下代码将文件夹中的文档转换为 PDF
string[] filePaths = Directory.GetFiles(txtFolderPath.Text, "*.doc",
SearchOption.TopDirectoryOnly);
foreach (string path in filePaths)
{
Application app = new Application();
app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
app.Visible = true;
var objPresSet = app.Documents;
var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
var temppath = path;
var pdfFileName = Path.ChangeExtension(path, ".pdf");
var pdfPath = Path.Combine(Path.GetDirectoryName(path), pdfFileName);
try
{
objPres.ExportAsFixedFormat(
pdfPath,
WdExportFormat.wdExportFormatPDF,
false,
WdExportOptimizeFor.wdExportOptimizeForPrint,
WdExportRange.wdExportAllDocument
);
}
catch
{
pdfPath = null;
}
finally
{
objPres.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
}
但是对于每个文档,我都会在下面弹出弹出窗口,尽管我已将警报设置为 none。
由于文件数量巨大,我如何在 C# 中以编程方式停止此警报。
您必须将第二个参数设置为 MsoTriState.msoFalse
,如下所示:
var objPres = objPresSet.Open(
path,
MsoTriState.msoFalse /* ConfirmConversions */,
MsoTriState.msoTrue,
MsoTriState.msoFalse);
因为您正在查看“转换文件”对话框,而 ConfirmConversions 控制着该对话框是否出现在您面前:
True to display the Convert File dialog box if the file isn't in Microsoft Word format.
MSDN 上的 Documents.Open 规范中提到了这一点。
看起来并不是所有的 *.doc 文件都是实际的 Word 文档,因此会弹出“转换文件”对话框。我假设 Word 会抛出异常,如果它的梦想转换(在你的富文本格式的例子中)结果是错误的,也就是不是 RTF 文件格式。