源代码网络上有很多,但是使用过程中发现问题还是有的,尤其是在文档属性配置不同的时候,出现的问题就是不能转换。在开发端没有问题,但在不同于开发端电脑的配置情况下,会出现意向不到的问题。此处对解决方案进行记录。

using Word = Microsoft.Office.Interop.Word;

public static bool  WordToPdf(object sourcePath, string targetPath)
        {
            bool result = false;
            Word.WdExportFormat wdExportFormatPDF = Word.WdExportFormat.wdExportFormatPDF;
            object missing = Type.Missing;
            Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
                document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
                if (document != null)
                {
                    document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, Word.WdExportOptimizeFor.wdExportOptimizeForPrint, Word.WdExportRange.wdExportAllDocument, 0, 0, Word.WdExportItem.wdExportDocumentContent, true, true, Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
                }
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (document != null)
                {
                    document.Close(ref missing, ref missing, ref missing);
                    document = null;
                }
                if (applicationClass != null)
                {
                    applicationClass.Quit(ref missing, ref missing, ref missing);
                    applicationClass = null;
                }
            }
            return result;
        }

在启动程序中输入dcomcnfg命令,打开组件服务,选定

设置其标识为启动用户,可以快速解决很多问题。

Asp.net Core使用Microsoft.Office.Interop.Word转换文档相关推荐

  1. 通过C#Microsoft.Office.Interop.Word理解互操作性

    目录 一.概念 二.应用 三.Word操作示例 四.总结 五.拓展 一.概念 C# 中的互操作性指的是C#托管代码和非托管代码之间实现互操作性的方法.(参考微软文档:互操作性概述) 参考:百度百科-- ...

  2. C#,使用office组件Microsoft.Office.Interop.Word,将网页内容下载为word的demo及权限配置要点。

    在做网页开发的时候,经常会遇到下载需求,以下这种方式是保存为原生的word格式.非直接将HTML另存为. 1. 首先是下载为word 的代码. 引用: using MSWord = Microsoft ...

  3. 错误类型:“系统找不到 Microsoft.Office.Interop.Word(转)

    错误类型:"系统找不到 Microsoft.Office.Interop.Word" 编译出现错误: "Could not load file or assembly ' ...

  4. 关于.net Microsoft.Office.Interop.Word组建操作word的问题,如何控制word表格单元格内部段落的样式。...

    控制word表格单元格内部文字样式.我要将数据导出到word当中,对于word表格一个单元格中的一段文字,要设置不同的样式,比如第一行文字作为标题要居中,加粗,第二行为正常的正文. 代码如下 publ ...

  5. html与word字体大小转换,字体大小不一样,当html转换为Word使用Microsoft.Office.Interop.Word...

    我试图将html文件转换为MS word(docx)文件. 使用Microsoft.Office.Interop.Word我能够从HTML转换为Word,但字体大小不一样的文件的HTML和Word格式 ...

  6. Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable

    错误信息: Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the appl ...

  7. Microsoft.Office.Interop.Word 创建word

    转载 https://www.cnblogs.com/chenbg2001/archive/2010/03/14/1685746.html Microsoft.Office.Interop.Word ...

  8. 无法将为“Microsoft.Office.Interop.Word.ApplicationClass”的 COM 对象强制转换为接口类型

    文章目录 1.错误原因 2. 解决方式 2.1 office多版本 2.1.1 删除多余版本 2.1.2 修复当前版本 2.1.3 其他操作(可选1) 2.1.4 其他操作(可选2) 2.2 wps卸 ...

  9. C# Microsoft.Office.Interop.Word设置Word页脚之添加当前页数

    用C#操作Word,电脑上原生的库为Microsoft.Office.Interop.Word,虽然有些慢,但也能用.操作word的另外一个库是Spire.Doc,但是商业的,破解版也不好搞,还是凑合 ...

最新文章

  1. 定义EditPlus 语法文件详解
  2. SyntaxError: Missing parentheses in call to 'print'
  3. bat文件无法保存为ansi_数组矩阵和PNG保存为nii文件
  4. python通过指定网卡发包_windows下用UDP 广播在特定网卡上发包
  5. 运算阶乘的代码_【必修1】2.4可以复用的代码
  6. rpt水晶报表制作过程
  7. 论文阅读02:基于深度学习的图像细粒度分类算法
  8. kesioncms ajax分页,改进KesionCMS V9.0x SQL标签分页支持嵌套
  9. 正确地实现以太币转账
  10. 快速向表中插入大量数据Oracle中append与Nologgin的作用
  11. java中UUID类生成32位随机数(附加 6 位随机数)
  12. 基于SpringBoot的框架SOFABoot,青出于蓝而胜于蓝
  13. Atcoder ARC093F : Dark Horse
  14. 1359: 最大公约数
  15. 微信小程序实现微信APP上的扫一扫扫码跳到小程序对应的结果页面和签字等功能
  16. vue3子组件修改传值给父组件报警告warn:Component emitted event “update:dialogAddVisible”
  17. mysql每秒写入量_MySQL每秒可以插入多少条记录?影响MySQL插入速度的因素有哪些?...
  18. android obb在哪,安卓手机如何打开.obb文件?
  19. python一键扣图_Python实例:一键批量抠图
  20. c++中按位取反运算

热门文章

  1. for 语句的基本用法
  2. 如何将centos 语系设置成为中文
  3. 致那些徘徊在测试界的屌丝
  4. 计算机网络公众号,计算机网络中写公众号文章的软件有哪些
  5. Java的四种访问修饰符介绍
  6. python: python环境搭建
  7. laydate时间控件在谷歌浏览器中兼容性问题
  8. 阿里云服务器如何设置IPV6通过appstore的审核
  9. 外置USB供电与内置锂电池供电自动切换电路
  10. 这个 Go 开发的网络抓包工具,不仅好用还支持ES检索