word ppt excel文档转换成pdf的C#实现代码
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;
}
}
}
上一篇: php分页实现的有关问题
下一篇: PIVOT行转列,UNPIVOT列转行
推荐阅读
-
word ppt excel文档转换成pdf的C#实现代码
-
C#获取Word文档中所有表格的实现代码分享
-
C#获取Word文档中所有表格的实现代码分享
-
ASP.NET Core 2.0集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
-
使用C#给PDF文档添加注释的实现代码
-
在C#里面给PPT文档添加注释的实现代码
-
C# 基于NPOI+Office COM组件 实现20行代码在线预览文档(word,excel,pdf,txt,png)
-
极强PDF转换器将Word文档转换成Excel文档的方法介绍
-
在C#里面给PPT文档实现添加注释的代码分享
-
JavaScript打开word文档的实现代码(c#)_javascript技巧