1. 程式人生 > WINDOWS開發 >C# wps轉pdf(word、ppt、excel),線上預覽pdf

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);
                doc.ExportAsFixedFormat(targetType,newFilePath,XlFixedFormatQuality.xlQualityStandard,true,false,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);
                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時要設定輸出

  技術分享圖片