需要引用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;
}
}
}

最新文章

  1. thrift笔记
  2. Android Binder
  3. HDU 1166敌兵布阵+NOJv2 1025: Hkhv love spent money(线段树单点更新区间查询)
  4. 简单插入排序(C++版)
  5. 谈谈 Repository、IUnitOfWork 和 IDbContext 的实践
  6. Week1 Team Homework #2: Introduction of each team member
  7. [转] shared_from_this 几个值得注意的地方
  8. JS浏览器对象-Screen对象
  9. mac下显示隐藏文件
  10. boost::bind的使用方法
  11. ecshop加入购物车效果(各个页面)
  12. 人工智能(AI)库TensorFlow 踩坑日记之二
  13. iOS 数据加密方案
  14. 【LSGDOJ 1408】邮局
  15. Android 推送和统计最优轮循(心跳策略)探究实践
  16. python类与对象-如何创建可管理的对象属性
  17. 高效的 JavaScript
  18. JDBC 接口学习
  19. iOS Dev (25) 解决“The executable was signed with invalid entitlements.”问题
  20. 2013长春网赛1001 hdu 4759 Poker Shuffle

热门文章

  1. Android开发之TextView实现跑马灯效果
  2. Android开发之XML的创建和解析
  3. Android 获取屏幕尺寸与密度
  4. HDU 4870 Rating (2014 Multi-University Training Contest 1)
  5. 用socket方式传输Image和Sound文件
  6. VS2010下编译安装DarwinStreamingServer5.5.5
  7. db2数据库sql报错信息
  8. winform实现自动更新并动态调用form实现
  9. android学习笔记六
  10. golang windows程序获取管理员权限(UAC ) via gocn