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:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Etapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Etapi.dll

    com組件 Upgrade WPS Presentation 3.0 Object Library (Beta),對應“PowerPoint”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wppapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wppapi.dll

    com組件Upgrade Kingsoft WPS 3.0 Object Library (Beta),對應“Word”,C:\WINDOWS\assembly\GAC_32\Kingsoft.Office.Interop.Wpsapi\3.0.0.0__15d99fb7f8fe5cb4\Kingsoft.Office.Interop.Wpsapi.dll

    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 == null) throw new ArgumentNullException(nameof(source));
            if (newFilePath == null) throw 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 == null) throw new ArgumentNullException(nameof(source));
            if (newFilePath == null) throw 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, true, false,
                    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;
        }
    }

 在線預覽pdf

  以前一直搜索不到“在線預覽pdf方法,后來在使用Aspose時發現了在線預覽的秘密,跟大家分享下。

  注意:在線預覽pdf主要使針對比較現代化的瀏覽器。

  

 

      后台輸出pdf時要設置輸出

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM