zoukankan      html  css  js  c++  java
  • C#开发之Word批量转PDF

    C#开发之Word批量转PDF

    微软Office Word本身已经提供了另存为PDF文档功能,对于少量文档,手工使用该方式进行Word转换为PDF尚可,一旦需要处理大量的文档,可能就显得有些捉襟见肘了。不过对于已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF了。

    源码奉上:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    using System.Threading;
    using Microsoft.Office.Interop.Word;
     
    namespace Word2Pdf
    {
        class Program
        {
            public static Microsoft.Office.Interop.Word.Document wordDocument { get; set; }
     
            static void Main(string[] args)
            {
                string strFolder_f = null;
                string strFolder_t = null;
                string strFlag = null;
                System.Console.WriteLine("请输入Word文档所在目录");
                strFolder_f = System.Console.ReadLine();
                if (strFolder_f.Substring(strFolder_f.Length - 1, 1) != "\\")
                {
                    strFolder_f += "\\";
                }
                strFolder_t = strFolder_f + @"pdf\";
                System.Console.WriteLine("\n创建PDF文档,请确认!");
                System.Console.Write("y(yes) or n(no) ?  ");
                strFlag = System.Console.ReadLine();
                if (strFlag == "y")
                {
                    System.Console.WriteLine("\n开始创建PDF文档...");
                    CheckFolder(strFolder_t);
                    string strPdfFile = null;
                    DirectoryInfo TheFolder = new DirectoryInfo(strFolder_f);
     
                    Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
                    object paramMissing = Type.Missing;
     
                    foreach (FileInfo NextFile in TheFolder.GetFiles())
                    {
                        strPdfFile = Path.ChangeExtension(strFolder_t + NextFile.Name, ".pdf");
                        wordDocument = appWord.Documents.Open(NextFile.FullName);
                        if (wordDocument != null)
                        {
                            wordDocument.ExportAsFixedFormat(strPdfFile, WdExportFormat.wdExportFormatPDF);
                            wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                            wordDocument = null;
                        }
                        System.Console.Write(".. ");
                    }
     
                    if (appWord != null)
                    {
                        appWord.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                        appWord = null;
                    }
                }
     
                //KillProcessByName("WINWORD");
                GC.Collect();
                GC.WaitForPendingFinalizers();
     
                System.Console.Write("\n处理完毕,输入任意键退出");
                System.Console.ReadKey();
            }
     
            static void CheckFolder(string strFolderPath)
            {
                if (Directory.Exists(strFolderPath))
                {
                    Directory.Delete(strFolderPath, true);
                    Directory.CreateDirectory(strFolderPath);
                }
                else
                {
                    Directory.CreateDirectory(strFolderPath);
                }
            }
     
            static void KillProcessByName(string name)
            {
                Process[] ps = Process.GetProcessesByName(name);
                foreach (Process p in ps)
                {
                    if (p.ProcessName == name)
                        p.Kill();
                }
            }
        }
    }

    需要注意的两个问题:①及时关闭代码中所打开的文档,见49行,否则会产生临时文件;②及时关闭“WINWORD”线程,否则所处理的Word文档会一直处于被该线程占用的情况。


    作者:韩 锁 
    出处:http://www.cnblogs.com/hans_gis/ 
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    hiho 103 平衡树·Treap
    HDU 5738 Eureka
    codeforces 682D Alyona and Strings
    Photoshop cs5 永久序列号
    httpd.exe你的电脑中缺失msvcr110.dll怎么办
    Gzip压缩启用图文方法详细说明
    帝国CMS开启全站搜索功能
    怎么给电脑统一设置查看文件为列表形式?
    帝国CMS网站地图sitemap的制作教程,分享2种帝国cms网站地图模板
    lol安装包打开没反应
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3100026.html
Copyright © 2011-2022 走看看