C#调用WPS转换文档到PDF的的实现代码。
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的的实现代码。的更多相关文章
- LibreOffice转换文档到pdf时中文乱码
根据我的测试,LibreOffice转换文档到pdf乱码主要有三个方面的原因: 1.centos缺少中文字体 2.jdk缺少中文字体 3.LibreOffice缺少中文字体. 解决该问题需要将wind ...
- C#调用WPS将文档转换成pdf进行预览
引用:https://www.jianshu.com/p/445996126c75 vs启动项目可以生成wps实例 本地iis部署的站点却不行 原因是vs是管理员权限,而iis没有权限 解决方法 启动 ...
- Java 调用OPENOFFIC 转换文档类型
public static void office2PDF(String sourceFile, String destFile) { try { File inputFile = new File( ...
- office 文档转pdf
本地先安装 金山wps,并确保可用 工程目录 1.使用前,先执行install.bat 安装jacob 到maven本地仓库 2.复制 jacob-1.18-M2-x64.dlljacob-1.18- ...
- Java 使用 jacob 将 word 文档转换为 pdf 文件
网上查询了许许多多的博客,说利用 poi.iText.Jsoup.jdoctopdf.使用 jodconverter 来调用 openOffice 的服务来转换等等,我尝试了很多种,但要么显示不完全, ...
- java实现MsOffice文档向pdf文档转化
本篇文档实现功能,将word和ppt文档的文件转化成pdf格式的文档 应用到jacob 第一步:下载压缩包 (1)jacob官网下载jacob压缩包 (2)网址:http://sourceforge. ...
- java使用jacob将office文档转换为PDF格式
jacob 包下载地址: http://sourceforge.net/projects/jacob-project/ 下载后,将jacob 与 jacob-1.19-x64.dll放到安装jdk目录 ...
- 使用python调用wps v9转换office文件到pdf
#!/usr/bin/python2.6 # -*- coding: utf-8 -*- # pip install timeout-decorator import os import win32c ...
- Java实现office文档与pdf文档的在线预览功能
最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...
- libreoffice转换文档的方法(支持各平台各版本的libreoffice)
前段时间完成了一个利用libreoffice转换文档进行预览的资源管理系统,用的是jodconvert这个多年未更新的转换项目,由于版本不兼容等原因,导致最新版的libreoffice不能用,浪费了许 ...
随机推荐
- 引擎之旅 Chapter.2 线程库
预备知识可参考我整理的博客 Windows编程之线程:https://www.cnblogs.com/ZhuSenlin/p/16662075.html Windows编程之线程同步:https:// ...
- Redis变慢?深入浅出Redis性能诊断系列文章(二)
(本文首发于"数据库架构师"公号,订阅"数据库架构师"公号,一起学习数据库技术) 本篇为Redis性能问题诊断系列的第二篇,本文主要从应用发起的典型命令使用上进 ...
- CSS之垂直水平居中的背后
最开始,我想说,这个体系有点大,我写的并不好.就当作是一个思路吧,虽然这个思路有点乱.几乎每一个实现方案的背后都是该属性及其组合的原理,每一个都要剖析其规范细节的话,这篇文章绝不会是这样的篇幅,所以每 ...
- mocha、chai和supertest单元测试
mocha单元测试 1. 因为有时候在代码中加了新的东西需要反复测试接口 或者 别人要求 重新跑接口非常的繁琐 2. 所有我们需要一个帮我们重复测试的东西 那就是mocha 3. 先下载 一定不要全 ...
- Java 加载、编辑和保存WPS表格文件(.et/.ett)
WPS表格文件是金山开发的专门用于处理表格数据的Office工具,属于WPS Office中WPS文字.WPS表格和WPS演示三大功能模块之一.通常以.et和.ett作为文件后缀.我们在通过后端来操作 ...
- Kubernetes实践技巧:资源预留
ubernetes 的节点可以按照节点的资源容量进行调度,默认情况下 Pod 能够使用节点全部可用容量.这样就会造成一个问题,因为节点自己通常运行了不少驱动 OS 和 Kubernetes 的系统守护 ...
- P3834 【模板】可持久化线段树 2
P3834 主席树模板,求区间第k小. 1 #include <bits/stdc++.h> 2 using namespace std; 3 #define lc tr[i].ch[0] ...
- Linux文本相关命令
Linux文本相关命令 目录 Linux文本相关命令 文本排序命令 文本去重命令 基础命令cut 文本三剑客 sed awk grep 文本排序命令 sort 常用参数: -n:以数值大小进行排序 - ...
- Windows活动目录_初识
计算机组织形式 工作组(用户本地登录时构造SID进行权限分配): 域(统一身份验证与管理) 域注意事项 实体:域控.域用户.加入域的机子. 依赖的服务:netlogon服务 强制刷新组策略gpupda ...
- 【Firefox浏览器】关闭触摸板双指滑动进行前进后退的功能
痛点 本以为只是Chrome浏览器存在这一奇葩功能,没成想Firefox也沦陷了!有好一阵子在使用Firefox的时候,并未发现其存在这个功能.直到有一天,打开自己的博客,翻阅上篇< [Chro ...