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(文 ...
随机推荐
- Nginx实现七层负载均衡配置指导
本文描述了如何使用Nginx实现在应用层实现7层负载均衡功能,Nginx支持虚拟主机,可以按照轮询,IP哈希,URL哈希,权重方式对后端服务器做负载均衡,还支持后端服务器健康检查功能.废话不多说,详细 ...
- Git branch (分支学习)
以前总结的一些git操作,分享在这里. Git 保存的不是文件差异或者变化量,而只是一系列文件快照. - 列出当前所有分支 git branch <--merge> | <--n ...
- 远程连接centos
yum install tigervnc yum install tigervnc-server Windows 7下载客户端 tigervnc-1.2.0.exe,在http://sourcef ...
- Restful与webService区别
有好多人问我们在设计底层服务的时候到底是应该选择目前最流行的RestFul架构还是选择老牌的webService呢?今天我就将这两个概念做一下阐述,到底什么情况下选择什么比较合理. 首先需要了解:RE ...
- TextField笔记
今天写scrollPanel组件,碰到一个问题:textfield自动什么时候会调节高度. 在创建TextField的时候,我制定了文本的height属性. 之后,无论怎么设置文本,height总是不 ...
- 输入一个正数 n,输出所有和为 n 连续正数序列。 java实现
用了半小时才写出来啊, 其实这种思路应用范围很广,利用有序性这个特点,比如本体只要是有序就行,例如两个集合是有序的,求交集,有序数组的查找,有序数组的归并,有序有了优化的可能. 输入一个正数 n,输出 ...
- Java JDK8 安装及环境变量配置
步骤: 1.下载JDK 2.安装 3.配置环境变量 注意: 1.在选择安装路径时,不要选择C盘或D盘下的[Program Files]目录.此目录名中有空格,会导致配置不成功. 参照链接: http: ...
- HW4.46
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- HW1.5
public class Solution { public static void main(String[] args) { System.out.println("(9.5 * 4.5 ...
- Codeforces245H - Queries for Number of Palindromes(区间DP)
题目大意 给定一个字符串s,q个查询,每次查询返回s[l-r]含有的回文子串个数(题目地址) 题解 和有一次多校的题目长得好相似,这个是回文子串个数,多校的是回文子序列个数 用dp[i][j]表示,s ...