吾爱汇编

 找回密码
 立即注册

QQ登录

绑定QQ避免忘记帐号

查看: 2248|回复: 6

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

[复制链接]
毒逆天 发表于 2015-12-14 14:35 | 显示全部楼层 |阅读模式

【源码】Word转PDF V1.0.1 小软件,供新手参考
昨天有一朋友让我帮忙找一款Word转PDF的软件,今天自己捣鼓出点成果封装个Helper供大家使用~
开源地址:https://github.com/dunitian/WordConvertPDF
软件下载:https://github.com/dunitian/WordConvertPDF/tree/master/Bin

                               
登录/注册后可看大图

                               
登录/注册后可看大图

                               
登录/注册后可看大图
封装了一个Helper类,供大家调用:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Office.Interop.Word;
  7. using System.IO;

  8. namespace WordConvertPDF
  9. {
  10.     public static class WordToPDFHelper
  11.     {
  12.         /// <summary>
  13.         /// Word转换成PDF(单个文件转换推荐使用)
  14.         /// </summary>
  15.         /// <param name="inputPath">载入完整路径</param>
  16.         /// <param name="outputPath">保存完整路径</param>
  17.         /// <param name="startPage">初始页码(默认为第一页[0])</param>
  18.         /// <param name="endPage">结束页码(默认为最后一页)</param>
  19.         public static bool WordToPDF(string inputPath, string outputPath, int startPage = 0, int endPage = 0)
  20.         {
  21.             bool b = true;

  22.             #region 初始化
  23.             //初始化一个application
  24.             Application wordApplication = new Application();
  25.             //初始化一个document
  26.             Document wordDocument = null;
  27.             #endregion

  28.             #region 参数设置~~我去累死宝宝了~~(所谓的参数都是根据这个方法来的:ExportAsFixedFormat)
  29.             //word路径
  30.             object wordPath = Path.GetFullPath(inputPath);

  31.             //输出路径
  32.             string pdfPath = Path.GetFullPath(outputPath);

  33.             //导出格式为PDF
  34.             WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF;

  35.             //导出大文件
  36.             WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;

  37.             //导出整个文档
  38.             WdExportRange wdExportRange = WdExportRange.wdExportAllDocument;

  39.             //开始页码
  40.             int startIndex = startPage;

  41.             //结束页码
  42.             int endIndex = endPage;

  43.             //导出不带标记的文档(这个可以改)
  44.             WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent;

  45.             //包含word属性
  46.             bool includeDocProps = true;

  47.             //导出书签
  48.             WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;

  49.             //默认值
  50.             object paramMissing = Type.Missing;

  51.             #endregion

  52.             #region 转换
  53.             try
  54.             {
  55.                 //打开word
  56.                 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);
  57.                 //转换成指定格式
  58.                 if (wordDocument != null)
  59.                 {
  60.                     wordDocument.ExportAsFixedFormat(pdfPath, wdExportFormat, false, wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true, paramCreateBookmarks, true, true, false, ref paramMissing);
  61.                 }
  62.             }
  63.             catch (Exception ex)
  64.             {
  65.                 b = false;
  66.             }
  67.             finally
  68.             {
  69.                 //关闭
  70.                 if (wordDocument != null)
  71.                 {
  72.                     wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
  73.                     wordDocument = null;
  74.                 }

  75.                 //退出
  76.                 if (wordApplication != null)
  77.                 {
  78.                     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
  79.                     wordApplication = null;
  80.                 }
  81.             }

  82.             return b;
  83.             #endregion
  84.         }

  85.         /// <summary>
  86.         /// Word转换成PDF(批量文件转换推荐使用)
  87.         /// </summary>
  88.         /// <param name="inputPath">文件完整路径</param>
  89.         /// <param name="outputPath">保存路径</param>
  90.         public  static int WordsToPDFs(string[] inputPaths, string outputPath)
  91.         {
  92.             int count = 0;

  93.             #region 初始化
  94.             //初始化一个application
  95.             Application wordApplication = new Application();
  96.             //初始化一个document
  97.             Document wordDocument = null;
  98.             #endregion

  99.             //默认值
  100.             object paramMissing = Type.Missing;

  101.             for (int i = 0; i < inputPaths.Length; i++)
  102.             {
  103.                 #region 参数设置~~我去累死宝宝了~~(所谓的参数都是根据这个方法来的:ExportAsFixedFormat)
  104.                 //word路径
  105.                 object wordPath = Path.GetFullPath(inputPaths[i]);

  106.                 //获取文件名
  107.                 string outputName = Path.GetFileNameWithoutExtension(inputPaths[i]);

  108.                 //输出路径
  109.                 string pdfPath = Path.GetFullPath(outputPath + @"" + outputName + ".pdf");

  110.                 //导出格式为PDF
  111.                 WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF;

  112.                 //导出大文件
  113.                 WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;

  114.                 //导出整个文档
  115.                 WdExportRange wdExportRange = WdExportRange.wdExportAllDocument;

  116.                 //开始页码
  117.                 int startIndex = 0;

  118.                 //结束页码
  119.                 int endIndex = 0;

  120.                 //导出不带标记的文档(这个可以改)
  121.                 WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent;

  122.                 //包含word属性
  123.                 bool includeDocProps = true;

  124.                 //导出书签
  125.                 WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;                           

  126.                 #endregion
  127.                  
  128.                 #region 转换
  129.                 try
  130.                 {
  131.                     //打开word
  132.                     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);
  133.                     //转换成指定格式
  134.                     if (wordDocument != null)
  135.                     {
  136.                         wordDocument.ExportAsFixedFormat(pdfPath, wdExportFormat, false, wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true, paramCreateBookmarks, true, true, false, ref paramMissing);
  137.                     }
  138.                     count++;
  139.                 }
  140.                 catch (Exception ex)
  141.                 {
  142.                 }
  143.                 finally
  144.                 {
  145.                     //关闭
  146.                     if (wordDocument != null)
  147.                     {
  148.                         wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
  149.                         wordDocument = null;
  150.                     }
  151.                 }
  152.             }

  153.             //退出
  154.             if (wordApplication != null)
  155.             {
  156.                 wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
  157.                 wordApplication = null;
  158.             }
  159.             return count;
  160.                 #endregion
  161.         }

  162.         #region 其他
  163.         /// <summary>
  164.         /// Word转换成PDF(带日记)
  165.         /// </summary>
  166.         /// <param name="inputPath">载入完整路径</param>
  167.         /// <param name="outputPath">保存完整路径</param>
  168.         /// <param name="log">转换日记</param>
  169.         /// <param name="startPage">初始页码(默认为第一页[0])</param>
  170.         /// <param name="endPage">结束页码(默认为最后一页)</param>
  171.         public static void WordToPDFCreateLog(string inputPath, string outputPath, out string log, int startPage = 0, int endPage = 0)
  172.         {
  173.             log = "success";

  174.             #region 初始化
  175.             //初始化一个application
  176.             Application wordApplication = new Application();
  177.             //初始化一个document
  178.             Document wordDocument = null;
  179.             #endregion

  180.             #region 参数设置~~我去累死宝宝了~~
  181.             //word路径
  182.             object wordPath = Path.GetFullPath(inputPath);

  183.             //输出路径
  184.             string pdfPath = Path.GetFullPath(outputPath);

  185.             //导出格式为PDF
  186.             WdExportFormat wdExportFormat = WdExportFormat.wdExportFormatPDF;

  187.             //导出大文件
  188.             WdExportOptimizeFor wdExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;

  189.             //导出整个文档
  190.             WdExportRange wdExportRange = WdExportRange.wdExportAllDocument;

  191.             //开始页码
  192.             int startIndex = startPage;

  193.             //结束页码
  194.             int endIndex = endPage;

  195.             //导出不带标记的文档(这个可以改)
  196.             WdExportItem wdExportItem = WdExportItem.wdExportDocumentContent;

  197.             //包含word属性
  198.             bool includeDocProps = true;

  199.             //导出书签
  200.             WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;

  201.             //默认值
  202.             object paramMissing = Type.Missing;

  203.             #endregion

  204.             #region 转换
  205.             try
  206.             {
  207.                 //打开word
  208.                 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);
  209.                 //转换成指定格式
  210.                 if (wordDocument != null)
  211.                 {
  212.                     wordDocument.ExportAsFixedFormat(pdfPath, wdExportFormat, false, wdExportOptimizeFor, wdExportRange, startIndex, endIndex, wdExportItem, includeDocProps, true, paramCreateBookmarks, true, true, false, ref paramMissing);
  213.                 }
  214.             }
  215.             catch (Exception ex)
  216.             {
  217.                 if (ex != null) { log = ex.ToString(); }
  218.             }
  219.             finally
  220.             {
  221.                 //关闭
  222.                 if (wordDocument != null)
  223.                 {
  224.                     wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
  225.                     wordDocument = null;
  226.                 }

  227.                 //退出
  228.                 if (wordApplication != null)
  229.                 {
  230.                     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
  231.                     wordApplication = null;
  232.                 }
  233.                 GC.Collect();
  234.                 GC.WaitForPendingFinalizers();
  235.                 GC.Collect();
  236.                 GC.WaitForPendingFinalizers();
  237.             }
  238.             #endregion
  239.         }
  240.         #endregion
  241.     }
  242. }
复制代码




评分

参与人数 12HB +33 THX +7 收起 理由
operation + 1 + 1 [吾爱汇编论坛52HB.COM]-感谢楼主热心分享,小小评分不成敬意!
消逝的过去 + 2
创客者V2.0 + 1
agan8888 + 1
87052749 + 1 [快捷评语]--吃水不忘打井人,给个评分懂感恩!
小朋友 + 1 + 1 [快捷评语]--吃水不忘打井人,给个评分懂感恩!
freesilo + 1 吃水不忘打井人,给个评分懂感恩!
2573668719 + 2 + 1 欢迎关注学破解论坛微信公众帐号,微信搜索:xuepojie
Shark恒 + 5 + 1 欢迎关注学破解论坛微信公众帐号,微信搜索:xuepojie
风动鸣 + 3 + 1 评分=感恩!简单却充满爱!感谢您的作品!
Aniz + 5 + 1 欢迎关注学破解论坛微信公众帐号,微信搜索:xuepojie
小七烤地瓜 + 10 + 1 欢迎关注学破解论坛微信公众帐号,微信搜索:xuepojie

查看全部评分

吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
血色 发表于 2015-12-14 15:09 | 显示全部楼层
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
gujin162 发表于 2015-12-14 15:33 | 显示全部楼层


感谢楼主分享,非常感谢~~
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
风动鸣 发表于 2015-12-14 16:11 | 显示全部楼层
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
Shark恒 发表于 2015-12-14 17:12 | 显示全部楼层

WIN7X64,点击“选择文件”,就无响应弹错。
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
 楼主| 毒逆天 发表于 2015-12-15 20:32 | 显示全部楼层

Shark恒 发表于 2015-12-14 17:12
WIN7X64,点击“选择文件”,就无响应弹错。

恒大大好~ 我再看看~我win10 64 没问题
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
freesilo 发表于 2018-8-8 22:54 | 显示全部楼层

最近太忙了,先收藏下,有空再试试,谢谢分享。
吾爱汇编论坛-学破解,防破解!知进攻,懂防守!逆向分析,软件安全!52HB.COM
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

警告:本站严惩灌水回复,尊重自己从尊重他人开始!

1层
2层
3层
4层
5层
6层
7层

免责声明

吾爱汇编(www.52hb.com)所讨论的技术及相关工具仅限用于研究学习,皆在提高软件产品的安全性,严禁用于不良动机。任何个人、团体、组织不得将其用于非法目的,否则,一切后果自行承担。吾爱汇编不承担任何因为技术滥用所产生的连带责任。吾爱汇编内容源于网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除。如有侵权请邮件或微信与我们联系处理。

站长邮箱:SharkHeng@sina.com
站长QQ:1140549900


QQ|RSS|手机版|小黑屋|帮助|吾爱汇编 ( 京公网安备11011502005403号 , 京ICP备20003498号-6 )|网站地图

Powered by Discuz!

吾爱汇编 www.52hb.com

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