C#调用WPS将文档转换成pdf进行预览
引用:https://www.jianshu.com/p/445996126c75
vs启动项目可以生成wps实例
本地iis部署的站点却不行
原因是vs是管理员权限,而iis没有权限
解决方法
启动IIS,应用程序池-“选定的应用程序池”-高级设置-进程模型-标识:设置为管理员账号administrator

代码
1.安装WPS 2016 专业版
2.方法一:在项目中引用etapi.dll,wpsapi.dll,wppapi.dll,在WPS的安装目录中,如C:\Program Files (x86)\Kingsoft\WPS Office\10.8.2.6666\office6
方法二:根据实际需要科添加下面的COM引用
原文:https://blog.csdn.net/xqf222/article/details/81237915
添加引用 -> COM -> Kingsoft Add-In Designer
添加引用 -> COM -> Microsoft Office 11.0 Object Library
添加引用 -> COM -> Upgrade WPS Office 3.0 Object Library(Beta)
添加引用 -> COM -> Upgrade WPS Presentation 3.0 Object Library(Beta)
添加引用 -> COM -> Upgrade Kingsoft WPS 3.0 Object Library(Beta)
添加引用 -> COM -> Kingsoft WPS Extend Apo 1.0 Object Library(Beta)
public class ToPdfHelper : IDisposable
{
dynamic wps;
public ToPdfHelper(string typeName)
{
if (typeName == "xls")
typeName = "KET.Application";
else if (typeName == "ppt")
typeName = "KWPP.Application";
else
typeName = "KWps.Application";
//创建wps实例,需提前安装wps
Type type = Type.GetTypeFromProgID(typeName);
if (type == null)
type = Type.GetTypeFromProgID("wps.Application");
wps = Activator.CreateInstance(type);
}
/// <summary>
/// 使用wps将Word转PDF
/// </summary>
/// <param name="saveUrl">文件路径</param>
/// <param name="targetPath">源文件路径</param>
/// <returns></returns>
public string WordWpsToPdf(string saveUrl, string targetPath)
{
if (targetPath == null)
{
throw new ArgumentNullException("wpsFilename");
}
var wordPath = saveUrl + targetPath;
var pdfPath = Path.ChangeExtension(wordPath, "pdf");
try
{
//用wps 打开word不显示界面
dynamic doc = wps.Documents.Open(wordPath, Visible: false);
//doc 转pdf
doc.ExportAsFixedFormat(pdfPath, WdExportFormat.wdExportFormatPDF);
//设置隐藏菜单栏和工具栏
//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
doc.Close();
doc = null;
}
catch (Exception e)
{
targetPath = GetEXCELtoPDF.CreatePDFs(saveUrl, targetPath);
}
finally
{
Dispose();
}
return Path.ChangeExtension(targetPath, "pdf");
}
/// <summary>
/// 使用wps将xls转PDF
/// </summary>
/// <param name="saveUrl">文件路径</param>
/// <param name="targetPath">源文件路径</param>
/// <returns></returns>
public string XlsWpsToPdf(string saveUrl, string targetPath)
{
if (targetPath == null)
{
throw new ArgumentNullException("wpsFilename");
}
var wordPath = saveUrl + targetPath;
var pdfPath = Path.ChangeExtension(wordPath, "pdf");
try
{
XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
//xls 转pdf
dynamic doc = wps.Application.Workbooks.Open(wordPath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
doc.ExportAsFixedFormat(targetType, pdfPath, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
//设置隐藏菜单栏和工具栏
//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
doc.Close();
doc = null;
}
catch (Exception e)
{
targetPath = GetEXCELtoPDF.CreatePDFs(saveUrl, targetPath);
}
finally
{
Dispose();
}
return Path.ChangeExtension(targetPath, "pdf");
}
/// <summary>
/// 使用ppt将xls转PDF
/// </summary>
/// <param name="saveUrl">文件路径</param>
/// <param name="targetPath">源文件路径</param>
/// <returns></returns>
public string PptWpsToPdf(string saveUrl, string targetPath)
{
if (targetPath == null)
{
throw new ArgumentNullException("wpsFilename");
}
var wordPath = saveUrl + targetPath;
var pdfPath = Path.ChangeExtension(wordPath, "pdf");
try
{
//ppt 转pdf
dynamic doc = wps.Presentations.Open(wordPath, 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);
doc.SaveAs(pdfPath, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
//设置隐藏菜单栏和工具栏
//wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
doc.Close();
doc = null;
}
catch (Exception e)
{
targetPath = GetEXCELtoPDF.CreatePDFs(saveUrl, targetPath);
}
finally
{
Dispose();
}
return Path.ChangeExtension(targetPath, "pdf");
}
public void Dispose()
{
if (wps != null) { wps.Quit(); wps = null; }
}
}
C#调用WPS将文档转换成pdf进行预览的更多相关文章
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- C#实现文档转换成PDF
网上有很多将doc.ppt.xls等类型的文档转换成pdf的方法,目前了解到的有两大类: 1.使用虚拟打印机将doc.ppt.xls等类型的文档 2.使用OFFICE COM组件 我采用了第二种方法实 ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
- asp.net将ppt文档转换成pdf
一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法 C# 代码 复制 // ...
- Python将word文档转换成PDF文件
如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...
- Java实现批量将word文档转换成PDF
先导入words的jar包 需要jar包的私聊我发你 代码如下:import com.aspose.words.Document;import java.io.File; public class W ...
- C# word文档转换成PDF格式文档
最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...
- JAVA:借用OpenOffice将上传的Word文档转换成Html格式
为什么会想起来将上传的word文档转换成html格式呢?设想,如果一个系统需要发布在页面的文章都是来自word文档,一般会执行下面的流程:使用word打开文档,Ctrl+A,进入发布文章页面,Ctrl ...
随机推荐
- [.NET] 控制只启动单个指定外部程序
独立观察员 2019 年 6 月 12 日 有的时候我们程序需要启动外部程序来配合实现某些功能,比如启动一个 Cef 相关程序来承载网页.那么如果那个外部程序并没有实现单例启动,我们程序去启动它的时候 ...
- Review: Basic Knowledge about WebForm
Asp.net shanzm
- 打印X
***.....***// .***...***.// ..***.***..// ...*****...// ....***....// ...*****...// ...
- 遍历倒排索引核心类:SegmentTermDocs/SegmentTermPositions
查询有哪些文档包含某个词元是Lucene搜索非常基础的一个功能,上层的搜索功能和索引功能都要基于这个功能来搭建.SegmentTermDocs就是查询词元所属文档的核心类,SegmentTermPos ...
- SQLi-LABS Page-1(Basic Challenges) Less1-Less4
Less-1 GET - Error based - Single quotes - Stri http://10.10.202.112/sqli/Less-1?id=1 http://10.10.2 ...
- 019.nexus搭建docker镜像仓库/maven仓库
一.安装docker CE 参考docker doc https://docs.docker.com/install/linux/docker-ce/centos/ 二.docker启动nexus3 ...
- Confluence 6.9.0 安装
平台环境:centos 7.6 数据库版本:mysql-5.7.26,提前安装好,安装步骤略. 软件版本:Confluence6.9.0 所需软件:提前下载到本地电脑 atlassian-conflu ...
- Windows 局域网内共享
前言 在局域网内,其它成员无需密码就可以访问某台计算机共享的某个磁盘或某个文件夹. 本文记录几个关键点, 共享端:需要共享的计算机 用户端:从共享计算机读取文件的计算机 共享端 开启guest用户 1 ...
- jq序 选择器
1.库和框架 库:小而精 直接操作DOM css() jquerry封装js的那些操作: 事件,属性, ajax(交互的技术),DOM,选择器 框架:大而全 事件,DOM,属性操作,ajax,&qu ...
- Fedora增加rc-local服务开机自启项
最近新装了一台Fedora 30系统,服务已经正常运行起来了,但是偶然发现当我的系统重启后,写在rc.local配置文件里的命令居然没生效,导致我系统重启,但是服务却没有正常运行,后来经过一番查阅 ...