zoukankan      html  css  js  c++  java
  • C# wps转pdf(word、ppt、excel),在线预览pdf

    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;
        }
    }
  • 相关阅读:
    自定义swiper索引值的样式
    返回上一页面后页面后自动刷新(跨域也可)
    点击跳转到页面指定位置
    移动端输入框弹起影响布局(包括fixed布局情况)
    thinkPHP5模版页面volist循环offset使用
    Vue i18n国际化在实际项目中的使用
    前端Cookie、essionStorage、LocalStorage 的区别
    前端常用的一些浏览器对象
    前端取?后面的值
    前端下载文件重命名解决
  • 原文地址:https://www.cnblogs.com/xtjatswc/p/13321186.html
Copyright © 2011-2022 走看看