You can use Microsoft word Api to convert word files (.doc, .docx) to a pdf files on the fly. if you are writing an automation tool or a system that manipulate word and pdf files you can utilize this API.
Microsoft.Office.Interop.Word assembly which can be found under the windows GAC folder is very easy to use and its performance are acceptable in some scenarios, so you need to study your case carefully before deciding using the component, because its uses unmanaged resources on the OS. and it have thread safety issues.
Microsoft.Office.Interop.Word assembly which can be found under the windows GAC folder is very easy to use and its performance are acceptable in some scenarios, so you need to study your case carefully before deciding using the component, because its uses unmanaged resources on the OS. and it have thread safety issues.
using System; using Microsoft.Office.Interop.Word; using Microsoft.Office.Interop; public class WordToPdfConverter { public void Convert(string sourceWordfile, string destinationPdffile) { Application word = new Application(); if (string.IsNullOrEmpty(sourceWordfile)) { throw new ArgumentNullException("sourceWordfile", "Source word file path must be passed."); } if (string.IsNullOrEmpty(destinationPdffile)) { throw new ArgumentNullException("destinationPdffile", "Desitination Pdf file must be passed."); } try { word.Documents.Open(sourceWordfile); word.Application.Visible = false; word.WindowState = WdWindowState.wdWindowStateMinimize; Document document = word.ActiveDocument; document.SaveAs(destinationPdffile, WdSaveFormat.wdFormatPDF); } catch (Exception ex) { return string.Format("An error occured during the conversion process, the message : {0} and the stack trace : {1}", ex.Message, ex.StackTrace); } finally { word.Documents.Close(); word.Quit(WdSaveOptions.wdDoNotSaveChanges); } } }
No comments:
Post a Comment