ASP.NET에서 Word를 PDF로 변환

9915 단어
1. Office 기반 솔루션
  
   1.지정한 폴더 아래의 모든 워드 파일 가져오기
static void Main(string[] args)
        {

            try
            {
                Console.WriteLine("------------  ---------------");
                string path = @"XXXXXXXXXXXXXXXXXX";//word      
                string SaveFile = @"XXXXXXXXXXXXXXXXXX";//    
                DirectoryInfo di = new DirectoryInfo(path);

                Console.Write("       (         ):");
                string filename = Console.ReadLine();

                //          
                FileInfo[] FileList;
                if (!string.IsNullOrEmpty(filename))
                {
                    FileList = di.GetFiles(filename);
                }
                else
                {
                    FileList = di.GetFiles();
                }
                int Success = 0;
                int Fail = 0;
                foreach (FileInfo item in FileList)
                {
                    string Filescr = SaveFile + item.Name;
                    bool bFlagPdf = Common.FileWordToPdf.ConvertToPdf(item.FullName, Filescr);
                    if (bFlagPdf == true)
                    {
                        Success++;
                        Console.WriteLine(item.Name + "   -----      ");
                    }
                    else
                    {
                        Fail++;
                        Console.WriteLine(item.Name + "   -----      ");
                    }
                }
                Console.WriteLine("------------  ,    :---------------");
                Console.WriteLine("  PDF :" + FileList.Length + " ");
                Console.WriteLine("  PDF :" + Success + "   ");
                Console.WriteLine("  PDF :" + Fail + "   ");
                Console.ReadKey();
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

   2.Helper 클래스 만들기using Microsoft.Office.Interop.Word; using System.IO; public class FileWordToPdf_Helper { /// /// Word PDF /// /// Word /// /// public static bool ConvertToPdf(string mWordSrc,string SavsFile) { try { #region Com component Microsoft.Office.Interop.Word.Application appWord = null; object oMissing = System.Reflection.Missing.Value; if (appWord == null) { appWord = new Microsoft.Office.Interop.Word.Application(); } Microsoft.Office.Interop.Word.Document doc = appWord.Documents.Open(mWordSrc); doc.Activate(); string mPDFSrc = Path.ChangeExtension(SavsFile, "pdf"); doc.ExportAsFixedFormat(mPDFSrc, WdExportFormat.wdExportFormatPDF); object saveChanges = WdSaveOptions.wdDoNotSaveChanges; ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); doc = null; ((_Application)appWord).Quit(ref oMissing, ref oMissing, ref oMissing); appWord = null; GC.Collect(); GC.WaitForPendingFinalizers(); #endregion return true; } catch (Exception ex) { return false; throw (ex); } } }

좋은 웹페이지 즐겨찾기