---
title: "C#  word转pdf"
author: "*YH*"
date: "2019-01-17T14:55:47Z"
tags: []
categories: ["编程语言"]
slug: "c-word-pdf"
draft: false
lastmod: "2019-01-22T10:14:27Z"
id: 2436734
url: "https://yh-fly.iteye.com/blog/2436734"
---

写在最前面，如果要部署，服务器需要安装相同版本的office，否则会出现一大堆问题
https://www.cnblogs.com/5426z/articles/4865312.html

需要单独引入的是COM中的Microsoft Office 12.0 Object Library 


```C#

public static bool WordToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Microsoft.Office.Interop.Word._Application application = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word._Document document = null;
            try
            {
                application.Visible = false;
                document = application.Documents.Open(sourcePath);
                document.ExportAsFixedFormat(targetPath, WdExportFormat.wdExportFormatPDF);
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                document.Close();
                application.Quit();
            }
            return result;
        }

```


参考：
https://www.cnblogs.com/wangxlei/p/9431149.html