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. 引擎之旅 Chapter.2 线程库

    预备知识可参考我整理的博客 Windows编程之线程:https://www.cnblogs.com/ZhuSenlin/p/16662075.html Windows编程之线程同步:https:// ...

  2. Linux下自动删除过期备份和自动异地备份

    每天自动删除过期备份 首先编写一个简单的Shell脚本DeleteExpireBackup.sh: #!/bin/bash # 修改需要删除的路径 location="/database/b ...

  3. RabbitMQ之消息模式简单易懂,超详细分享~~~

    前言 上一篇对RabbitMQ的流程和相关的理论进行初步的概述,如果小伙伴之前对消息队列不是很了解,那么在看理论时会有些困惑,这里以消息模式为切入点,结合理论细节和代码实践的方式一起来学习. 正文 常 ...

  4. 8.X 的安装配置简化体现

    文章转载自:https://mp.weixin.qq.com/s/CEYYKUub81mk59B3lqAcBA 节点无需任何安全配置,即可实现 TLS 加密通信.Https 加密通信. TLS 应用于 ...

  5. RDS MySQL内存管理

    官方文档地址:https://help.aliyun.com/product/26090.html?spm=5176.7920929.1290474.7.2c6f4f7bACaToi 官方文档地址:h ...

  6. 图片 css剪切,等比例缩放

    object-fit: cover; .img1 {//自定义图片宽高,并且等比例缩放 width: 200px; height: 400px; object-fit: cover; }

  7. Java程序设计(一)作业

    题目1:输入圆的半径,计算圆的面积,然后把面积的值输出.(注意所使用的数据类型). package test; import java.util.Scanner; public class test1 ...

  8. varchar与varchar2的区别

    1. varchar2所有字符都占两字节处理(一般情况下),varchar只对汉字和全角等字符占两字节,数字,英文字符等都是一个字节. 2. varchar2把空串等同于null处理,而varchar ...

  9. BinaryBombs(二进制炸弹实验)

    实验介绍 使用所学知识拆除Binary Bombs来增强对程序的机器级表示.汇编语言.调试器和逆向工程等理解. Binary Bombs(二进制炸弹)是一个可执行程序,是C语言编译链接成的,包含pha ...

  10. SSM项目环境快速搭建

    SSM项目的环境搭建 环境搭建的目标 工程创建 创建父工程 创建空 maven工程 xxx-parent 作为父工程 修改父工程中的 pom.xml <!--?xml version=" ...