C#调用WPS转换Word、Excel、PPT文件为PDF,耗时稍微长一点,但是文件转换格式不存在问题。

需要安装WPS

using System;
using Excel;
using PowerPoint;
using Word;

namespace SharePint.Code
{
    public class FileTransform
    {
        /// <summary>
        /// Word转PDF
        /// </summary>
        /// <param name="pWordFile">word文件地址</param>
        /// <param name="pSavePdf">保存PDF地址</param>
        /// <returns></returns>
        public static bool WordToPdf(string pWordFile, string pSavePdf)
        {
            var type = Type.GetTypeFromProgID("KWps.Application");
            dynamic wps = Activator.CreateInstance(type);
            try
            {
                var doc = wps.Documents.Open(pWordFile, Visible: false);
                doc.ExportAsFixedFormat(pSavePdf, WdExportFormat.wdExportFormatPDF);
                doc.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                wps.Quit();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }

        /// <summary>
        /// Excel转PDF
        /// </summary>
        /// <param name="pExcelFile">Excel文件地址</param>
        /// <param name="pSavePdf">保存PDF地址</param>
        /// <returns></returns>
        public static bool ExcelToPdf(string pExcelFile, string pSavePdf)
        {
            var type = Type.GetTypeFromProgID("KET.Application");
            dynamic wps = Activator.CreateInstance(type);
            try
            {
                var targetType = XlFixedFormatType.xlTypePDF;
                var missing = Type.Missing;
                var doc = wps.Application.Workbooks.Open(pExcelFile, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                doc.ExportAsFixedFormat(targetType, pSavePdf, XlFixedFormatQuality.xlQualityStandard, true, false,
                    missing, missing, missing, missing);
                doc.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                wps.Quit();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }

        /// <summary>
        /// PPT转PDF
        /// </summary>
        /// <param name="pPptFile">PPT文件地址</param>
        /// <param name="pSavePdf">保存PDF地址</param>
        /// <returns></returns>
        public static bool PptToPdf(string pPptFile, string pSavePdf)
        {
            var type = Type.GetTypeFromProgID("KWPP.Application");
            dynamic wps = Activator.CreateInstance(type);
            try
            {
                var doc = wps.Presentations.Open(pPptFile, MsoTriState.msoCTrue, MsoTriState.msoCTrue,
                    MsoTriState.msoCTrue);
                doc.SaveAs(pSavePdf, PpSaveAsFileType.ppSaveAsPDF, MsoTriState.msoTrue);
                doc.Close();
            }
            catch (Exception e)
            {
                return false;
            }
            finally
            {
                wps.Quit();
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return true;
        }


    }
}
``
最后修改:2022 年 06 月 17 日 10 : 21 AM
如果觉得我的文章对你有用,请随意赞赏