一. 话不多说,先上代码:
public void DelComment_PPT() {
string filePath = "C:\\Projects\\MyTest\\TestNPOI\\test.pptx"; // PowerPoint 文件路径
// 创建 PowerPoint 应用程序对象
Application pptApp = new Application();
pptApp.WindowState = PpWindowState.ppWindowMinimized; // 将ppt以最小窗口打开
pptApp.DisplayAlerts = Microsoft.Office.Interop.PowerPoint.PpAlertLevel.ppAlertsNone; // 去除弹框
// 将程序委托给另一个线程进行处理,实现程序的隐藏
System.Threading.Tasks.Task.Run(() =>
{
// 在此处执行 PowerPoint 相关的任务
// 打开 PowerPoint 文件
Presentation presentation = pptApp.Presentations.Open(filePath);
// 遍历每个幻灯片
foreach (Slide slide in presentation.Slides)
{
// 遍历每个批注
foreach (Comment comment in slide.Comments)
{
// 删除批注
comment.Delete();
}
}
// 保存并关闭 PowerPoint 文件
presentation.Save();
presentation.Close();
// 退出 PowerPoint 应用程序
pptApp.Quit();
Console.WriteLine("Comments removed successfully.");
});
}
二. 引用第三方库
1.Microsoft.Office.Interop.PowerPoint
2. Office(注意,版本需要与Microsoft.Office.Interop.PowerPoint匹配)
注意: 利用这个库的前提是电脑得安装office才可生效