using Word = Microsoft.Office.Interop.Word;
/// <summary>
/// word轉爲PDF
/// </summary>
/// <param name="inputFilePath">傳入文件的路徑</param>
/// <param name="outFilePath">傳出文件的路徑</param>
/// <returns></returns>
public static bool wordToPDF(string inputFilePath, string outFilePath)
{
// 1. 打開Word文件
Word.Application wordApp = null;
Word.Document wordDoc = null;
try
{
wordApp = new Word.Application();//创建word应用程序
wordDoc = wordApp.Documents.Open(inputFilePath); // 更改為你的Word文件路径
wordDoc.SaveAs2(outFilePath, Word.WdSaveFormat.wdFormatPDF); // 更改為你的输出目录和文件名
}
catch (Exception e)
{
throw e;
}
finally
{
if (wordDoc != null)
{
object saveOption = Word.WdSaveOptions.wdSaveChanges;
wordDoc.Close(saveOption);
}
if (wordApp != null)
{
object saveOption = Word.WdSaveOptions.wdDoNotSaveChanges;
wordApp.Quit(saveOption);
}
}
return true;
}