#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组件转换 ...
随机推荐
- Android相对布局(RelativeLayout)
Android相对布局(RelativeLayout) 备注:这里的视图和元素是等同的概念. RelativeLayout是一个允许子视图相对于其他兄弟视图或是父视图显示的视图组(通过ID指定).每个 ...
- firebug的使用方法和技巧(web开发调试工具)
Firebug是firefox下的一个插件,能够调试所有网站语言,如Html,Css等,但FireBug最吸引我的就是javascript调试功 能,使用起来非常方便,而且在各种浏览器下都能使用(IE ...
- Java中静态代码块,代码块,构造方法优先级、区别及代码示例
在项目中遇到了代码块的知识点,跑了下测试,写下结论 代码优先级:静态代码块 -> 构造代码块 -> 构造方法 多个代码块优先级,按照“先定义的代码先执行,后定义的代码后执行”原则执行 静态 ...
- Android开发之消息机制
转:http://stackvoid.com/introduction-to-Message-Handler-in-Android/ http://blog.dreamtobe.cn/2016/03/ ...
- servlet基础讲解
基本知识一.Web结构1.两种应用程序 ①桌面应用程序:QQ.CS.MyEclipse.Office.DW.360.浏览器等必须下载.安装.桌面快捷方式.注册表信息.操作系统后台服务.占用操作系统端口 ...
- Zxing 扫二维码
1 http://blog.csdn.net/xiaanming/article/details/10163203 2 我会把一个可以运行的Demo云盘:http://pan.baidu.com/s/ ...
- hdu 4635 Strongly connected(强连通)
考强连通缩点,算模板题吧,比赛的时候又想多了,大概是不自信吧,才开始认真搞图论,把题目想复杂了. 题意就是给你任意图,保证是simple directed graph,问最多加多少条边能使图仍然是si ...
- apache开源项目 -- VXQuery
Apache VXQuery 是一个兼容标准的 XML 查询处理器的实现.主要适合非常大量的 XML 数据处理. 参考: http://www.apache.org/
- distinct数据去重关键字
在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 示例1 select distinct nam ...
- Android-监听sdcard状态
public class MyService extends Service { private static final String TAG = "MyService"; Fi ...