word ppt excel文档转换成pdf
1.把word文档转换成pdf
(1).添加引用
using Microsoft.Office.Interop.Word;
添加引用
(2).转换方法
/// <summary>
/// 把Word文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns>成功返回true,失败返回false</returns>
public static bool WordToPdf(string sourcePath, string targetPath)
{
bool result = false;
WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;//转换格式1.wdExportFormatPDF转换成pdf格式 2.wdExportFormatXPS转换成xps格式
object missing = Type.Missing;
Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
Document document = null;
try
{
applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
object inputfileName = sourcePath;//需要转格式的文件路径
string outputFileName = targetPath;//转换完成后PDF或XPS文件的路径和文件名名称
WdExportFormat exportFormat = wdExportFormatPDF;//导出文件所使用的格式
bool openAfterExport = false;//转换完成后是否打开
WdExportOptimizeFor wdExportOptimizeForPrint = WdExportOptimizeFor.wdExportOptimizeForPrint;//导出方式1.wdExportOptimizeForPrint针对打印进行导出,质量较高,生成的文件大小较大。2.wdExportOptimizeForOnScreen 针对屏幕显示进行导出,质量较差,生成的文件大小较小。
WdExportRange wdExportAllDocument = WdExportRange.wdExportAllDocument;//导出全部内容(枚举)
int from = ;//起始页码
int to = ;//结束页码
WdExportItem wdExportDocumentContent = WdExportItem.wdExportDocumentContent;//指定导出过程中是否只包含文本或包含文本的标记.1.wdExportDocumentContent:导出文件没有标记,2.导出文件有标记
bool includeDocProps = true;//指定是否包含新导出的文件在文档属性
bool keepIRM = true;//
WdExportCreateBookmarks wdExportCreateWordBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;//1.wdExportCreateNoBookmarks:不要在导出文件中创建书签,2.wdExportCreateHeadingBookmarks:标题和文本框导出的文件中创建一个书签,3.wdExportCreateWordBookmarks每个字的书签,其中包括除包含页眉和页脚中的所有书签导出的文件中创建一个书签。
bool docStructureTags = true;
bool bitmapMissingFonts = true;
bool UseISO19005_1 = false;//生成的文档是否符合 ISO 19005-1 (PDF/A)
document = applicationClass.Documents.Open(ref inputfileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (document != null)
{
document.ExportAsFixedFormat(outputFileName, exportFormat, openAfterExport, wdExportOptimizeForPrint, wdExportAllDocument, from, to, wdExportDocumentContent, includeDocProps, keepIRM, wdExportCreateWordBookmarks, docStructureTags, bitmapMissingFonts, UseISO19005_1, ref missing);
}
result = true;
}
catch
{
result = false;
}
finally
{
if (document != null)
{
document.Close(ref missing, ref missing, ref missing);
document = null;
}
if (applicationClass != null)
{
applicationClass.Quit(ref missing, ref missing, ref missing);
applicationClass = null;
}
}
return result;
}
方法
/// <summary>
/// 把Word文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns>成功返回true,失败返回false</returns>
public static bool WordToPdf(object sourcePath, string targetPath)
{
bool result = false;
WdExportFormat wdExportFormatPDF = WdExportFormat.wdExportFormatPDF;
object missing = Type.Missing;
Microsoft.Office.Interop.Word.ApplicationClass applicationClass = null;
Document document = null;
try
{
applicationClass = new Microsoft.Office.Interop.Word.ApplicationClass();
document = applicationClass.Documents.Open(ref sourcePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
if (document != null)
{
document.ExportAsFixedFormat(targetPath, wdExportFormatPDF, false, WdExportOptimizeFor.wdExportOptimizeForPrint, WdExportRange.wdExportAllDocument, , , WdExportItem.wdExportDocumentContent, true, true, WdExportCreateBookmarks.wdExportCreateWordBookmarks, true, true, false, ref missing);
}
result = true;
}
catch
{
result = false;
}
finally
{
if (document != null)
{
document.Close(ref missing, ref missing, ref missing);
document = null;
}
if (applicationClass != null)
{
applicationClass.Quit(ref missing, ref missing, ref missing);
applicationClass = null;
}
}
return result;
}
简洁方法
(3).调用
OfficeToPdf.WordToPdf("d:\\1234.doc", "d:\\1234.pdf");
调用
2.把Excel文档转换成pdf
(1).添加引用
using Microsoft.Office.Interop.Excel;
添加引用
(2).转换方法
/// <summary>
/// 把Excel文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns></returns>
public static bool ExcelToPdf(string sourcePath, string targetPath)
{
bool result = false;
XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
Workbook workbook = null;
try
{
applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
string inputfileName = sourcePath;//需要转格式的文件路径
string outputFileName = targetPath;//转换完成后PDF文件的路径和文件名名称
XlFixedFormatType xlFixedFormatType = xlTypePDF;//导出文件所使用的格式
XlFixedFormatQuality xlFixedFormatQuality = XlFixedFormatQuality.xlQualityStandard;//1.xlQualityStandard:质量标准,2.xlQualityMinimum;最低质量
bool includeDocProperties = true;//如果设置为True,则忽略在发布时设置的任何打印区域。
bool openAfterPublish = false;//发布后不打开
workbook = applicationClass.Workbooks.Open(inputfileName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
if (workbook!=null)
{
workbook.ExportAsFixedFormat(xlFixedFormatType, outputFileName, xlFixedFormatQuality, includeDocProperties, openAfterPublish, missing, missing, missing, missing);
}
result = true;
}
catch
{
result = false;
}
finally
{
if (workbook != null)
{
workbook.Close(true, missing, missing);
workbook = null;
}
if (applicationClass != null)
{
applicationClass.Quit();
applicationClass = null;
}
}
return result;
}
方法
/// <summary>
/// 把Excel文件转换成pdf文件
/// </summary>
/// <param name="sourcePath">需要转换的文件路径和文件名称</param>
/// <param name="targetPath">转换完成后的文件的路径和文件名名称</param>
/// <returns></returns>
public static bool ExcelToPdf(string sourcePath, string targetPath)
{
bool result = false;
XlFixedFormatType xlTypePDF = XlFixedFormatType.xlTypePDF;//转换成pdf
object missing = Type.Missing;
Microsoft.Office.Interop.Excel.ApplicationClass applicationClass = null;
Workbook workbook = null;
try
{
applicationClass = new Microsoft.Office.Interop.Excel.ApplicationClass();
workbook = applicationClass.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
if (workbook != null)
{
workbook.ExportAsFixedFormat(xlTypePDF, targetPath, 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 (applicationClass != null)
{
applicationClass.Quit();
applicationClass = null;
}
}
return result;
}
简洁方法
(3).调用
OfficeToPdf.ExcelToPdf("d:\\1234.xls", "d:\\1234.pdf");
调用
3.把ppt转换成pdf
(1).添加引用
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
添加引用
(2).转换方法
///<summary>
/// 把PowerPoint文件转换成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路径</param>
///<param name="targetPath">目标文件路径</param>
///<returns>成功返回true,失败返回false</returns>
public static bool PPTConvertToPDF(string sourcePath, string targetPath)
{
bool result;
PpSaveAsFileType ppSaveAsFileType = PpSaveAsFileType.ppSaveAsPDF;//转换成pdf
object missing = Type.Missing;
Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
Presentation persentation = null;
try
{
application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
if (persentation!=null)
{
persentation.SaveAs(targetPath, ppSaveAsFileType, MsoTriState.msoTrue);
}
result = true;
}
catch
{
result = false;
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
}
return result;
}
方法
(3).调用
OfficeToPdf.PPTToPDF("d:\\12345.pptx", "d:\\12345.pdf");
调用
word ppt excel文档转换成pdf的更多相关文章
- C#实现文档转换成PDF
网上有很多将doc.ppt.xls等类型的文档转换成pdf的方法,目前了解到的有两大类: 1.使用虚拟打印机将doc.ppt.xls等类型的文档 2.使用OFFICE COM组件 我采用了第二种方法实 ...
- ASP.NET将word文档转换成pdf的代码
一.添加引用 using Microsoft.Office.Interop.Word; 二.转换方法 1.方法 C# 代码 /// <summary> /// 把Word文件转换成pdf文 ...
- asp.net将ppt文档转换成pdf
一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法 C# 代码 复制 // ...
- Python将word文档转换成PDF文件
如题. 代码: ''' #將word文档转换为pdf文件 #用到的库是pywin32 #思路上是调用了windows和office功能 ''' #导入所需库 from win32com.client ...
- Java利用aspose-words将word文档转换成pdf(破解 无水印)
首先下载aspose-words-15.8.0-jdk16.jar包 http://pan.baidu.com/s/1nvbJwnv 引入jar包,编写Java代码 package doc; impo ...
- Java实现批量将word文档转换成PDF
先导入words的jar包 需要jar包的私聊我发你 代码如下:import com.aspose.words.Document;import java.io.File; public class W ...
- C# word文档转换成PDF格式文档
最近用到一个功能word转pdf,有个方法不错,挺方便的,直接调用即可,记录下 方法:ConvertWordToPdf(string sourcePath, string targetPath) so ...
- C#调用WPS将文档转换成pdf进行预览
引用:https://www.jianshu.com/p/445996126c75 vs启动项目可以生成wps实例 本地iis部署的站点却不行 原因是vs是管理员权限,而iis没有权限 解决方法 启动 ...
- java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...
随机推荐
- c#自定义液晶数字控件
c#自定义液晶数字控件 项目中经常与硬件打交道,LED显示屏上面的液晶数字很好看,QT中直接就有数码管这个控件,C#中自己来写一个,其实掌握了一种自定义控件的写法,其他的都是浮云,举一反三,最笨的 ...
- C#初始化数组的三种方式
C#声明数组并初始化,有三种方式. 对于一维数组: using System;using System.Data;using System.Configuration;using System.Web ...
- C#使用SQLite出错:无法加载 DLL“SQLite.Interop.dll”,找不到指定的模块
在SQLite官方下载了System.Data.SQLite,编写如下测试代码: 复制内容到剪贴板 程序代码 using (SQLiteConnection conn = new SQLiteConn ...
- [转]NHibernate之旅(12):初探延迟加载机制
本节内容 引入 延迟加载 实例分析 1.一对多关系实例 2.多对多关系实例 结语 引入 通过前面文章的分析,我们知道了如何使用NHibernate,比如CRUD操作.事务.一对多.多对多映射等问题,这 ...
- QT-opencv小结
gcc –v 4.5 cmake编译opencv 2.4.4 ox00000005错误(gcc版本不对) gcc –v4.5 <----------qt 推荐打包,而不是static
- 引用类型传递 ListView展示数据
教师评分项目总结 //创建一个SE员工类 1.1 //首先分析项目 * 01.我需要在LIstView控件中显示三个员工的信息 * 那么可以定义一个长度为3的数组来承载要显示的数据 * 0 ...
- Jni 类型转换接口
1.JString char*互相转化 char* JstringToChar(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsstr ...
- 使用正则表达式匹配JS函数代码
使用正则表达式匹配JS函数代码 String someFunction="init"; Pattern regex = Pattern.compile("function ...
- gcc编译选项的循环重复查找依赖库等命令
link时,若liba.a依赖libb.a,若这样写 -lb -la,则链接通不过,gcc有个选项:-Xlinker ,可以让gcc在链接时反复查找依赖库,用法 : gcc -shared -o li ...
- Objective-C中一种消息处理方法performSelector: withObject:
Objective-C中调用函数的方法是“消息传递”,这个和普通的函数调用的区别是,你可以随时对一个对象传递任何消息,而不需要在编译的时候声明这些方法.所以Objective-C可以在runtime的 ...