需要引用Microsoft.Office.Interop.Word,版本是07之上的。

这个版本会判断文件是否被占用。

using Microsoft.Office.Interop.Word;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Runtime.InteropServices;
namespace SavePdf
{
class Program
{
private static int NONE_EXIST = -1;
private static int OCCUPY = 1;
private static int NONE_OCCUPY = 0;
[DllImport("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite); [DllImport("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject); public const int OF_READWRITE = 2;
public const int OF_SHARE_DENY_NONE = 0x40;
public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
static void Main(string[] args)
{
Program sp = new Program();
foreach (var arg in args)
{
Console.WriteLine(arg);
}
if (args.Length == 2)
{
if (sp.IsOCcupy(args[0]) == NONE_OCCUPY)
{
DateTime dt = DateTime.Now; sp.SavePdf(args[0], args[1]);
TimeSpan ts = DateTime.Now.Subtract(dt);
Console.WriteLine(ts.ToString() + "ms");
}
} }
public int IsOCcupy(String vFileName)
{
if (!File.Exists(vFileName))
{ return NONE_EXIST;
}
IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
if (vHandle == HFILE_ERROR)
{ return OCCUPY;
}
CloseHandle(vHandle);
return NONE_OCCUPY;
} private void SavePdf(String filePath, String outFile)
{
object oMissing = System.Reflection.Missing.Value;
_Application wordApp;
_Document wordDoc;
if (File.Exists(filePath) == false)
{
return;
}
FileInfo fi = new FileInfo(filePath);
object outputFileName = Path.Combine(fi.Directory.FullName, outFile); object fileFormat = WdSaveFormat.wdFormatPDF;
wordApp = new Microsoft.Office.Interop.Word.Application();
wordApp.Visible = false;
object file = (object)filePath;
wordDoc = wordApp.Documents.Open(ref file, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
wordDoc.SaveAs(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
try
{
if (wordDoc != null)
{
wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
}
if (wordApp != null)
{
wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
}
}
catch (Exception ex)
{
throw ex;
}
return;
}
}
}

#c word转换PDF的更多相关文章

  1. SharePoint 2013 Word 转换PDF服务介绍及示例

    前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...

  2. SharePoint Word 转换PDF服务介绍及示例

    前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...

  3. openoffice excel word 转换pdf 支持本地调用和远程调用

    OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows.Linux.MacOS X (X11)和 Solaris 等操作系统上执行.它与各个主要的办公室软件套件兼容.OpenO ...

  4. .net调用word转换pdf出现80080005错误的解决办法

    检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80080005. 1:在服务器上安装offi ...

  5. tp5 使用phpword 替换word模板并利用com组件转换pdf

    tp5   使用phpword 替换word模板并利用com组件转换pdf 一.首先composer安装PHPword,就不多说了 二.然后是把模板中要替换的部分用变量代替 三.把原始的模板文件放入项 ...

  6. word转PDF,PDF转Image,使用oppenOffice注意事项等

    最近在电子合同等项目中需要把word或者pdf转换成image,用到了openOffice把word转换pdf,以及把pdf转换成图片 感谢小伙伴张国清花费了三天时间来实现了此功能.下面我将把具体的步 ...

  7. 在windows系统上word转pdf

    一.前言:我在做文件转换过程中遇到的一些坑,在这里记录下,因为项目需求,需要使用html转pdf,由于itext转换质量问题(一些Css属性不起作用),导致只能通过word文件作为跳板来转换到pdf文 ...

  8. openOffice word转pdf,pdf转图片优化版

    之前写了一个版本的,不过代码繁琐而且不好用,效率有些问题.尤其pdf转图片速度太慢.下面是优化版本的代码. spriing_boot 版本信息:2.0.1.RELEASE 1.配置信息: packag ...

  9. Linux系统下word转pdf,xls转pdf,ppt转pdf

    word转换pdf的技术方案,供参考.[doc/docx/ppt/pptx/xls/xlsx均支持转换]           本方案是Java结合shell命令完成,不同于以往的仅依赖java组件转换 ...

随机推荐

  1. When you’re nearly 40 and unmarried, and you realize you’re going to be okay

    https://medium.com/the-gathering-kind/when-you-re-nearly-40-and-unmarried-and-you-realize-you-re-goi ...

  2. Android Studio Gradle

    http://blog.zhaiyifan.cn/2016/03/14/android-new-project-from-0-p2/

  3. eclipse教程

    http://www.eclipse.org/downloads/eclipse-packages/http://wiki.eclipse.org/Eclipse_Articles,_Tutorial ...

  4. span元素定义宽高度

    由于span是行内元素,不可能有高度和宽度的,在span标签里添加内容,可以撑出来宽高,想要定义宽高必须转话成块级元素. span{ display:block; } 或者使用 span{ displ ...

  5. [swustoj 373] Antiprime数

    Antiprime数(0373) 问题描述 如果一个自然数n(n>=1),满足所有小于n的自然数(>=1)的约数个数都小于n的约数个数,则n是一个Antiprime数.譬如:1, 2, 4 ...

  6. js学习总结

    转自 http://blog.sina.com.cn/s/blog_75cf5f3201011csu.html 一: 关于基本数据类型在栈内存和堆内存中的关系 基本数据对于栈内存和堆内存是可以复制的, ...

  7. 我是红领巾,分享2014 google不能用的方法。

    那啥已经20天打不开了. 得爬qiang. 今天无意间发现一个好东东. 特记录一下.    360浏览器设置 1.   工具菜单==>选项==>高级设置==>管理搜索引擎 . 2. ...

  8. HDU 2544 最短路 (最短路,spfa)

    题意:中文题目 思路:spfa+SLF优化.关于SPFA的详情请戳我 #include <bits/stdc++.h> using namespace std; , INF=0x7f7f7 ...

  9. Scrum&Kanban在移动开发团队的实践 (一)

    现在大多数团队都在谈敏捷开发,似乎觉得敏捷是软件开发的银弹.只需要实践下一些敏捷开发的模式就能如何如何,其实我觉得不论是敏捷开发还是传统的瀑布流开发都是有他们的市场的,取决于团队人员构成,取决你的产品 ...

  10. 五分钟solr4.5教程(搭建、运行)

    环境要求 jdk1.6及以上版本 solr发布版本 下载地址 http://lucene.apache.org/solr/mirrors-solr-latest-redir.html? 启动solr ...