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 =>属性=>标识选项卡,然后选择交互式用户:::=>"安全" ...
随机推荐
- 柯南君:看大数据时代下的IT架构(3)消息队列之RabbitMQ-安装、配置与监控
柯南君:看大数据时代下的IT架构(3)消息队列之RabbitMQ-安装.配置与监控 一.安装 1.安装Erlang 1)系统编译环境(这里采用linux/unix 环境) ① 安装环境 虚拟机:VMw ...
- golang之interface(接口)与 reflect 机制
一.概述 什么是interface,简单的说,interface是一组method的组合,通过interface来定义对象的一组行为: interface类型定义了一组方法,如果某个对象实现了某个接口 ...
- DataReader转泛型
实体类的字段类型要和数据库一致,不然可能会出现错误. /// <summary> /// DataReader转泛型 /// </summary> /// <typepa ...
- pycharm中添加扩展工具pylint
今天调试了好几个小时,想吧pylint集成到pycharm中去,从网上找了个宝贝帖 子,但是不好用,原因是作者写的脚本是检查工程和模块的,而我的是单独检查一个文件,当然前者肯定会在项目后期用的.所以就 ...
- Linux分区方案
创建三个分区 1./boot 启动分区 存放内核和启动程序 空间分配:100M 类型:ext4 2./swap 交换分区 虚 ...
- 在MAC下使用github
一 安装git 这个咱们就不说了,一般安装xcode都会自带git 如果不知道有没有下载git, 在终端运行 $which git 会打印出git的安装路径 /usr/local/bin/gi ...
- leetcode Contains Duplicate python
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- apache hide index.php
<Directory "D:/usr/local/www"> AllowOverride all Options +FollowSymLinks +SymL ...
- linux grep 指定字符串的正则表达式
cat all_uuid_log | grep "[a-z0-9]\{32\}"
- Eclipse+EGit的配置注意点, 以及解决Github多个本地仓库之间的冲突
问题描述 不同本地仓库(e.g. Repo1, Repo2)之间同时修改一个文件时, 出现文件无法merge的情况. 具体表现为, 冲突(红色双向实心箭头)一直存在, 点pull没反应, 点push报 ...