zoukankan      html  css  js  c++  java
  • #c word转换PDF

    需要引用Microsoft.Office.Interop.Word,版本是07之上的。
    这个版本会判断文件是否被占用。

    using Microsoft.Office.Interop.Word;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Runtime.InteropServices;
    namespace SavePdf
    {
        class Program
        {
            private static int NONE_EXIST = -1;
            private static int OCCUPY = 1;
            private static int NONE_OCCUPY = 0;
            [DllImport("kernel32.dll")]
            public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
    
            [DllImport("kernel32.dll")]
            public static extern bool CloseHandle(IntPtr hObject);
    
            public const int OF_READWRITE = 2;
            public const int OF_SHARE_DENY_NONE = 0x40;
            public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
            static void Main(string[] args)
            {
                Program sp = new Program();
                foreach (var arg in args)
                {
                    Console.WriteLine(arg);
                }
                if (args.Length == 2)
                {
                    if (sp.IsOCcupy(args[0]) == NONE_OCCUPY)
                    {
                        DateTime dt = DateTime.Now;
    
                        sp.SavePdf(args[0], args[1]);
                        TimeSpan ts = DateTime.Now.Subtract(dt);
                        Console.WriteLine(ts.ToString() + "ms");
                    }
                }
    
            }
            public int IsOCcupy(String vFileName)
            {
                if (!File.Exists(vFileName))
                {
    
                    return NONE_EXIST;
                }
                IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
                if (vHandle == HFILE_ERROR)
                {
    
                    return OCCUPY;
                }
                CloseHandle(vHandle);
                return NONE_OCCUPY;
            }
    
            private void SavePdf(String filePath, String outFile)
            {
                object oMissing = System.Reflection.Missing.Value;
                _Application wordApp;
                _Document wordDoc;
                if (File.Exists(filePath) == false)
                {
                    return;
                }
                FileInfo fi = new FileInfo(filePath);
                object outputFileName = Path.Combine(fi.Directory.FullName, outFile);
    
                object fileFormat = WdSaveFormat.wdFormatPDF;
                wordApp = new Microsoft.Office.Interop.Word.Application();
                wordApp.Visible = false;
                object file = (object)filePath;
                wordDoc = wordApp.Documents.Open(ref file, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                wordDoc.SaveAs(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                try
                {
                    if (wordDoc != null)
                    {
                        wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                    }
                    if (wordApp != null)
                    {
                        wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return;
            }
        }
    }
    
    
  • 相关阅读:
    solr8.4.1开发测试环境的简单应用
    spring aop + xmemcached 配置service层缓存策略
    git配置httpd服务-web_dav模式
    notepad++快捷键
    Eclipse默认快捷键说明
    maven&nexus_repository 私库搭建与使用
    CENTOS下搭建git代码仓库 ssh协议
    送给iOS求职者的一份硬核面试指南,你可以不优秀,但是你必须重视!
    2020年中高级iOS大厂面试宝典+答案
    iOS开发者经验总结:在腾讯的九年,我的成长之路和职业思考
  • 原文地址:https://www.cnblogs.com/wardensky/p/4269286.html
Copyright © 2011-2022 走看看