C#调用WPS将文档转换成pdf进行预览

摘要:
vs startup项目可以生成wps实例,但本地iis部署的站点不能。原因是vs是管理员,而iis没有权限。解决方案:启动IIS。应用程序池-“选定的应用程序池”-高级设置-流程模型-标识:设置为管理员帐户管理员代码。1.安装WPS2016 Professional Edition。2.方法1:参考etapi。dll,wpsapi。dll,wppapi。dll。在WPS安装目录中,例如,C:\ProgramFiles(x86)

vs启动项目可以生成wps实例

本地iis部署的站点却不行

原因是vs是管理员权限,而iis没有权限

解决方法

启动IIS,应用程序池-“选定的应用程序池”-高级设置-进程模型-标识:设置为管理员账号administrator

 
C#调用WPS将文档转换成pdf进行预览第1张
 

代码

1.安装WPS 2016 专业版

2.方法一:在项目中引用etapi.dll,wpsapi.dll,wppapi.dll,在WPS的安装目录中,如C:Program Files (x86)KingsoftWPS Office10.8.2.6666office6

方法二:根据实际需要科添加下面的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进行预览》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Openlayers中使用Overlay实现点击要素弹窗并且弹窗随之移动Linux服务器丢包故障的解决下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

安卓 TextToSpeech: speak failed: not bound to TTS engine

关于语音播报一段时间没有使用系统返回 speak failed: not bound to TTS engine 错误解决办法 通过textToSpeech?.speak 返回参数判断播放是否成功如果返回-1需要重新实例化TextToSpeech。 完整代码: SystemTTS package com.dzw.pushlib.audio import...

java将Excel文件上传并解析为List数组

前端 //导入excel文件 layui.use('upload', function() { var upload =layui.upload; //指定允许上传的文件类型 var uploadInst =...

跟我一起玩Win32开发(10):绘图(C)

今天我们来欣赏一下用于填充图形的函数,当然我不会逐个去介绍,因为我们参考MSDN直接套参数就可以了。 SetDCBrushColor函数有必要扯一下,它的声明如下: [cpp] view plain copy  COLORREF SetDCBrushColor(     __in  HDC hdc,     __in  COLORREF crCo...

httpclient 实现文件上传中转

开发功能: web前端提交上传文件 —> a服务器接收 —> 转发到b服务器进行文件处理 下面是简单实现的代码,具体细节优化根本自己的需求更改。 public String handleResponse(HttpServletRequest request, HttpServletResponse response)...

Asp.Net WebAPI 通过HttpContextBase或者HttpRquest 获取请求参数

WEBAPI中的Request是HttpRequestMessage类型,不能像Web传统那样有querystring和from 方法接收参数,而传统的HttpReqest的基类是HttpReqestBase 所以这里我们就直接使用(HttpContextBase)Request.Properties["MS_HttpContext"] public vo...

C#泛型接口

使用泛型可以定义接口,在接口中定义的方法可以带泛型参数。----------《C#高级编程(第7版)》清华大学出版社 namespace Test { class Program { static void Main(string[] args) { TestClass testCla...