首先应用中引入Microsoft.Office.Interop.Excel、Microsoft.Office.Interop.Word两个dll,将嵌入式互操作类型设为False,

WORD转换成PDF代码如下:

#region 将word文档转换成PDF格式
        /// <summary>
        /// 将word文档转换成PDF格式
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns></returns>
        private bool WordConvertPDF(string sourcePath, string targetPath)
        {
            bool result;
            Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;   //PDF格式
            object paramMissing = Type.Missing;
            Word.ApplicationClass wordApplication = new Word.ApplicationClass();
            Word.Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;

Word.WdExportFormat paramExportFormat = exportFormat;
                bool paramOpenAfterExport = false;
                Word.WdExportOptimizeFor paramExportOptimizeFor =
                        Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
                Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
                int paramStartPage = 0;
                int paramEndPage = 0;
                Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
                bool paramIncludeDocProps = true;
                bool paramKeepIRM = true;
                Word.WdExportCreateBookmarks paramCreateBookmarks =
                        Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
                bool paramDocStructureTags = true;
                bool paramBitmapMissingFonts = true;
                bool paramUseISO19005_1 = false;

wordDocument = wordApplication.Documents.Open(
                        ref paramSourceDocPath, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing, ref paramMissing, ref paramMissing,
                        ref paramMissing);

if (wordDocument != null)
                {
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                            paramExportFormat, paramOpenAfterExport,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1,
                            ref paramMissing);
                    result = true;
                }
                else
                    result = false;
            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        #endregion

EXCEL转换成PDF代码如下:

#region 将excel文档转换成PDF格式
        /// <summary>
        /// 将excel文档转换成PDF格式
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns></returns>
        private bool ExcelConvertPDF(string sourcePath, string targetPath)
        {
            bool result;
            Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF; //PDF格式
            object missing = Type.Missing;
            Excel.ApplicationClass application = null;
            Excel.Workbook workBook = null;
            try
            {
                application = new Excel.ApplicationClass();
                object target = targetPath;
                object type = targetType;
                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing, missing, missing, missing);
                if (workBook != null)
                {
                    workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                    result = true;
                }
                else
                    result = false;
            }
            catch (Exception ex)
            {
                result = false;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        #endregion

对DCOM组件进行权限配置:

1、打开comexp.msc -32

2、Microsoft Excel Application、和Microsoft Word 97-2003 Document属性里面进行配置,如下:

  标识:设为“交互式用户”

  安全:启动和激活权限添加“NETWORK SERVICE”,勾选本地启动和本地激活,访问权限添加类似

以上两点设置完成后还有问题,继续以下操作:

3、应用进程池标识转换为“LocalSystem”

4、在C:/Windows/System32/config/systemprofile和C:/Windows/SysWOW64/config/systemprofile目录下创建名为Desktop目录

出现的问题:发布到服务器上后,WORD转换没问题,EXCEL转换PDF时转换卡住,只能生成temp的临时文件,以下操作解决问题:

4、在DCOM组件的Microsoft Excel Application、和Microsoft Word 97-2003 Document属性安全中额外添加“IIS_IUSRS”用户组,权限跟之前的“NETWORL SERVICE”一样

转载于:https://www.cnblogs.com/yulin626/p/7843237.html

C# 使用Microsoft.Office.Interop将Excel、Word转换成PDF遇到的问题总结相关推荐

  1. Python 实现office单个文件或整个文件夹(word,ppt,excel)转换成PDF文件,并获取PDF文件页数

    Python 实现office单个文件或整个文件夹(word,ppt,excel)转换成PDF文件,并获取PDF文件页数 文件夹中获取需要转换的文件数,将其全部转换或可单独转换单个文件,并获取转成PD ...

  2. C#将Word转换成PDF方法总结(基于Office和WPS两种方案)

    有时候,我们需要在线上预览word文档,当然我们可以用NPOI抽出Word中的文字和表格,然后显示到网页上面,但是这样会丢失掉Word中原有的格式和图片.一个比较好的办法就是将word转换成pdf,然 ...

  3. 把word转换成PDF的实用方法

    无论是哪一个版本office中的word相信大家是非常的熟悉的,几个版本的word操作方法也是差不多的,当然大家对PDF文档肯定也不会陌生.这两者之间的互相转换大部分人都会依靠第其他软件下载安装之后进 ...

  4. word转换成pdf转换器

    word转换成pdf转换器     很多朋友向我苦诉,经常办公中遇到PD格式转换时,用普通的PDF转化出来的文字都成一堆密密麻麻的乱码字,内容也缩短了不少,里面的插图都转换不了,这时候她们不得不花上大 ...

  5. 生成目录_将word转换成PDF

    word2010生成目录_将word转换成PDF.doc 一, 概要 1.1 在 Microsoft Word2010 中 自动 生成目录 1.2 在 WPS 中 将 word 转换 成 PDF 1. ...

  6. 怎么样在线Word转换成PDF转换器

    导语:从事文字编辑行业的用户对于PDF资料文件转换成Word并不陌生,但有时候因为客观原因我们需要将编辑好的Word转换成PDF转换器在线http://app.xunjiepdf.com,方便客户浏览 ...

  7. WORD转换成PDF转换器2015官方版?

    WORD转换成PDF转换器2015官方版 Word是我们工作中或多或少都要接触的文件格式,自己做一份文件的话一般都是以Word来做,因为是最常用的一种文档.可是当我们做出的文件要发给同事或者领导的话, ...

  8. Word转换成PDF

    使用documents4j转换,仅支持windows,linux上面测试无法转换,仅适用于安装了MS Office的计算机 引入jar包 <!--word 转换成 pdf begin--> ...

  9. Java使用aspse实现Excel文件转换成PDF文件

    使用Java代码把Excel文件转换成PDF文件 需要引用aspose包,引入操作我写了一个博客,地址如下 https://blog.csdn.net/weixin_46713508/article/ ...

最新文章

  1. 原 CNCF 执行董事 Dan Kohn 辞世,沉痛哀悼
  2. 编程那么苦,学习那么累,这组漫画可以治愈(慢慢品味)
  3. 你有没有想过你的上级为什么让你干这件事情,他想干什么
  4. Object/DataSet Relational Mapping(对象/数据集关系映射)完整版本下载
  5. Hadoop 00_hadoop伪分布式,完全分布式,HA搭建
  6. nstimer循环引用_警惕使用NSTimer时的循环引用
  7. ❤️六W字《计算机基础知识》(五)(建议收藏)❤️
  8. [SHOI2012]魔法树 链剖
  9. onu光功率多少是正常_ONU、机顶盒、路由器常见网络问题及处理方法
  10. 【LeetCode】【HOT】581. 最短无序连续子数组
  11. python中验证码连通域分割的方法详解
  12. C#开发MySQL数据库程序时需要注意的几点
  13. 汇编:在BUFFER中定义了的十个带符号字,将其中的负数变成绝对值,并以十进制方式输出
  14. 2022-01-31的新年flag
  15. 散列表(Hash表)
  16. 2017腾讯校招面试回忆 成功拿到offer
  17. iif能用到mysql中吗_数据库iif
  18. 基于SVM算法的人脸微笑识别
  19. NAVICAT 还原mssql bak备份文件
  20. SCTF 2019 re部分题解(持续更新中)

热门文章

  1. 中科院自动化所目标跟踪论文整理!三篇综述、两篇ICCV 2019!
  2. 独家总结| 基于深度学习的目标检测详解
  3. 基于三维点云场景的语义及实例分割:RandLA-Net和3D-BoNet
  4. Tensorflow新手通过PlayGround可视化初识神经网络
  5. java nslookup_nslookup使用教程
  6. 软考信息安全工程师备考笔记3:第三章网络安全基础备考要点
  7. 为Android安装BusyBox
  8. JS-面向对象-操作对象的属性 / 检测对象的某个属性是否存在 / 遍历(枚举)对象的属性 / 属性的分类
  9. formSelects使用
  10. 上传下载文件到Linux服务器