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

转载于:https://www.cnblogs.com/wardensky/p/4269286.html

#c word转换PDF相关推荐

  1. Linux系统Word转换PDF,文档字体乱码不显示问题解决。

    1.问题 在Windows系统中,使用Java将Word文档转换为PDF格式时,结果文档内容正常:但是在Linux系统中,转出来PDF文档出现乱码或###符号等. 2.问题原因 这个问题是由于Linu ...

  2. 关于poi操作word,word转换pdf预览,这边文章就够了

    最近公司一个项目中需要导出一个简历word版,对于实习生的我开始是无从下手的,后来通过搜索发现可以使用poi来操作. 话不多说,先引入依赖 <dependency><groupId& ...

  3. Java WORD转换PDF 并添加水印 (附赠jar提取链接)

    Java WORD转换PDF 并添加水印 直接上代码 实现功能 docx文档转换为PDF 转换之后排版不混乱 使用工具(Jar包) aspose-words-15.8.0-jdk16.jar(用于PD ...

  4. Java使用aopse实现word转换pdf

    Java使用aopse实现word转换pdf 需要引用aspose包,引入操作我写了一个博客,地址如下 https://blog.csdn.net/weixin_46713508/article/de ...

  5. 解决 linux(centos7)word转换pdf时,pdf乱码问题

    本文解决当word转换为pdf时,pdf文档中的中文字体全部都是乱码(方框)的问题. 先简单的写一下word转换pdf的代码,这个网上有很多(最下方有引用一遍博客详细的讲解了如何转换) /**** @ ...

  6. Java使用Spire.Pdf或Aspose-Words实现Word转换Pdf在Linux服务器上的中文乱码问题

    一.问题产生的背景 当Word文档中含有中文字符时,不管是使用Spire.Pdf或Aspose-Words实现Word转换Pdf,代码的执行都需要调用操作系统的本地字体库支持,否则所有中文字符都将乱码 ...

  7. JAVA 使用 com.aspose.words将word转换PDF等

    因为公司前端需要在线查看word和PDF,后台上传需求将word等文件转换为PDF,原本使用的是liboffice进行转换,后来部署到服务器端之后,发现并不是很适合,由此找到com.aspose.wo ...

  8. Linux服务器(centos7)中Word转换PDF,文档出现中文乱码或方格【亲测可用,已解决】

    提示:在centos服务器使用aspose.word转换word文件为pdf的时候只有中文乱码或则方格,但是在win服务器上使用可以正常转换.本次文章主要解决字体缺失问题 目录 前言 一.在linux ...

  9. word转换pdf方法分享!word转换pdf软件合集。​

    word转换pdf方法分享!众所周知,将 Word 文档转换成 PDF 后,可以确保文档内容及格式在不同的设备.操作系统和软件中都能够保持一致,避免因为软件版本或者操作系统的差异而导致文档出现错乱等问 ...

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

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

最新文章

  1. Bzoj4558 [JLoi2016]方
  2. 网传的 Spring 大漏洞
  3. POJ 1185 炮兵阵地
  4. 计算机应用基础 access,计算机应用基础第7章数据库应用基础Access
  5. 虽然自己不是唱歌的材料
  6. http与https的作用与区别
  7. SQL Server Reporting Services最佳做法
  8. c语言 指针 地址溢出,堆栈溢出,指针在C中
  9. SVN同步分支代码到主干
  10. 笔记本玩游戏掉帧怎么办。
  11. 从一个骗人的技术说起光线追踪——光影技术的里程碑
  12. MTSP遗传算法解决
  13. 把时间当作朋友 读书笔记
  14. 2022推荐大宗商品行业研究报告投资策略产业发展前景市场行情分析(附件中为网盘链接,报告持续更新)
  15. Python 对称矩阵顺时针旋转90度
  16. 我从《阿里云:人工智能应用实践与趋势》白皮书学到了什么?
  17. Android图文识别
  18. 20230308-二维数组的长度
  19. The Frog Prince (青蛙王子) and Hello Kitty (凯蒂猫)
  20. OpenGL硬件加速判断

热门文章

  1. 死磕 java集合之终结篇
  2. 微信公众号直播的玩法须知(下)
  3. 使用QTP11.0的过程中,常见的部分问题解决方法(持续更新...)
  4. python 微信机器人自动聊天+回怼表情包+Adidas公众号自动抽签 四、获取表情包中的文字并进行同类型回怼
  5. 深度学习笔记(十)—— 梯度检验[Gradient Checks]
  6. 一分钟带你了解GMS安卓市场行业的发展
  7. 基于逻辑回归,支持向量机,朴素贝叶斯以及简单深度学习文本分类方法(BiLSTM、CNN)实现的中文情感分析,含数据集可直接运行
  8. python wmi安装_Python wmi 模块的学习
  9. opencv-python 小白笔记(7)
  10. 手把手教学构建农业知识图谱:农业领域的信息检索+智能问答,命名实体识别,关系抽取,实体关系查询