使用wps插件,實現word轉PDF


 

項目需求:不打算用office自帶的組件實現word轉pdf操作

環境需求:安裝wps2016專業版

  1. 新建一個控制台應用程序
  2. 添加引用:在COM下 Kingsoft Add-In Designer和Upgrade Kingsoft WPS 3.0 Object Library(Beta)
  3. 引用中出現了Word引用圖標,右鍵---屬性---嵌入互操作類型 改為False
  4. 創建轉換工具類 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Word;

namespace wpswordtopdf
{
    public class Wps2Pdf : IDisposable
    {
        dynamic wps;

        public Wps2Pdf()
        {
            //這里創建wps實例不知道用了什么騷操作就沒有報錯過 本機安裝的是wps2016

            var type = Type.GetTypeFromProgID("KWps.Application");
            wps = Activator.CreateInstance(type);
        }
        public void ToPdf(string wpsFilename, string pdfFilename = null)
        {
            if (wpsFilename == null) { throw new ArgumentNullException("wpsFilename"); }
            if (pdfFilename == null)
            {
                pdfFilename = Path.ChangeExtension(wpsFilename, "pdf");
            }
            Console.WriteLine($@"正在轉換 [{wpsFilename}]-> [{pdfFilename}]");

            dynamic doc = wps.Documents.Open(wpsFilename, Visible: false);//這句大概是用wps 打開  word  不顯示界面
            doc.ExportAsFixedFormat(pdfFilename, WdExportFormat.wdExportFormatPDF);//doc  轉pdf 
            doc.Close();
        }

        public void Dispose()
        {
            throw new NotImplementedException();
        }
    }
}
View Code

主應用調用

        static void Main(string[] args)
        {
            Wps2Pdf wps2 = new Wps2Pdf();
            wps2.ToPdf(@"D://1.docx");
        }    

完成!!!!


免責聲明!

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



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