栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

c#调用microsoft word将word另存为pdf

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c#调用microsoft word将word另存为pdf

引用 Microsoft Word 15.0 Object Library 

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace word2pdf
{
    class Program
    {   
        static void OutputNullHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            //Console.WriteLine(outLine.Data);
        }

        //static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        //{
        //    String str = outLine.Data;
        //    if (str != null && str.Contains("动态"))
        //        Console.WriteLine(outLine.Data);
        //}

        //WINWORD.EXE POWERPNT.EXE   有.EXE后缀
        static void KillProcess(String processName)
        {
            Process process = new Process();
            process.StartInfo.FileName = "taskkill";
            process.StartInfo.Arguments = "/f /im " + processName;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.OutputDataReceived += new DataReceivedEventHandler(OutputNullHandler);
            process.ErrorDataReceived += new DataReceivedEventHandler(OutputNullHandler);
            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            process.WaitForExit();
        }

        public static bool word2PDF(string sourcePath, string targetPath)
        {
            KillProcess("WINWORD.EXE");
            bool result = false;
            Application application = new Application();
            Document document = null;
            try{                
                document = application.Documents.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);                
                document.SaveAs(targetPath, WdSaveFormat.wdFormatPDF);
                result = true;
            }
            catch (Exception e){
                Console.WriteLine("Error " + e.Message);
                result = false;
            }
            finally
            {                
                if (document != null)
                {
                    try{
                        document.Close();                        
                    }
                    catch (Exception e){                        
                        result = false;
                    }
                    try{
                        Marshal.ReleaseComObject(document);
                    }
                    catch (Exception e){                    
                        result = false;
                    }
                    document = null;
                }
                if (application != null)
                {
                    try{
                        application.Quit();
                    }
                    catch (Exception e){
                        result = false;
                    }
                    try{
                        Marshal.ReleaseComObject(application);
                    }
                    catch (Exception e){
                        result = false;
                    }                    
                    application = null;
                }
                if (!result)
                    KillProcess("WINWORD.EXE");
                //主动激活垃圾回收器,主要是避免超大批量转文档时,内存占用过多,而垃圾回收器并不是时刻都在运行!
                //GC.Collect();
                //GC.WaitForPendingFinalizers();
            }
            return result;
        }

        
        //type 为1表示word转pdf, type为3表示ppt转pdf, 预留2,excel2pdf
        static void Main(string[] args)
        {
            Console.WriteLine("start word2pdf");
            int len = args.Length;
            if (len > 1)
            {
                String srcPath = args[0].Replace(""", "");  //路径有空格,用""括起来
                String tarPath = args[1].Replace(""", "");
                word2PDF(srcPath, tarPath); 
            }
            Console.WriteLine("end!");
        }


    }
}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1023641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号