【源码】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 ...
随机推荐
- 【原】iOS学习之苹果原生代码实现Autolayout和VFL语言
1.添加约束的规则 在创建约束之后,需要将其添加到作用的view上 在添加时要注意目标view需要遵循以下规则: 1)对于 两个同层级view之间 的约束关系,添加到它们的父view上 2)对于 两个 ...
- HTML5 与 CSS3 jQuery部分知识总结
一. HTML5 为什么需要HTML5 什么是HTML5 HTML5现状及浏览器支持 HTML5优点与缺点 HTML5语法规则与文档声明 HTML5新增表达标签 HTML5多媒体组件 HTML5 ...
- [知识点]字符串Hash
1.前言 字符串的几大主要算法都多少提及过,现在来讲讲一个称不上什么算法, 但是非常常用的东西——字符串Hash. 2.Hash的概念 Hash更详细的概念不多说了,它的作用在于能够对复杂的状态进行简 ...
- iOS特性一 关闭系统日志打印
解决办法 (1)Product -->Scheme -->Edit Scheme -->Run -->Arguments (2)添加一个属性值OS_ACTIVITY_MODE: ...
- 解析文件+AcitonBar展示:
//项目效果:
- [ios]利用alertView 插入数据都数据库。笔记
利用alertView 插入数据都数据库 -(void)addItemToList { UIAlertView *alter=[[UIAlertViewalloc]initWithTitle:@&qu ...
- pythonchallenge 解谜 Level 8
#-*- coding:utf-8 -*- #代码版本均为python 3.5.1 #Level 7 import bz2 un=b'BZh91AY&SYA\xaf\x82\r\x00\x00 ...
- BOM对象有哪些:
BOM对象有哪些: 1.window对象 ,是JS的最顶层对象,其他的BOM对象都是window对象的属性: 2.document对象,文档对象: 3.location对象,浏览器当前URL信息: 4 ...
- 验证mongodb主从复制过程~记录操作
接 mongodb的安装:http://www.cnblogs.com/myrunning/p/4319367.html 1.1创建数据目录 在这里我们将不使用mongodb的配置文件启动mongod ...
- IOS ASI http 框架详解
本文转自:http://my.oschina.net/sunqichao/blog/75011 ASIHTTPRequest对CFNetwork API进行了封装,并且使用起来非常简单,用Object ...