博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【源码】Word转PDF V1.0.1 小软件,供新手参考
阅读量:5873 次
发布时间:2019-06-19

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

昨天有一朋友让我帮忙找一款Word转PDF的软件,今天自己捣鼓出点成果封装个Helper供大家使用~ 

开源地址:https://github.com/dunitian/WordConvertPDF

软件下载:https://github.com/dunitian/WordConvertPDF/tree/master/Bin

封装了一个Helper类,供大家调用:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Microsoft.Office.Interop.Word;using System.IO;namespace WordConvertPDF{    public static class WordToPDFHelper    {        ///         /// Word转换成PDF(单个文件转换推荐使用)        ///         /// 载入完整路径        /// 保存完整路径        /// 初始页码(默认为第一页[0])        /// 结束页码(默认为最后一页)        public static bool WordToPDF(string inputPath, string outputPath, int startPage = 0, int endPage = 0)        {            bool b = true;            #region 初始化            //初始化一个application            Application wordApplication = new Application();            //初始化一个document            Document wordDocument = null;            #endregion            #region 参数设置~~我去累死宝宝了~~(所谓的参数都是根据这个方法来的:ExportAsFixedFormat)            //word路径            object wordPath = Path.GetFullPath(inputPath);            //输出路径            string pdfPath = Path.GetFullPath(outputPath);            //导出格式为PDF            WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF;            //导出大文件            WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;            //导出整个文档            WdExportRange wdExportRange = WdExportRange.wdExportAllDocument;            //开始页码            int startIndex = startPage;            //结束页码            int endIndex = endPage;            //导出不带标记的文档(这个可以改)            WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent;            //包含word属性            bool includeDocProps = true;            //导出书签            WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;            //默认值            object paramMissing = Type.Missing;            #endregion            #region 转换            try            {                //打开word                wordDocument = wordApplication.Documents.Open(ref wordPath, 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(pdfPath, wdExportFormat, false, wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true, paramCreateBookmarks, true, true, false, ref paramMissing);                }            }            catch (Exception ex)            {                b = 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;                }            }            return b;            #endregion        }        ///         /// Word转换成PDF(批量文件转换推荐使用)        ///         /// 文件完整路径        /// 保存路径        public  static int WordsToPDFs(string[] inputPaths, string outputPath)        {            int count = 0;            #region 初始化            //初始化一个application            Application wordApplication = new Application();            //初始化一个document            Document wordDocument = null;            #endregion            //默认值            object paramMissing = Type.Missing;            for (int i = 0; i < inputPaths.Length; i++)            {                #region 参数设置~~我去累死宝宝了~~(所谓的参数都是根据这个方法来的:ExportAsFixedFormat)                //word路径                object wordPath = Path.GetFullPath(inputPaths[i]);                //获取文件名                string outputName = Path.GetFileNameWithoutExtension(inputPaths[i]);                //输出路径                string pdfPath = Path.GetFullPath(outputPath + @"\" + outputName + ".pdf");                //导出格式为PDF                WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF;                //导出大文件                WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;                //导出整个文档                WdExportRange wdExportRange = WdExportRange.wdExportAllDocument;                //开始页码                int startIndex = 0;                //结束页码                int endIndex = 0;                //导出不带标记的文档(这个可以改)                WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent;                //包含word属性                bool includeDocProps = true;                //导出书签                WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;                                            #endregion                                #region 转换                try                {                    //打开word                    wordDocument = wordApplication.Documents.Open(ref wordPath, 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(pdfPath, wdExportFormat, false, wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true, paramCreateBookmarks, true, true, false, ref paramMissing);                    }                    count++;                }                catch (Exception ex)                {                }                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;            }            return count;                #endregion        }        #region 其他        ///         /// Word转换成PDF(带日记)        ///         /// 载入完整路径        /// 保存完整路径        /// 转换日记        /// 初始页码(默认为第一页[0])        /// 结束页码(默认为最后一页)        public static void WordToPDFCreateLog(string inputPath, string outputPath, out string log, int startPage = 0, int endPage = 0)        {            log = "success";            #region 初始化            //初始化一个application            Application wordApplication = new Application();            //初始化一个document            Document wordDocument = null;            #endregion            #region 参数设置~~我去累死宝宝了~~            //word路径            object wordPath = Path.GetFullPath(inputPath);            //输出路径            string pdfPath = Path.GetFullPath(outputPath);            //导出格式为PDF            WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF;            //导出大文件            WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;            //导出整个文档            WdExportRange wdExportRange = WdExportRange.wdExportAllDocument;            //开始页码            int startIndex = startPage;            //结束页码            int endIndex = endPage;            //导出不带标记的文档(这个可以改)            WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent;            //包含word属性            bool includeDocProps = true;            //导出书签            WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;            //默认值            object paramMissing = Type.Missing;            #endregion            #region 转换            try            {                //打开word                wordDocument = wordApplication.Documents.Open(ref wordPath, 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(pdfPath, wdExportFormat, false, wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true, paramCreateBookmarks, true, true, false, ref paramMissing);                }            }            catch (Exception ex)            {                if (ex != null) { log = ex.ToString(); }            }            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();            }            #endregion        }        #endregion    }}

  

转载地址:http://vvenx.baihongyu.com/

你可能感兴趣的文章
Eclipse中修改代码格式
查看>>
GRUB Legacy
查看>>
关于 error: LINK1123: failure during conversion to COFF: file invalid or corrupt 错误的解决方案...
查看>>
python实现链表
查看>>
java查找string1和string2是不是含有相同的字母种类和数量(string1是否是string2的重新组合)...
查看>>
Android TabActivity使用方法
查看>>
Eclipse的 window-->preferences里面没有Android选项
查看>>
《麦田里的守望者》--[美]杰罗姆·大卫·塞林格
查看>>
遇到的那些坑
查看>>
央行下属的上海资信网络金融征信系统(NFCS)签约机构数量突破800家
查看>>
[转] Lazy evaluation
查看>>
常用查找算法总结
查看>>
被神话的大数据——从大数据(big data)到深度数据(deep data)思维转变
查看>>
修改校准申请遇到的问题
查看>>
Linux 进程中 Stop, Park, Freeze【转】
查看>>
文件缓存
查看>>
远程协助
查看>>
Scrum实施日记 - 一切从零开始
查看>>
关于存储过程实例
查看>>
配置错误定义了重复的“system.web.extensions/scripting/scriptResourceHandler” 解决办法...
查看>>