C# wps转pdf(word、ppt、excel),在线预览pdf

摘要:
wps到pdf注意:在试用期间,我使用了wps的专业版本,并测试了windows10 10系统vs2019 webform。
wps转pdf

  注:我是在wps试用期专业版,windows10系统  vs2019 webform(.net framework4.5)测试。

  前提:需要下载安装wps专业版、企业版。

  项目中需要引用wps的com组件

    com组件Upgrade WPS Spreadsheets 3.0 Object Library (Beta)  ,对应“Excel”,C:WINDOWSassemblyGAC_32Kingsoft.Office.Interop.Etapi3.0.0.0__15d99fb7f8fe5cb4Kingsoft.Office.Interop.Etapi.dll

    com组件 Upgrade WPS Presentation 3.0 Object Library (Beta),对应“PowerPoint”,C:WINDOWSassemblyGAC_32Kingsoft.Office.Interop.Wppapi3.0.0.0__15d99fb7f8fe5cb4Kingsoft.Office.Interop.Wppapi.dll

    com组件Upgrade Kingsoft WPS 3.0 Object Library (Beta),对应“Word”,C:WINDOWSassemblyGAC_32Kingsoft.Office.Interop.Wpsapi3.0.0.0__15d99fb7f8fe5cb4Kingsoft.Office.Interop.Wpsapi.dll

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
public static class WpsToPdf
{
    /// <summary>
    /// word转pdf
    /// </summary>
    /// <param name="source">源<see cref="string"/>.</param>
    /// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
    /// <returns>The <see cref="bool"/>.</returns>
    public static bool WordWpsToPdf(string source, string newFilePath)
    {
        if (source == nullthrow new ArgumentNullException(nameof(source));
        if (newFilePath == nullthrow new ArgumentNullException(nameof(newFilePath));
 
        var type = Type.GetTypeFromProgID("KWps.Application");
        dynamic wps = Activator.CreateInstance(type);
        try
        {
            //用wps打开word不显示界面
            dynamic doc = wps.Documents.Open(source, Visible: false);
 
            //转pdf
            doc.ExportAsFixedFormat(newFilePath, WdExportFormat.wdExportFormatPDF);
 
            //设置隐藏菜单栏和工具栏
            //wps.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar);
            doc.Close();
        }
        catch (Exception e)
        {
            //添加你的日志代码
            return false;
        }
        finally
        {
            wps.Quit();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
 
        return true;
    }
 
    /// <summary>
    /// excel转pdf
    /// </summary>
    /// <param name="source">源<see cref="string"/>.</param>
    /// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
    /// <returns>The <see cref="bool"/>.</returns>
    public static bool ExcelToPdf(string source, string newFilePath)
    {
        if (source == nullthrow new ArgumentNullException(nameof(source));
        if (newFilePath == nullthrow new ArgumentNullException(nameof(newFilePath));
 
        var type = Type.GetTypeFromProgID("KET.Application");
        dynamic wps = Activator.CreateInstance(type);
        try
        {
            var targetType = XlFixedFormatType.xlTypePDF;
            var missing = Type.Missing;
            //转pdf
            var doc = wps.Application.Workbooks.Open(source, missing, missing, missing, missing, missing,
                missing, missing, missing, missing, missing, missing, missing, missing, missing);
            doc.ExportAsFixedFormat(targetType, newFilePath, XlFixedFormatQuality.xlQualityStandard, truefalse,
                missing, missing, missing, missing);
            doc.Close();
        }
        catch (Exception e)
        {
            //添加你的日志代码
            return false;
        }
        finally
        {
            wps.Quit();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return true;
    }
 
    /// <summary>
    /// ppt转pdf
    /// </summary>
    /// <param name="source">源<see cref="string"/>.</param>
    /// <param name="newFilePath">新文件路径<see cref="string"/>.</param>
    /// <returns>The <see cref="bool"/>.</returns>
    public static bool PptToPdf(string source, string newFilePath)
    {
        var type = Type.GetTypeFromProgID("KWPP.Application");
        dynamic wps = Activator.CreateInstance(type);
        try
        {
            //转pdf
            var doc = wps.Presentations.Open(source, MsoTriState.msoCTrue, MsoTriState.msoCTrue,
                MsoTriState.msoCTrue);
            doc.SaveAs(newFilePath, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
            doc.Close();
        }
        catch (Exception e)
        {
            //添加你的日志代码
            return false;
        }
        finally
        {
            wps.Quit();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return true;
    }
}

免责声明:文章转载自《C# wps转pdf(word、ppt、excel),在线预览pdf》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇mongodb $where 查询中的坑[转]Putty中文乱码解决方法下篇

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

相关文章

从源码看Spring Boot 2.0.1

Spring Boot 命名配置很少,却可以做到和其他配置复杂的框架相同的功能工作,从源码来看是怎么做到的。 我这里使用的Spring Boot版本是 2.0.1.RELEASE Spring Boot最重要的注解: @SpringBootApplication 打开它: 其中的几个注解: @SpringBootConfiguration   标注这个类...

C#(4):XML序列化

一、使用 System.Xml.Serialization类 1、定义元数据 引入System.Xml.Serialization命名空间。 XML序列化常用属性: XmlRoot XmlType XmlText XmlEnum [Serializable] [XmlRoot] public class Product { public int...

word的常用操作

using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.IO;using System.Web;using System.Data;using System.Reflection;usin...

【转载】C/C++中的char,wchar,TCHAR

点击这里查看原文章 总体简介:由于字符编码的不同,在C++中有三种对于字符类型:char, wchar_t , TCHAR。其实TCHAR不能算作一种类型,他紧紧是一个宏。我们都知道,宏在预编译的时候会被替换成相应的内容。TCHAR 在使用多字节编码时被定义成char,在Unicode编码时定义成wchar_t。 1.VC++中的char,wchar_t...

java8中List根据某一字段去重

实体类: package test; public class User { private String userid; private String username; private String age; private String address; public User(String use...

两种访问接口的方式(get和post)

跨机器、跨语言的远程访问形式一共有三种:scoket发送数据包、http发送请求、rmi远程连接; http发送请求方式;分为post和get两种方式 importjava.io.IOException; importjava.io.InputStream; import java.util.Map; importjava.util.concurre...