博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ASP.NET将word文档转换成pdf的代码
阅读量:4319 次
发布时间:2019-06-06

本文共 4243 字,大约阅读时间需要 14 分钟。

一、添加引用

 

using Microsoft.Office.Interop.Word;

二、转换方法

1、方法

C# 代码  

/// <summary>

    /// 把Word文件转换成pdf文件

    /// </summary>

    /// <param name="sourcePath">需要转换的文件路径和文件名称</param>

    /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>

    /// <returns>成功返回true,失败返回false</returns>

    public static bool WordToPdf(string sourcePath, string targetPath)

    {

        bool result = false;

        WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式

 

1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式

        object missing = Type.Missing;

        Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;

        Document document = null;

        try

        {

            applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();

            object inputfileName = sourcePath;//需要转格式的文件路径

            string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称

            WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式

            bool openAfterExport = false;//转换完成后是否打开

            WdExportOptimizeFor wdExportOptimizeForPrint =

 

WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进

 

行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,

 

质量较差,生成的文件大小较小。

            WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全

 

部内容(枚举)

            int from = 0;//起始页码

            int to = 0;//结束页码

            WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//

 

指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.

 

导出文件有标记

            bool includeDocProps = true;//指定是否包含新导出的文件在文档属性

            bool keepIRM = true;//

            WdExportCreateBookmarks wdExportCreateWordBookmarks =

 

WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导

 

出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,

 

3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中

 

创建一个书签。

            bool docStructureTags = true;

            bool bitmapMissingFonts = true;

            bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)

            document = applicationClass.Documents.Open(ref inputfileName, 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(outputFileName, exportFormat, openAfterExport,

 

wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent,

 

includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags,

 

bitmapMissingFonts, UseISO19005_1, 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;

    }

2、简洁方法

C# 代码  

/// <summary>

    /// 把Word文件转换成pdf文件

    /// </summary>

    /// <param name="sourcePath">需要转换的文件路径和文件名称</param>

    /// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>

    /// <returns>成功返回true,失败返回false</returns>

    public static bool WordToPdf(object sourcePath, string targetPath)

    {

        bool result = false;

        WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;

        object missing = Type.Missing;

        Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;

        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,

 

WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, 0, 0,

 

WdExportItem.wdExportDocumentContent, true, true,

 

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;

    }

 

三、调用

 

 OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");

转载于:https://www.cnblogs.com/taofx/p/4137254.html

你可能感兴趣的文章
海亮SC
查看>>
[Hibernate] - Generic Dao
查看>>
【Linux】一步一步学Linux——Linux系统常用快捷键(12) 待更新...
查看>>
Vue中computed和watch使用场景和方法
查看>>
laravel路由与控制器(资源路由restful)
查看>>
Html5移动端页面自适应布局详解(阿里rem布局)
查看>>
memoize-one在React中的应用
查看>>
SpringBoot整合JDBC数据库操作第二弹-配置基本数据库连接源
查看>>
nginx日志切割脚本
查看>>
ipvsadm添加虚拟服务器报错问题
查看>>
LVS-DR集群搭建脚本
查看>>
Docker拉取的镜像源更改为国内的镜像源
查看>>
LVS健康检查脚本
查看>>
PowerCLI 对vm批量关机
查看>>
拿来即用学PYTHON:序
查看>>
github+jenkins+maven+docker自动化构建部署
查看>>
前端禁止鼠标右键、禁止全选、复制、粘贴
查看>>
六. k8s--ingress学习笔记
查看>>
二. python数组和列表
查看>>
七. k8s--volumes之pv pvc学习笔记
查看>>