前面两篇博客讲解了怎么通过程序控制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. Unix/Linux环境C编程入门教程(9) unbntu CCPP开发环境搭建

    1.      首先启动VMware,如果没有安装,请查看前面VMware的安装视频 2 启动虚拟机向导,选择自定义 3 单击下一步 4 选择稍后安装操作系统 5 .选择unbntu 64linux ...

  2. S3C6410嵌入式应用平台构建(二)

    [2014-4/11~4/14]经过之前的实验,对Uboot已经有了大体的了解,前我们已经把led灯给点亮,但这不是我们的根本目的,我们是要进入boot启动,经过两天的分析代码和反复的实验,终于可以进 ...

  3. icon

    <link rel="icon" href="favicon.ico" type="image/x-icon"> <lin ...

  4. Linux系统CentOS6.2版本号下安装JDK7具体过程

    前言:        java 是一种能够撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE( ...

  5. Sicily 4495. Print permutations

    http://soj.me/4495 按字典序生成字符串的全排列 直接递归: #include <iostream> #include <string> #include &l ...

  6. HDOJ 1561 - 树形DP,泛化背包

    刚看题...觉得这不是棵树...可能有回路...仔细一想..这还真是棵树(森林)...这是由于每个城堡所需要提前击破的城堡至多一个..对于一个城堡.其所需提前击破的城堡作为其父亲构图.... dp[k ...

  7. English - little,a little,a few,few的区别

    few.a few.little.a little的区别和联系: few / a few(形容词)用来修饰可数名词,few表示否定意义,没有,几乎没有, a few表示有肯定意思,有几个. 例如:a ...

  8. window.external.JavaScriptCallCpp

    方案2: 1.编写html <html> <head> </head> <body> <script language="javascr ...

  9. fork进程函数总结

    学习链接: http://blog.csdn.net/jason314/article/details/5640969 http://coolshell.cn/articles/7965.html 搜 ...

  10. 服务器搭建纪录linux+mysql+nginx+php

    新的项目启动 第一版 首先买了阿里云,选好环境镜像包,一键安装. 第一版php打算不用框架,完全手写,主要的功能点 数据交互和图片传输. 后台搭建好后,使用PHP的Laravel, web端还是选定b ...