#c word转换PDF
需要引用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的更多相关文章
- SharePoint 2013 Word 转换PDF服务介绍及示例
前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...
- SharePoint Word 转换PDF服务介绍及示例
前言:在SharePoint使用过程中,经常会发现将文档进行格式转换的需求,之前,看到SharePoint 2013有将PPT转换PDF文档的服务,后来,才发现SharePoint 2010开始,就有 ...
- openoffice excel word 转换pdf 支持本地调用和远程调用
OpenOffice.org 是一套跨平台的办公室软件套件,能在Windows.Linux.MacOS X (X11)和 Solaris 等操作系统上执行.它与各个主要的办公室软件套件兼容.OpenO ...
- .net调用word转换pdf出现80080005错误的解决办法
检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80080005. 1:在服务器上安装offi ...
- tp5 使用phpword 替换word模板并利用com组件转换pdf
tp5 使用phpword 替换word模板并利用com组件转换pdf 一.首先composer安装PHPword,就不多说了 二.然后是把模板中要替换的部分用变量代替 三.把原始的模板文件放入项 ...
- word转PDF,PDF转Image,使用oppenOffice注意事项等
最近在电子合同等项目中需要把word或者pdf转换成image,用到了openOffice把word转换pdf,以及把pdf转换成图片 感谢小伙伴张国清花费了三天时间来实现了此功能.下面我将把具体的步 ...
- 在windows系统上word转pdf
一.前言:我在做文件转换过程中遇到的一些坑,在这里记录下,因为项目需求,需要使用html转pdf,由于itext转换质量问题(一些Css属性不起作用),导致只能通过word文件作为跳板来转换到pdf文 ...
- openOffice word转pdf,pdf转图片优化版
之前写了一个版本的,不过代码繁琐而且不好用,效率有些问题.尤其pdf转图片速度太慢.下面是优化版本的代码. spriing_boot 版本信息:2.0.1.RELEASE 1.配置信息: packag ...
- Linux系统下word转pdf,xls转pdf,ppt转pdf
word转换pdf的技术方案,供参考.[doc/docx/ppt/pptx/xls/xlsx均支持转换] 本方案是Java结合shell命令完成,不同于以往的仅依赖java组件转换 ...
随机推荐
- 1742. Team building(dfs)
1742 最小的是找联通块数 最大的找环 一个环算一个 其它的数各算一个 #include <iostream> #include<cstdio> #include<cs ...
- 11月下旬poj其他题
poj1000,poj1003,poj1004,poj1064,poj1218 水题 poj1012:0<k<14——漂亮的打表 poj1651:与能量项链很像的dp poj1159:回文 ...
- [转]Jquery Ajax用法
原文地址:http://www.php100.com/html/program/jquery/2013/0905/6004.html jQuery学习之jQuery Ajax用法详解 来源: 时间 ...
- NSIS 2.0界面快速入门
NSIS 2.0 版本支持定制的用户界面.所谓的 Modern UI(下称 MUI) 就是一种模仿最新的 Windows 界面风格的界面系统.MUI 改变了 NSIS 脚本的编写习惯,它使用 NSIS ...
- POJ 3352 Road Construction(边双连通分量,桥,tarjan)
题解转自http://blog.csdn.net/lyy289065406/article/details/6762370 文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业 ...
- shell 中grep命令详解
用‘grep’搜索文本文件如果您要在几个文本文件中查找一字符串,可以使用‘grep’命令.‘grep’在文本中搜索指定的字符串.举个例子:假设您正在‘/usr/src/linux/Documentat ...
- 专题:Windows编译x264、SDL、faac、ffmpeg过程
Windows上编译ffmpeg完整过程,包括编译x264.SDL.faac.在Windows上编译ffmpeg需要用MinGW+msys,本专题用于记录编译过程中遇到的各种问题及解决方法,转载请注明 ...
- ffmpeg windows 雪花声解决方法
替换所有文件里的<math.h>为<mathimf.h>即可. 我用ffmpeg-0.6.3版测试时,好像mathimf.h文件和其他文件有冲突,需要修改源码. 和qdm2.c ...
- 【Unity3D】枪战游戏—发射子弹、射线检测
一.子弹的碰撞检测: 因为子弹的移动速度非常的快,那么如果为子弹添加一个collider,就有可能检测不到了. 因为collider是每一帧在执行,第一帧子弹可能在100米处,那么下一帧就在900米处 ...
- 【转】No JVM could be found on your system解决方法
原文网址:http://my.oschina.net/liusicong/blog/324964 在安装android studio时,报错: Error launching android Stud ...