前面两篇博客讲解了怎么通过程序控制word的生成,包括生成文字、添加表格、添加图片等。这篇博客主要说一下怎么把word图片转换成pdf。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; using Microsoft.Office.Core; namespace PDFTest
{
class PDFUtil
{
/// <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;
//} }
}

程序中包含了各种office格式转换pdf的代码,在使用之前,一定要加载对应的COM库进来,然后才可以使用。

C#操作Office.word(三)的更多相关文章

  1. C#操作Office.word(二)

    在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...

  2. C#操作Office.word(一)

    该文章主要是讲述如何使用VS2010创建word文档,因为在项目中我们可能需要点击一个按钮把数据库中的项目表单或图片显示到word文档中,因此该文章主要分析如何使用VS2010创建word文档并填写相 ...

  3. 使用ABAP编程实现对微软Office Word文档的操作

    SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...

  4. java 使用 POI 操作 XWPFDocumen 创建和读取 Office Word 文档基础篇

    注:有不正确的地方还望大神能够指出,抱拳了 老铁! 参考 API:http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDoc ...

  5. 新建 Microsoft Office Word 文档 来源:牛客网

    题目 链接:https://ac.nowcoder.com/acm/contest/28886/1015 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...

  6. 打开office word excel弹出visual studio 2008

    打开office word 或者excel或者ppt等文档就会弹出visual studio 2008正在配置对话框. 按照我下面的步骤,解决方法非常简单. 如果你的电脑是win7操作系统,从第一步开 ...

  7. 【Office Word】论文排版有关技巧

    本文分两部分,第一部分呢是Word中标题的编号以及图表的编号:第二部分是MathType中公式编号的右对齐方法.   1. word中标题的编号以及图表的编号   本部分转载自:http://blog ...

  8. Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结

    Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求 ...

  9. 关于 .net 开发 Office Word 的一些问题小结

    1.在"组件服务"的DCom配置中找到Microsoft Office Word =>属性=>标识选项卡,然后选择交互式用户:::=>"安全" ...

随机推荐

  1. JDBC增强

    JDBC增强 批处理:批量处理sql语句,比如批量添加用户信息. addBatch()  //pstmt.addBatch()  就是替换一条一条执行的execute****** executeBat ...

  2. adb server is out of date.

    1:今天调试android的时候发现一个诡异的问题 C:\Users\xxxx>adb start-server adb server is out of date.  killing... A ...

  3. JS学习笔记(三)函数

    js中的方法名一般都是首字母小写,其余单词首字母大写的规范. 声明 function 函数名(参数列表) { // 函数体 return 返回值; } 调用 函数名(); (js中花括号喜欢用这种方式 ...

  4. Javascript 自定义事件 (custom event)

    Javascript 中经常会用到自定义事件.如何创建一个简单的自定义事件呢?在创建自定义的事件之前,我们应该考虑一下和事件有关的东西.例如 click 事件,首先我们要能注册一个click事件(在一 ...

  5. 2014.8.18for循环

    for循环 1.初始状态 2.循环条件 3.循环体 4.状态改变 语法    for( 初始状态 ; 循环条件 ; 状态改变 ) { 循环体; } eg: ; i <= ; i++) { Con ...

  6. hive join 优化

    common join : 即reducer join,瓶颈在shuffle阶段,会产生较大的网络io: map join:即把小表放前面,扫描后放入每个节点的内存,在map阶段进行匹配: 开启map ...

  7. BZOJ 3774: 最优选择( 最小割 )

    最小割...二分染色然后把颜色不同的点的源汇反过来..然后就可以做了. 某个点(x,y): S->Id(x,y)(回报), Id(x,y)->T(代价), Id(i,j)&& ...

  8. 关于eclipse(64位)下aptana插件安装报错问题解决

    最近一直没有写过js,换了新电脑以后,eclipse下的aptana插件也没有装过,这几天要写js想重新把aptana装上,但是不知怎的,link方式.在线安装方式还有离线包下载下来利用eclipse ...

  9. Android Gradle配置

    解决问题 错误: Could not find the AndroidManifest.xml file, going up from path //打开app build.gradle文件加入以下代 ...

  10. 排序算法——交换排序(冒泡排序、快速排序)(java)

    一.冒泡排序 时间复杂度:O(n^2) 公认最慢的排序,每次把最大/最小的放一边,原理: [57,68,59,52] [57,68,59,52] [57,59,68,52] [57,59,52,68] ...