C#操作Office.word(三)
前面两篇博客讲解了怎么通过程序控制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(三)的更多相关文章
- C#操作Office.word(二)
在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档, ...
- C#操作Office.word(一)
该文章主要是讲述如何使用VS2010创建word文档,因为在项目中我们可能需要点击一个按钮把数据库中的项目表单或图片显示到word文档中,因此该文章主要分析如何使用VS2010创建word文档并填写相 ...
- 使用ABAP编程实现对微软Office Word文档的操作
SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...
- java 使用 POI 操作 XWPFDocumen 创建和读取 Office Word 文档基础篇
注:有不正确的地方还望大神能够指出,抱拳了 老铁! 参考 API:http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDoc ...
- 新建 Microsoft Office Word 文档 来源:牛客网
题目 链接:https://ac.nowcoder.com/acm/contest/28886/1015 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...
- 打开office word excel弹出visual studio 2008
打开office word 或者excel或者ppt等文档就会弹出visual studio 2008正在配置对话框. 按照我下面的步骤,解决方法非常简单. 如果你的电脑是win7操作系统,从第一步开 ...
- 【Office Word】论文排版有关技巧
本文分两部分,第一部分呢是Word中标题的编号以及图表的编号:第二部分是MathType中公式编号的右对齐方法. 1. word中标题的编号以及图表的编号 本部分转载自:http://blog ...
- Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结
Atitit.office word excel ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word excel pdf 的web预览要求 ...
- 关于 .net 开发 Office Word 的一些问题小结
1.在"组件服务"的DCom配置中找到Microsoft Office Word =>属性=>标识选项卡,然后选择交互式用户:::=>"安全" ...
随机推荐
- CentOS 删除自带的OpenJDK 和 安装SunJDK
[root@WX32 local]# java -version java version "1.6.0" OpenJDK Runtime Environment (build - ...
- genymotion 模拟器 真是牛叉了 速度超快啊!!! 不解释了!建议大家速度去体验一把吧!
已经有人写了blog了 我就不再赘述了,详情去这里看去吧!! android genymotion模拟器怎么使用以及和google提供的模拟器性能对比 http://blog.csdn.net/ ...
- 删除select中所有option选项
这样写 <select id="search"> <option>baidu</option> <option>sogou</ ...
- UVA 12075 - Counting Triangles(容斥原理计数)
题目链接:12075 - Counting Triangles 题意:求n * m矩形内,最多能组成几个三角形 这题和UVA 1393类似,把总情况扣去三点共线情况,那么问题转化为求三点共线的情况,对 ...
- 根据输出设置select的被选中值
$("#startupStatus").find("option").map(function(i) { if ($('#st-status').val() = ...
- Android快速开发框架汇总
知乎贴:Android 开发有什么好的架构么? 里面这篇不错:Architecting Android…The clean way? 知乎贴: 一.如果对App的性能.包size有要求,对代码有洁癖不 ...
- 关于matlab鼠标响应
今天看了一下Matlab中响应鼠标的事件,整理如下, (1)函数WindowButtonMotionFcn,当鼠标在窗口上运动的时候就会相应此函数,于是在此函数中可以设置运动时想要的代码,如:改变鼠标 ...
- 穿透的 div ( pointer-events )
pointer-events 是一個滿有趣的 CSS3 屬性,雖然主要是針對 SVG ,但其中幾個屬性應用在 div 上也是頗有意思.顧名思義,這是一個針對滑鼠事件的屬性,預設值為 auto,若值為 ...
- IO库 8.5
题目:重写8.4中的函数,将每一个单词作为一个独立的元素进行存储. #include <iostream> #include <fstream> #include <st ...
- JS兼容性问题列表
记录平时遇见的兼容性问题,有更好的解决办法希望各位提出,会随着开发遇到问题而更新,标记为黄色的为未解决和猜测答案 提出时间 问题描述 解决方案 2014/10/22 submit按钮阻止了默认事件不能 ...