首先呢,在项目中建立一个文件夹 wordpath,这个文件夹是存放你的word和pdf的。javascript
首先要准备一个word放进去(.doc)。java
这个呢也是须要引用的,这个引用2015中就有 引用-添加引用-扩展 Microsoft.Office.Interop.Word.dll浏览器
而后呢 准备copy代码app
public bool WordToPDF(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; }
贴上去以后你会发现 你的引用没加 会报错呦,所以就要加一个引用,像这样using Word = Microsoft.Office.Interop.Word;visual-studio
等你加完引用以后 你会发现 艾玛 应该没什么问题了,有问题的话 你能够百度查查。哈哈哈...spa
其次就是找个地方调用了,这里呢就在Page_Load里面调用了code
if (!IsPostBack) { string strWord = Server.MapPath("/wordpath/***.doc");//文档路径 WordToPDF(strWord); string browsertype = Page.Request.Browser.Type;//浏览器类型判断 if (browsertype != "IE6" && browsertype != "IE7") { //这里是生成好的pdf是作一个显示,在这一步以前 pdf已经生成好了 Response.Write("<script language='javascript'>window.open('/wordpath/***.pdf');</script>"); } }
就是用微软的一个方法 以 PDF 或 XPS 格式保存文档。orm
若是须要扩展能够去官网看看blog
https://docs.microsoft.com/zh-cn/previous-versions/visualstudio/visual-studio-2010/bb398522(v=vs.100)ip