c development word batch to pdf source sharing

  • 2020-05-27 07:02:18
  • OfStack

Microsoft Office Word itself already provides the ability to save as PDF documents, and for a small number of documents, manually converting Word to PDF is fine. Once you have to deal with a large number of documents, you may not be able to do it. However, for the Office environment already installed, with some simple code to achieve batch Word to PDF.

Source:


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(" Please enter the Word Directory of documents ");
            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 create PDF Document, please confirm! ");
            System.Console.Write("y(yes) or n(no) ?  ");
            strFlag = System.Console.ReadLine();
            if (strFlag == "y")
            {
                System.Console.WriteLine("\n Start to create PDF The document ...");
                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 After processing, enter any key to exit ");
            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();
            }
        }
    }
}

Two problems need to be paid attention to: (1) close the open document in the code in time, see 49 lines, otherwise a temporary file will be generated; Close the "WINWORD" thread in time, otherwise the Word document will be occupied by the thread.


Related articles: