ASP.NET Word转为PDF

1、首先安装 Microsoft Office 2007加载项:Microsoft Save as PDF-简体中文版:下载地址:

http://download.microsoft.com/download/3/8/8/388812b2-0d3f-474e-a7ef-b095d3d0d3cd/SaveAsPDF.exe

2,代码如下

/// <summary>
/// 转换word为pdf
/// </summary>
/// <param name="filename">doc文件路径</param>
/// <param name="savefilename">pdf保存路径</param>
void ConvertWordPDF1(object filename, object savefilename)
{
Object Nothing = System.Reflection.Missing.Value;
//创建一个名为WordApp的组件对象
wordApp = new ApplicationClass();
//创建一个名为WordDoc的文档对象并打开
Document doc = wordApp.Documents.Open(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//设置保存的格式
object filefarmat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
//保存为PDF
doc.SaveAs(ref savefilename, ref filefarmat, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
//关闭文档对象
doc.Close(ref Nothing, ref Nothing, ref Nothing);
//推出组建
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
doc = null;
wordApp = null;
}

//下面是各种转换格式

object filefarmat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;//设置保存的格式,还可以为以下格式:

Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML  //保存为HTML

Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatRTF     //保存为RTF

Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatText    //保存为文本

Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXML    //保存为XML

Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXPS    //保存为XPS

 

你可能感兴趣的:(ASP.NET Word转为PDF)