using System;
using Microsoft.Office.Core; namespace Office
{
class Util
{
private Util() { }
/// <summary>
/// 把Word文件转换成为PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool WordToPDF(string sourcePath, string targetPath)
{
bool result = false;
Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
Microsoft.Office.Interop.Word.ApplicationClass application = null; Microsoft.Office.Interop.Word.Document document = null;
try
{
application = new Microsoft.Office.Interop.Word.ApplicationClass();
application.Visible = false;
document = application.Documents.Open(sourcePath);
document.SaveAs2();
document.ExportAsFixedFormat(targetPath, exportFormat);
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
if (document != null)
{
document.Close();
document = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
} /// <summary>
/// 把Microsoft.Office.Interop.Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool ExcelToPDF(string sourcePath, string targetPath)
{
bool result = false;
Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.ApplicationClass application = null;
Microsoft.Office.Interop.Excel.Workbook workBook = null;
try
{
application = new Microsoft.Office.Interop.Excel.ApplicationClass();
application.Visible = false;
workBook = application.Workbooks.Open(sourcePath);
workBook.SaveAs();
workBook.ExportAsFixedFormat(targetType, targetPath);
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
if (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
/// <summary>
/// 把PowerPoint文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool PowerPointToPDF(string sourcePath, string targetPath)
{
bool result;
Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
try
{
application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
//application.Visible = MsoTriState.msoFalse;
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue); result = true;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
} /// <summary>
/// 把Visio文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool VisioToPDF(string sourcePath, string targetPath)
{
bool result;
Microsoft.Office.Interop.Visio.VisFixedFormatTypes targetType = Microsoft.Office.Interop.Visio.VisFixedFormatTypes.visFixedFormatPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.Visio.ApplicationClass application = null;
Microsoft.Office.Interop.Visio.Document document = null;
try
{
application = new Microsoft.Office.Interop.Visio.ApplicationClass();
application.Visible = false;
document = application.Documents.Open(sourcePath);
document.Save();
document.ExportAsFixedFormat(targetType, targetPath, Microsoft.Office.Interop.Visio.VisDocExIntent.visDocExIntentScreen, Microsoft.Office.Interop.Visio.VisPrintOutRange.visPrintAll);
result = true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
result = false;
}
finally
{
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}

C#实现MS-Office文档转Pdf(Word、Execel、PowerPoint、Visio、Project)的更多相关文章

  1. java读取pdf和MS Office文档

    有时候PDF中的文字无法复制,这可能是因为PDF文件加密了,不过使用PDFBox开源软件就可以把它读出来. 还有一个用于创建PDF文件的项目----iText. PDFBox下面有两个子项目:Font ...

  2. Java实现web在线预览office文档与pdf文档实例

    https://yq.aliyun.com/ziliao/1768?spm=5176.8246799.blogcont.24.1PxYoX 摘要: 本文讲的是Java实现web在线预览office文档 ...

  3. Java实现office文档与pdf文档的在线预览功能

    最近项目有个需求要java实现office文档与pdf文档的在线预览功能,刚刚接到的时候就觉得有点难,以自己的水平难以在三四天做完.压力略大.后面查找百度资料.以及在同事与网友的帮助下,四天多把它做完 ...

  4. office文档转pdf

    这里贴下代码吧,没啥好说的. using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  5. 转:C#实现office文档转换为PDF或xps的一些方法

    代码支持任意office格式 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 [url]http://www.microsoft ...

  6. Java中几种office文档转pdf的方式

    最近公司要做office的文档,搜集了几种office文档转pdf的方式,简单的做下总结 我主要尝试了三种方式:openoffice,aspose,jacob 对他们进行了大文件,小文件,在linux ...

  7. 在禅道中实现WORD等OFFICE文档转换为PDF进行在线浏览

    条件: 安装好禅道的服务器 能直接浏览PDF的浏览器(或通过 安装插件实现 ) 文档转换服务程序(建议部署在另一台服务器上)     实现 原理: 修改禅道的文件预览功能(OFFICE文档其使用的是下 ...

  8. office 文档转pdf

    本地先安装 金山wps,并确保可用 工程目录 1.使用前,先执行install.bat 安装jacob 到maven本地仓库 2.复制 jacob-1.18-M2-x64.dlljacob-1.18- ...

  9. C#实现office文档转换为PDF或xps的一些方法( 转)

    源博客http://blog.csdn.net/kable999/article/details/4786654 代码支持任意office格式 需要安装office 2007 还有一个office20 ...

  10. C#实现office文档转换为PDF格式

    1.安装组件OfficeSaveAsPDFandXPS 需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址   OfficeSave ...

随机推荐

  1. SQL Server 自定义聚合函数

    说明:本文依据网络转载整理而成,因为时间关系,其中原理暂时并未深入研究,只是整理备份留个记录而已. 目标:在SQL Server中自定义聚合函数,在Group BY语句中 ,不是单纯的SUM和MAX等 ...

  2. 二分查找-python

    约12年年底的时候,接触了python不到半年的样子,入门是直接实现GUI测试case的.今天面试地平线机器人,发现忘得差不多了- -. 当时的问题是这样的 写一个二分查找是实现,我好像不记得二分查找 ...

  3. awk实现join

    awk 有时确实比较省事,做些简单的文本处理,还是很方便的,在这介绍下两个文件的join的操作. 原始文本 bb.txt a 10 b 12 cc.txt a 11 b 13 c 15 awk joi ...

  4. 关于spring AOP的学习

    比较好的帖子http://www.cnblogs.com/xing901022/p/4265544.html

  5. JSON与XML优缺点对比分析

    本文从各个方面向大家对比展示了json和xml的优缺点,十分的全面细致,有需要的小伙伴可以参考下. 1. 定义介绍 1.1 XML定义 扩展标记语言 (Extensible Markup Langua ...

  6. DELPHI 读取csv 格式文本文件

    procedure TForm1.btn1Click(Sender: TObject); var sFile,sLine: TStrings; r,c:Integer; begin sFile := ...

  7. 已解决: 已引发: "无法加载 DLL“opencv_core2410”: 找不到指定的模块。

    之前是win7 32位系统,程序运行没有问题:换了64位 win7后,系统就找不到opencv相关库了,网上各种查询解决方法,感觉不太可行,或者很麻烦...多次试验后,找到方便关键的解决方案如下: 计 ...

  8. --查询nvarchar(max)的表和字段

    --查询nvarchar(max)的表和字段 select 'insert into #tempTabelInfo select '''+d.name+''', '''+a.name+''', max ...

  9. 计算机病毒实践汇总六:IDA Pro基础

    在尝试学习分析的过程中,判断结论不一定准确,只是一些我自己的思考和探索.敬请批评指正! 1. IDA使用 (1)搜索.下载并执行IDA Pro,对可执行程序lab05-01.dll进行装载,分别以图形 ...

  10. 如何在HTML中加载Flash(2种实现方法)_HTML/Xhtml_网页制作

    点评:如何在HTML中加载Flash,为网页添加更多的色彩,普通的网页以无法满足用户的需求,接下来为大家介绍下2种在HTML中加载Flash的方法,感兴趣的各位可以适当参考下,希望对你有所帮助 第一种 ...