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 ...
随机推荐
- 『CSP2019初赛后的总结』
初赛已经过去了,分数大概也已经知道了,接下来的一个月停课应该就是全部准备复赛. 联赛前几次讲课的内容是组合计数,计数\(dp\),字符串,概率期望,数论,数据结构,多数知识点难度都是大于联赛难度的,不 ...
- Spring源码系列 — 容器Extend Point(一)
前言 前文介绍了Spring中的BeanDefinition的细节,随着Spring的启动流程,这节我们介绍Spring的后续处理过程 - Spring的扩展点: BeanFactoryPostPro ...
- 转 经典分类网络Googlenet
转自https://my.oschina.net/u/876354/blog/1637819 2014年,GoogLeNet和VGG是当年ImageNet挑战赛(ILSVRC14)的双雄,GoogLe ...
- VS2013(InstallShield2015LimitedEdition)打包程序详解
VS2012没有自带打包工具,所以要先下载并安装一个打包工具.我采用微软提供的打包工具: InstallShield2015LimitedEdition.下载地址:https://msdn.micr ...
- 从新手小白到老手大白的心路历程-First Blog
本人于2019年毕业重庆市某一所乡间大学,所学专业方向是.net,至今已经工作了1个多月了,天天被上司骂,还差点儿被开除,但我死皮赖脸的勉强的“活”了下来,在今后的日子里面,我会陆续的分享我的成长经历 ...
- SqlServer 创建数据库两种方式
一个SqlServer 数据库实例大概可以创建三万多个数据库. 创建数据库的第一种方式:SqlServer Management Studio管理工具进行可视化创建. 1).打开数据库管理工具,在&q ...
- js删除html标记 去掉所有html标记
js删除html标记 去掉所有html标记 function delHtml(str){ return str.replace(/<[^>]+>/g,""); / ...
- ConstraintLayout 用法
当前描述是基于constraint-layout:1.1.2. 一.前言 在以前,android是使用布局如LinearLayout .RelativeLayout等来构建页面,但这些布局使用起来很麻 ...
- javascript 和oc交互
http://www.apkbus.com/android-127963-1-1.html
- Python从零开始——集合Set
一:Python集合知识概览 二:Python的特性.格式.以及各序列结构对比 三:Python集合set的创建 四:集合常用操作之——添加元素 五:集合常见操作之——删除元素 六:集合常见操作之—— ...