第一步:本机安装WPS(我安装的时WPS2013)
第二步:添加引用,在COM中以Kingsoft开头的
注意:引用添加后,wps对应的时doc格式;wpp对应的是ppt格式;et对应的是excel格式
/// <summary> /// 转换DOC到PDF /// </summary> /// <param name="wpsFilename">原始文件</param> /// <param name="pdfFilename">转换后pdf文件</param> private void ConvertDocToPdf(string wpsFilename, string pdfFilename) { WPS.Application wps = new WPS.Application(); try { if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); } //保存路径为空时,保存在原始文件目录 if (pdfFilename == "") { pdfFilename = Path.ChangeExtension(wpsFilename, "pdf"); } //忽略警告提示 wps.DisplayAlerts = WPS.WpsAlertLevel.wpsAlertsNone; Document doc = wps.Documents.Open(wpsFilename,Visible: true, NoEncodingDialog: true); doc.ExportPdf(pdfFilename); doc.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } finally { //无论是否成功,都退出 if (wps != null) { wps.Terminate(); //强制关闭所有wps进程 System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("wps"); foreach (System.Diagnostics.Process prtemp in process) { prtemp.Kill(); } } } } /// <summary> /// 转换EXCEL到PDF /// </summary> /// <param name="wpsFilename">原始文件</param> /// <param name="pdfFilename">转换后pdf文件</param> private void ConvertExcelToPdf(string wpsFilename, string pdfFilename) { ET.Application eps = new ET.Application(); try { if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); } //保存路径为空时,保存在原始文件目录 if (pdfFilename == "") { pdfFilename = Path.ChangeExtension(wpsFilename, "pdf"); } //忽略警告提示 eps.DisplayAlerts = false; eps.Workbooks.Open(wpsFilename); eps.ActiveWorkbook.ExportPdf(pdfFilename); eps.ActiveWorkbook.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } finally { //无论是否成功,都退出 if (eps != null) { eps.Quit(); eps.Terminate(); //强制关闭所有et进程 System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("et"); foreach (System.Diagnostics.Process prtemp in process) { prtemp.Kill(); } } } } /// <summary> /// 转换PPT到PDF /// </summary> /// <param name="wpsFilename">原始文件</param> /// <param name="pdfFilename">转换后pdf文件</param> private void ConvertPptToPdf(string wpsFilename, string pdfFilename) { WPP.Application pps = new WPP.Application(); try { if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); } //保存路径为空时,保存在原始文件目录 if (pdfFilename == "") { pdfFilename = Path.ChangeExtension(wpsFilename, "pdf"); } //忽略警告提示 此处无法设置,原因不清楚! //pps.DisplayAlerts = WPP.WpAlertLevel.wpAlertsAll; //应用程序不可见 //pps.Visible = KSO.KsoTriState.ksoFalse; pps.Presentations.Open(wpsFilename); pps.ActivePresentation.ExportPdf(pdfFilename); pps.ActivePresentation.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } finally { //无论是否成功,都退出 if (pps != null) { pps.Quit(); //强制关闭所有wpp进程 System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("wpp"); foreach (System.Diagnostics.Process prtemp in process) { prtemp.Kill(); } } } }