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

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

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

3、代码类如下

    /// <summary>
/// WPS文件转Pdf类
/// </summary>
public class ToPdfHelper : IDisposable
{
/// <summary>
/// 是否杀死全部WPS程序
/// </summary>
public bool IsKillAllWps = false;
//Wps的动态对象
dynamic wps;
/// <summary>
/// 初始化类基础信息
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <param name="IsKillAllWps">转换完成后是否杀死全部WPS应用</param>
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("找不到原文件,请检查!");
}
}
/// <summary>
/// 源文件路径
/// </summary>
public string FilePath { get; set; }
/// <summary>
/// 使用wps将Word转PDF
/// </summary>
/// <param name="TargetPath">目标文件路径,不传默认在源文件的所属目录</param>
/// <returns>Pdf文件路径</returns>
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;
}
/// <summary>
/// 使用wps将xls转PDF
/// </summary>
/// <param name="TargetPath">目标文件路径,不传默认在源文件的所属目录</param>
/// <returns>Pdf文件路径</returns>
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;
} /// <summary>
/// 使用ppt将xls转PDF
/// </summary>
/// <param name="TargetPath">目标文件路径,不传默认在源文件的所属目录</param>
/// <returns>Pdf文件路径</returns>
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;
} /// <summary>
/// 支持释放资源可以使用using
/// </summary>
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、调用代码如下

        /// <summary>
/// 开始转换Pdf
/// </summary>
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("文件不存在,检查文件路径是否正常,只支持绝对路径!");
}
}

C#调用WPS转换文档到PDF的的实现代码。的更多相关文章

  1. LibreOffice转换文档到pdf时中文乱码

    根据我的测试,LibreOffice转换文档到pdf乱码主要有三个方面的原因: 1.centos缺少中文字体 2.jdk缺少中文字体 3.LibreOffice缺少中文字体. 解决该问题需要将wind ...

  2. C#调用WPS将文档转换成pdf进行预览

    引用:https://www.jianshu.com/p/445996126c75 vs启动项目可以生成wps实例 本地iis部署的站点却不行 原因是vs是管理员权限,而iis没有权限 解决方法 启动 ...

  3. Java 调用OPENOFFIC 转换文档类型

    public static void office2PDF(String sourceFile, String destFile) { try { File inputFile = new File( ...

  4. office 文档转pdf

    本地先安装 金山wps,并确保可用 工程目录 1.使用前,先执行install.bat 安装jacob 到maven本地仓库 2.复制 jacob-1.18-M2-x64.dlljacob-1.18- ...

  5. Java 使用 jacob 将 word 文档转换为 pdf 文件

    网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...

  6. java实现MsOffice文档向pdf文档转化

    本篇文档实现功能,将word和ppt文档的文件转化成pdf格式的文档 应用到jacob 第一步:下载压缩包 (1)jacob官网下载jacob压缩包 (2)网址:http://sourceforge. ...

  7. java使用jacob将office文档转换为PDF格式

    jacob 包下载地址: http://sourceforge.net/projects/jacob-project/ 下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录 ...

  8. 使用python调用wps v9转换office文件到pdf

    #!/usr/bin/python2.6 # -*- coding: utf-8 -*- # pip install timeout-decorator import os import win32c ...

  9. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  10. libreoffice转换文档的方法(支持各平台各版本的libreoffice)

    前段时间完成了一个利用libreoffice转换文档进行预览的资源管理系统,用的是jodconvert这个多年未更新的转换项目,由于版本不兼容等原因,导致最新版的libreoffice不能用,浪费了许 ...

随机推荐

  1. LFS(Linux From Scratch)构建过程全记录(三):下载所需的软件包

    写在前面 本文将记录构建LFS的过程中,下载软件包的全过程 准备下载的路径 注意请确保$LFS已经设置完毕 我们需要创建一个文件夹,地址为$LFS/sources,用于保存对应的源码 输入的指令如下: ...

  2. TortoiseSVN 执行清理( cleanUp )失败的解决方案

    TortoiseSVN 执行清理( cleanUp )失败的解决方案 今天碰到了一个比较棘手的问题,在这里做一下记录,以方便自己和有需要的朋友在之后碰到该类问题时有个参考. 现象 更新SVN时弹出清理 ...

  3. k8s ingress-nginx 使用 snippet 添加自定义配置 (比如:新增请求头)

    比如在有些时候我们需要在 server 里或者 location 里添加一些参数,例如添加包体大小限制.添加跨域配置.添加自定义header.处理响应header等等.遇到这些需求的时候,我们开始怀念 ...

  4. centos7.9使用yum方式安装MongoDB 5.x

    1.配置阿里云yum仓库 #vim /etc/yum.repos.d/mongodb-org-5.0.repo [mngodb-org] name=MongoDB Repository baseurl ...

  5. SNI 路由和多协议端口的 TCP

    文章转载自:https://mp.weixin.qq.com/s/nMMN7hAJK6SFn1V1YyxvHA 下面是一个简单的示例配置 - 使用最新支持的 YAML 文件格式,将请求路由到一个数据库 ...

  6. kubernetes 查看pod 的容器日志

    1.pod若处于运行状态,则通过kubectl logs 即可 # 查看指定pod的日志 kubectl logs <pod_name> kubectl logs -f <pod_n ...

  7. Java 泛型程序设计

    1.  泛型类 public class Pair<T> { private T first; private T second; public void setSecond(T seco ...

  8. TTD 专题 (第一篇):C# 那些短命线程都在干什么?

    一:背景 1.讲故事 在分析的众多dump中,经常会遇到各种奇葩的问题,仅通过dump这种快照形式还是有很多问题搞不定,而通过 perfview 这种粒度又太粗,很难找到问题之所在,真的很头疼,比如本 ...

  9. 洛谷P3376 (最大流模板)

    1 #include<bits/stdc++.h> 2 #define int long long 3 using namespace std; 4 const int maxn=5005 ...

  10. 齐博x1fun实例 鉴于很多人问列表的筛选怎么放到首页、内容页等等地方 贴出方法

    application\common\fun\Field.php 你可以复制一份 也可以直接改 直接改记得加锁 不然升级就覆盖了 我们把   public function list_filter($ ...