【源码】Word转PDF V1.0.1 小软件,供新手参考
昨天有一朋友让我帮忙找一款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
{
/// <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
}
}
【源码】Word转PDF V1.0.1 小软件,供新手参考的更多相关文章
- 保姆级教程——Ubuntu16.04 Server下深度学习环境搭建:安装CUDA8.0,cuDNN6.0,Bazel0.5.4,源码编译安装TensorFlow1.4.0(GPU版)
写在前面 本文叙述了在Ubuntu16.04 Server下安装CUDA8.0,cuDNN6.0以及源码编译安装TensorFlow1.4.0(GPU版)的亲身经历,包括遇到的问题及解决办法,也有一些 ...
- [笔记] Ubuntu 18.04源码编译安装OpenCV 4.0流程
标准常规安装方法安装的OpenCV版本比较低,想尝鲜使用4.0版本,只好源码安装. 安装环境 OS:Ubuntu 18.04 64 bit 显卡:NVidia GTX 1080 CUDA:10.0 c ...
- Android音乐播放器源码(歌词.均衡器.收藏.qq5.0菜单.通知)
一款Android音乐播放器源码,基本功能都实现了 qq5.0菜单(歌词.均衡器.收藏.qq5.0菜单.通知) 只有向右滑动出现,菜单键和指定按钮都还没有添加. 源码下载:http://code.66 ...
- springmvc工作原理以及源码分析(基于spring3.1.0)
springmvc是一个基于spring的web框架.本篇文章对它的工作原理以及源码进行深入分析. 一.springmvc请求处理流程 二.springmvc的工作机制 三.springmvc核心源码 ...
- CentOS 6.4 64位 源码编译hadoop 2.2.0
搭建环境:Centos 6.4 64bit 1.安装JDK 参考这里2.安装mavenmaven官方下载地址,可以选择源码编码安装,这里就直接下载编译好的wget http://mirror.bit. ...
- 源码编译安装 PHP5.5.0,解决curl_exec访问HTTPS返回502错误的问题(修改PATH路径)
最近碰到一个奇怪的问题, PHP使用 curl_exec 访问 HTTPS 网页时, 返回502错误, 访问HTTP网页时没有问题, 用 echo phpinfo() ; 查看, 支持op ...
- Android -- 从源码带你从EventBus2.0飚到EventBus3.0(一)
1,最近看了不少的面试题,不管是百度.网易.阿里的面试题,都会问到EventBus源码和RxJava源码,而自己只是在项目中使用过,却没有去用心的了解它底层是怎么实现的,所以今天就和大家一起来学习学习 ...
- 源码阅读之mongoengine(0)
最近工作上用到了mongodb,之前只是草草了解了一下.对于NoSQL的了解也不是太多.所以想趁机多学习一下. 工作的项目直接用了pymongo来操作直接操作mongodb.对于用惯了Djongo O ...
- 【 js 基础 】【 源码学习 】 setTimeout(fn, 0) 的作用
在 zepto 源码中,$.fn 对象 有个 ready 函数,其中有这样一句 setTimeout(fn,0); $.fn = { ready: function(callback){ // don ...
随机推荐
- [BZOJ4196][NOI2015]软件包管理器
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1040 Solved: 603[Submit][Stat ...
- xpath tutorial
http://www.cnblogs.com/yukaizhao/archive/2011/07/25/xpath.html http://www.w3schools.com/xpath/defaul ...
- WPF-开机自启
#region 开机自启 /// <summary> /// 开机自启创建 /// </summary> /// <param name="exeName&qu ...
- weex逻辑控制
在WEEX中,有if 和 repeat 两种逻辑运算,需要注意的是,逻辑控制不能够作用于<template>这样的根节点. if 控制判断条件true/false直接对节点进行操作,if= ...
- 如何去掉dede列表推荐时标题被加粗
dede在列表推荐文章默认为加粗不清楚的可以看图: 那个加黑的是默认的.如果你不想要被加黑,可以做如下改动.在include里找到文件:arc.listview.class.php查找并删除(注释掉也 ...
- SpringMVC4 实例
之前我先创建普通web项目,再直接添加maven框架.结果springMVC框架一直不能正确的访问控制器. 但通过以下方式创建maven web项目就能获得正确的架构路径. 1.新建一个maven项目 ...
- 关于mysql ERROR 1045 (28000)错误的解决办法
错误情景: 使用Navicat打开mysql的时候弹出错误框 错误代码: ERROR 1045 (28000): Access denied for user 'ODBC'@'localhost' ( ...
- Html5选择本地视频音频文件播放
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 使用Spring JdbcTemplate实现数据库操作
今天我来演示 关于JDBCTemplate实现对数据库的查询和添加 首先是添加 第一步大家都知道 创建一个实体类 然后写一个方法 把实体类当参数传进去 在实现这个接口 JdbcDaoSupport这个 ...
- SQL Server中CROSS APPLY和OUTER APPLY的应用详解
SQL Server数据库操作中,在2005以上的版本新增加了一个APPLY表运算符的功能.新增的APPLY表运算符把右表表达式应用到左表表达式中的每一行.它不像JOIN那样先计算那个表表达式都可以, ...