1. 程式人生 > >轉換Word文件為PDF檔案

轉換Word文件為PDF檔案

public bool WordToPDF2(string sourcePath)
      {

            bool result = false;
            Word.Application application = new Word.Application();
            Word.Document document = null;

            try

            {
                application.Visible = false;

                document = application.Documents.Open(sourcePath);

                string PDFPath = sourcePath.Replace(".doc", ".pdf");//pdf存放位置

                if (!File.Exists(PDFPath))//存在PDF,不需要繼續轉換

                {
                    document.ExportAsFixedFormat(PDFPath, Word.WdExportFormat.wdExportFormatPDF);
                }
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document.Close();
            }
            return result;
      }