C#调用WPS转换文档到PDF的的实现代码。 您所在的位置:网站首页 pdf转pptWPS C#调用WPS转换文档到PDF的的实现代码。

C#调用WPS转换文档到PDF的的实现代码。

2024-06-10 21:09| 来源: 网络整理| 查看: 265

1、WPS安装,最好用这个版本别的版本不清楚,安装Pro Plus2016版本。

https://ep.wps.cn/product/wps-office-download.html

 

2、添加相关的引用:wpsapiex.dll,etapi.dll,wppapi.dll,wpsapi.dll,目前就发现这几个

 

 

 

3、代码类如下

/// /// WPS文件转Pdf类 /// public class ToPdfHelper : IDisposable { /// /// 是否杀死全部WPS程序 /// public bool IsKillAllWps = false; //Wps的动态对象 dynamic wps; /// /// 初始化类基础信息 /// /// 文件路径 /// 转换完成后是否杀死全部WPS应用 public ToPdfHelper(string FilePath, bool IsKillAllWps = false) { if (File.Exists(FilePath)) { this.IsKillAllWps = IsKillAllWps; this.FilePath = FilePath; string Extension = Path.GetExtension(FilePath).ToLower();//扩展名 ".aspx" switch (Extension) { case "xls": Extension = "KET.Application"; break; case "xlsx": Extension = "KET.Application"; break; case "ppt": Extension = "KWPP.Application"; break; case "pptx": Extension = "KWPP.Application"; break; default: Extension = "KWps.Application"; break; } Type type = Type.GetTypeFromProgID(Extension); if (type == null) { Extension = "wps.Application"; type = Type.GetTypeFromProgID("wps.Application"); } wps = Activator.CreateInstance(type); //比较完整的一些 //WPS文字 KWPS.Aplication //WPS的Excel KET.Application //WPS的演示文档 KWPP.Application //Word Word.Application //Excel Excel.Application //Powerpoint Powerpoint.Application } else { throw new Exception("找不到原文件,请检查!"); } } /// /// 源文件路径 /// public string FilePath { get; set; } /// /// 使用wps将Word转PDF /// /// 目标文件路径,不传默认在源文件的所属目录 /// Pdf文件路径 public string WordWpsToPdf(string TargetPath = "") { if (string.IsNullOrEmpty(FilePath)) { throw new Exception("请传入文件路径"); } //如果没传入文件路径就默认使用源目录 if (string.IsNullOrEmpty(TargetPath)) { TargetPath = Path.ChangeExtension(FilePath, "pdf"); } try { //忽略警告提示 wps.DisplayAlerts = false; //用wps 打开word不显示界面 dynamic doc = wps.Documents.Open(FilePath, Visible: false); //保存为Pdf doc.ExportAsFixedFormat(TargetPath, Word.WdExportFormat.wdExportFormatPDF); //设置隐藏菜单栏和工具栏 //wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar); doc.Close(); doc = null; } catch (Exception e) { throw e; } finally { Dispose(); } return TargetPath; } /// /// 使用wps将xls转PDF /// /// 目标文件路径,不传默认在源文件的所属目录 /// Pdf文件路径 public string XlsWpsToPdf(string TargetPath = "") { if (string.IsNullOrEmpty(FilePath)) { throw new Exception("请传入文件路径"); } //如果没传入文件路径就默认使用源目录 if (string.IsNullOrEmpty(TargetPath)) { TargetPath = Path.ChangeExtension(FilePath, "pdf"); } try { XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF; object missing = Type.Missing; //忽略警告提示 wps.DisplayAlerts = false; //xls 转pdf dynamic doc = wps.Application.Workbooks.Open(FilePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing); //保存为Pdf doc.ExportAsFixedFormat(targetType, TargetPath, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing); //设置隐藏菜单栏和工具栏 //wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar); doc.Close(); doc = null; } catch (Exception e) { throw e; } finally { Dispose(); } return TargetPath; } /// /// 使用ppt将xls转PDF /// /// 目标文件路径,不传默认在源文件的所属目录 /// Pdf文件路径 public string PptWpsToPdf(string TargetPath = "") { if (string.IsNullOrEmpty(FilePath)) { throw new Exception("请传入文件路径"); } //如果没传入文件路径就默认使用源目录 if (string.IsNullOrEmpty(TargetPath)) { TargetPath = Path.ChangeExtension(FilePath, "pdf"); } try { //忽略警告提示 wps.DisplayAlerts = false; //ppt 转pdf dynamic doc = wps.Presentations.Open(FilePath, MsoTriState.msoCTrue, MsoTriState.msoCTrue, MsoTriState.msoCTrue); object missing = Type.Missing; //doc.ExportAsFixedFormat(pdfPath, PpFixedFormatType.ppFixedFormatTypePDF, // PpFixedFormatIntent.ppFixedFormatIntentPrint, // MsoTriState.msoCTrue, PpPrintHandoutOrder.ppPrintHandoutHorizontalFirst, // PpPrintOutputType.ppPrintOutputBuildSlides, // MsoTriState.msoCTrue, null, PpPrintRangeType.ppPrintAll,"", // false, false, false, false, false, missing); //保存为Pdf doc.SaveAs(TargetPath, PowerPoint.PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue); //设置隐藏菜单栏和工具栏 //wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar); doc.Close(); doc = null; } catch (Exception e) { throw e; } finally { Dispose(); } return TargetPath; } /// /// 支持释放资源可以使用using /// public void Dispose() { if (wps != null) { wps.Quit(); //释放掉wps对象 wps = null; #region 强制关闭所有wps的功能慎用,尤其是带并发的 //强制关闭所有wps进程,解决文件占用的问题 if (this.IsKillAllWps) { System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName("wps"); foreach (System.Diagnostics.Process prtemp in process) { prtemp.Kill(); } } #endregion } } }

3、调用代码如下

/// /// 开始转换Pdf /// private void StatButton_Click(object sender, EventArgs e) { if (File.Exists(PdfFileTextBox.Text)&& Path.IsPathRooted(PdfFileTextBox.Text)) { Stopwatch sw = new Stopwatch(); sw.Start(); using (ToPdfHelper Help = new ToPdfHelper(PdfFileTextBox.Text,true)) { Help.WordWpsToPdf(); } sw.Stop(); TimeSpan ts2 = sw.Elapsed; TimeLabel.Text = string.Format("转换使用时间:总共花费{0}ms.", ts2.TotalMilliseconds); } else { MessageBox.Show("文件不存在,检查文件路径是否正常,只支持绝对路径!"); } }

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有