word ppt excel文档转换成pdf的C#实现代码
发布时间:2022-10-10 16:51:54
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
namespace ConvertToPDF
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (DOCConvertToPDF("C:/test.doc", "C:/testD.pdf"))
{
MessageBox.Show("DOC转换成功!");
}
else
{
MessageBox.Show("对不起,转换失败!");
}
if (XLSConvertToPDF("C:/test.xls", "C:/testX.pdf"))
{
MessageBox.Show("XLS转换成功!");
}
else
{
MessageBox.Show("对不起,转换失败!");
}
if (PPTConvertToPDF("C:/需求提纲.pptx", "C:/testP.pdf"))
{
MessageBox.Show("PPT转换成功!");
}
else
{
MessageBox.Show("对不起,转换失败!");
}
}
//Word转换成pdf
///<summary>
/// 把Word文件转换成为PDF格式文件
///</summary>
///<param name="sourcePath">源文件路径</param>
///<param name="targetPath">目标文件路径</param>
///<returns>true=转换成功</returns>
private bool DOCConvertToPDF(string sourcePath, string targetPath)
{
bool result=false;
Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Word.ApplicationClass wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
catch
{
result = false;
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
///<summary>
/// 把Excel文件转换成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路径</param>
///<param name="targetPath">目标文件路径</param>
///<returns>true=转换成功</returns>
private bool XLSConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch
{
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>
private bool PPTConvertToPDF(string sourcePath, string targetPath)
{
bool result;
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
PowerPoint.ApplicationClass application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch
{
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;
}
}
}


猜你喜欢
- 前言单例模式,是工作中比较常见的一种设计模式,通常有两种实现方式,懒汉式和饿汉式。但是这两种实现方式存在一些问题。懒汉式需要在多线程环境下使
- 1、python的每一个语句的后面可以添加分号也可以不添加分号;在一行有多条语句的时候,必须使用分号加以区分2、查看Python版本号,在D
- 知识准备需要了解POI工具,以及POI对Excel中的对象的封装对应关系。什么是POIApache POI 是用Java编写的免费开源的跨平
- Android 中 Tweened animation的实例详解Tweened animation有四种类型,下面主要介绍Scale类型。运
- Spring Boot读取yml文件的主要方式有以下几种:1.@Value注解? 我们可以在bean的属性上使用@Value注解,直接读取y
- 本文实例为大家分享了java客户端登陆服务器用户名验证的具体实现代码,供大家参考,具体内容如下客户端通过键盘录入用户名,服务端对用户名进行验
- 一、前言java是一门跨硬件平台的面向对象高级编程语言,java程序运行在java虚拟机上(JVM),由JVM管理内存,这点是和C++最大区
- GC简介何为GCGC(Garbage Collection)称之为垃圾回收,是对内存中的垃圾对象,采用一定的算法进行内存回收的一个动作。比方
- Android仿通话来电界面,供大家参考,具体内容如下简介:开发中需要模拟来电时的通话界面,仿照来电界面实现来电时播放铃声,界面通过动画模拟
- 网上各种解决方案,我试了好久,整合了几篇文章才凑出来,在这里分享一下,实在不想网友们在这里面绕圈子,毕竟,写代码的时间是愉快的,解决bug也
- 本文实例分析了Android编程之json解析的方法。分享给大家供大家参考,具体如下:JSON的定义:一种轻量级的数据交换格式,具有良好的可
- 安卓和苹果的客户端开发中,经常会使用到webview,我们一般做法是将webview加入到native页面中。当我们对页面进行销毁的时候,其
- 如何配置swagger?在使用项目中,我们希望去查看我们的webapi的测试,那么我们是需要去有一个集成的测试的。步骤1.在nutget管理
- 问题使用前后端分离模式开发项目时,往往会遇到这样一个问题 -- 无法跨域获取服务端数据这是由于浏览器的同源策略导致的,目的是为了安全。在前后
- 本文以一个实例的形式讲述了C#实现复杂XML的序列化与反序列化的方法。分享给大家供大家参考。具体方法如下:已知.xml(再此命名defaul
- 本文实例总结了C#中split用法。分享给大家供大家参考,具体如下:以下是我转载的两个不同的人的,方便大家及自己查阅string s=&qu
- 关于Fresco的优点大家自行谷歌吧,它太强大太优秀了,我这一片小博文容纳不下啊羡慕,今天主要给大家简单介绍下Fresco的使用以及一些属性
- 我们在shader中对贴图处理时,有时候会有一些比较复杂的运算,比方说三角函数,开方等,一般情况下,如果可以在越上层做运算,性能会越高。C#
- 在软件开发过程中经常需要知道程序运行的大概时间,或者需要在规定时间内取数据,这是可以使用下面的方法获取时间段,还可以用在限时循环方法一:/*
- C#文件夹加锁小工具用C#语言实现一个文件夹锁的程序,网上类似的“xxx文件夹xxx”软件很多,但是基本上都是C/C++语言实现的,且都没有