吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 9195|回复: 19
收起左侧

[其他转载] 【源码】Word转PDF V1.0.1 小软件,供新手参考

  [复制链接]
毒逆天 发表于 2015-12-13 14:48
昨天有一朋友让我帮忙找一款Word转PDF的软件,今天自己捣鼓出点成果封装个Helper供大家使用~
开源地址:https://github.com/dunitian/WordConvertPDF
软件下载:https://github.com/dunitian/WordConvertPDF/tree/master/Bin

封装了一个Helper类,供大家调用:
[C#] 纯文本查看 复制代码
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
    {
        /// <summary>
        /// Word转换成PDF(单个文件转换推荐使用)
        /// </summary>
        /// <param name="inputPath">载入完整路径</param>
        /// <param name="outputPath">保存完整路径</param>
        /// <param name="startPage">初始页码(默认为第一页[0])</param>
        /// <param name="endPage">结束页码(默认为最后一页)</param>
        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
        }
 
        /// <summary>
        /// Word转换成PDF(批量文件转换推荐使用)
        /// </summary>
        /// <param name="inputPath">文件完整路径</param>
        /// <param name="outputPath">保存路径</param>
        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 其他
        /// <summary>
        /// Word转换成PDF(带日记)
        /// </summary>
        /// <param name="inputPath">载入完整路径</param>
        /// <param name="outputPath">保存完整路径</param>
        /// <param name="log">转换日记</param>
        /// <param name="startPage">初始页码(默认为第一页[0])</param>
        /// <param name="endPage">结束页码(默认为最后一页)</param>
        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
    }
}

33.png

免费评分

参与人数 5吾爱币 +1 热心值 +4 收起 理由
SCHBBS + 1 鼓励转贴优秀软件安全工具和文档!
dsl200970 + 1 谢谢@Thanks!
hongri3000 + 1 热心回复!
山顶的一棵草 + 1 前排支持来了~
sxc767024156 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩.

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

xjwebs 发表于 2015-12-13 15:48
话说,打开word,文件——另存为PDF格式就OK啦。
倒是PDF转word是很多人的需求,市面上免费版已经转收费了
Mooch 发表于 2015-12-13 15:29
楼主好厉害  点个赞!!                                                  
小沐 发表于 2015-12-13 14:55
dong2825678 发表于 2015-12-13 14:58
感谢楼主分享啊·
stuffer 发表于 2015-12-13 15:14
非常感谢楼主分享思路,由于自己是64位暂时没试验,但还是很感谢你
hxz8998 发表于 2015-12-13 15:25
哦哦。好东西呀,谢谢你啦!
liubenqiang 发表于 2015-12-13 15:35 来自手机
回去测试一下!
唯一哟i 发表于 2015-12-13 15:52
这好像没什么用。- -
流年cf 发表于 2015-12-13 19:34
赞一个
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-27 02:07

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表