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不能用,浪费了许 ...
随机推荐
- 璞华HawkEye平台助力乳品行业巨头在数字化转型中领“鲜”一步!
中国乳制品的市场规模接近4,000亿.在今天,产业数字化正在帮助这个传统产业实现更高质量的发展. 乳品行业现状 随着乳品行业规模扩大,各工厂引进大量的专用设备,设备故障也随之增多.设备的突发故障极易造 ...
- G&GH01 注册/安装/设置
注意事项与声明 平台: Windows 10 作者: JamesNULLiu 邮箱: jamesnulliu@outlook.com 博客: https://www.cnblogs.com/james ...
- Rust-语句和表达式
语句和表达式 Rust 的函数体是由一系列语句组成,最后由一个表达式来返回值,例如: fn add_with_extra(x: i32, y: i32) -> i32 { let x = x + ...
- Django环境安装
1.安装Django # 自动安装PyPi提供的最新版本 pip install django # 安装指定版本 pip install django==2.2 # 验证安装 >>> ...
- MySQL集群搭建(4)-MMM+LVS+Keepalived
1 LVS 介绍 1.1 简介 LVS 是 Linux Virtual Server 的简写,意即 Linux 虚拟服务器,是一个虚拟的服务器集群系统.本项目在 1998 年 5 月由章文嵩博士成立, ...
- Docker 容器日志管理
Docker 日志分为两类: Docker 引擎日志(也就是 dockerd 运行时的日志), 容器的日志,容器内的服务产生的日志. 一 .Docker 引擎日志 Docker 引擎日志一般是交给了 ...
- NSIS限制程序运行次数和使用日期
#七八年前写着玩的小东西,实际用途不大,但对刚接触nsis的新手来说应该还有一些帮助,包括创建控件,获取系统时间等,与诸位共勉! !system '>blank set/p=MSCF<nu ...
- 案例分享-https证书链不完整导致请求失败
背景 话不多说,直接上堆栈 javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX p ...
- 微服务架构学习与思考(10):微服务网关和开源 API 网关01-以 Nginx 为基础的 API 网关详细介绍
微服务架构学习与思考(10):微服务网关和开源 API 网关01-以 Nginx 为基础的 API 网关详细介绍 一.为什么会有 API Gateway 网关 随着微服务架构的流行,很多公司把原有的单 ...
- 基于tauri+vue3.x多开窗口|Tauri创建多窗体实践
最近一种在捣鼓 Tauri 集成 Vue3 技术开发桌面端应用实践,tauri 实现创建多窗口,窗口之间通讯功能. 开始正文之前,先来了解下 tauri 结合 vue3.js 快速创建项目. taur ...