c# word excel ppt 转pdf

office 2010 

 

using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

 

  1 //将word文档转换成PDF格式
  2         public bool DocConvert(string sourcePath, string targetPath, Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF)
  3         {
  4             bool result;
  5             object paramMissing = Type.Missing;
  6             Word.ApplicationClass wordApplication = new Word.ApplicationClass();
  7             Word._Document wordDocument = null;
  8             try
  9             {
 10                 object paramSourceDocPath = sourcePath;
 11                 string paramExportFilePath = targetPath;
 12 
 13                 Word.WdExportFormat paramExportFormat = exportFormat;
 14                 bool paramOpenAfterExport = false;
 15                 Word.WdExportOptimizeFor paramExportOptimizeFor =
 16                         Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
 17                 Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
 18                 int paramStartPage = 0;
 19                 int paramEndPage = 0;
 20                 Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
 21                 bool paramIncludeDocProps = true;
 22                 bool paramKeepIRM = true;
 23                 Word.WdExportCreateBookmarks paramCreateBookmarks =
 24                         Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
 25                 bool paramDocStructureTags = true;
 26                 bool paramBitmapMissingFonts = true;
 27                 bool paramUseISO19005_1 = false;
 28 
 29                 wordDocument = wordApplication.Documents.Open(
 30                         ref paramSourceDocPath, ref paramMissing, ref paramMissing,
 31                         ref paramMissing, ref paramMissing, ref paramMissing,
 32                         ref paramMissing, ref paramMissing, ref paramMissing,
 33                         ref paramMissing, ref paramMissing, ref paramMissing,
 34                         ref paramMissing, ref paramMissing, ref paramMissing,
 35                         ref paramMissing);
 36 
 37                 if (wordDocument != null)
 38                     wordDocument.ExportAsFixedFormat(paramExportFilePath,
 39                             paramExportFormat, paramOpenAfterExport,
 40                             paramExportOptimizeFor, paramExportRange, paramStartPage,
 41                             paramEndPage, paramExportItem, paramIncludeDocProps,
 42                             paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
 43                             paramBitmapMissingFonts, paramUseISO19005_1,
 44                             ref paramMissing);
 45                 result = true;
 46             }
 47             catch (Exception ex)
 48             {
 49                 log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ConvertToPdf));
 50                 logger.Error("DocConvert" + ex.Message);
 51                 result = false;
 52             }
 53             finally
 54             {
 55                 if (wordDocument != null)
 56                 {
 57                     wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
 58                     wordDocument = null;
 59                 }
 60                 if (wordApplication != null)
 61                 {
 62                     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
 63                     wordApplication = null;
 64                 }
 65                 GC.Collect();
 66                 GC.WaitForPendingFinalizers();
 67                 GC.Collect();
 68                 GC.WaitForPendingFinalizers();
 69             }
 70             return result;
 71         }
 72 
 73         //将excel文档转换成PDF格式
 74         public bool ExcelConvert(string sourcePath, string targetPath, XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF)
 75         {
 76             bool result;
 77             object missing = Type.Missing;
 78             Excel.ApplicationClass application = null;
 79             Workbook workBook = null;
 80             try
 81             {
 82                 application = new Excel.ApplicationClass();
 83                 object target = targetPath;
 84                 object type = targetType;
 85                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
 86                         missing, missing, missing, missing, missing, missing, missing, missing, missing);
 87 
 88                 workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
 89                 result = true;
 90             }
 91             catch (Exception ex)
 92             {
 93                 log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ConvertToPdf));
 94                 logger.Error("ExcelConvert" + ex.Message);
 95                 result = false;
 96             }
 97             finally
 98             {
 99                 if (workBook != null)
100                 {
101                     workBook.Close(true, missing, missing);
102                     workBook = null;
103                 }
104                 if (application != null)
105                 {
106                     application.Quit();
107                     application = null;
108                 }
109                 GC.Collect();
110                 GC.WaitForPendingFinalizers();
111                 GC.Collect();
112                 GC.WaitForPendingFinalizers();
113             }
114             return result;
115         }
116 
117         //将ppt文档转换成PDF格式
118         public bool PPtConvert(string sourcePath, string targetPath, PpSaveAsFileType targetFileType = PpSaveAsFileType.ppSaveAsPDF)
119         {
120             bool result;
121             object missing = Type.Missing;
122             PowerPoint.ApplicationClass application = null;
123             Presentation persentation = null;
124             try
125             {
126                 application = new PowerPoint.ApplicationClass();
127                 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
128                 persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
129 
130                 result = true;
131             }
132             catch (Exception ex)
133             {
134                 log4net.ILog logger = log4net.LogManager.GetLogger(typeof(ConvertToPdf));
135                 logger.Error("PPtConvert" + ex.Message);
136                 result = false;
137             }
138             finally
139             {
140                 if (persentation != null)
141                 {
142                     persentation.Close();
143                     persentation = null;
144                 }
145                 if (application != null)
146                 {
147                     application.Quit();
148                     application = null;
149                 }
150                 GC.Collect();
151                 GC.WaitForPendingFinalizers();
152                 GC.Collect();
153                 GC.WaitForPendingFinalizers();
154             }
155             return result;
156         }

 

转载于:https://www.cnblogs.com/dingshuaixing/p/10906675.html

【非付费】C#关于将Excel转为PDF的两种方法 其实有很多的组套软件提供Excel转PDF,而且也包含更多更强大的关于文件格式转化和操作的API,像是ITextshape,但全都是商业付费的。上午只找到两种可以使用的方法,暂时先记录一下。 阅读详情

相关推荐

C#实现MS-Office文档转Pdf(Word、Execel、PowerPoint、Visio、Project)

直接奉上源代码,运行环境:Office2010,Project2010(如果需要),Visio2010(如果需要),.net framework2.0 using System; using Microsoft.Office.Core; namespace Office { class Util { private Util() { } //

baiyangliu 8264

C# 将word/ppt文档转换为Pdf的三种方法

常需要将office 文件上传并通过网页进行在线阅读,一种最普遍的方法就是转换office文件到pdf格式,以pdf文件方式进行在线预览。文中介绍了三种office转pdf的方法。

lanhai96的专栏 1万+

C#word,ppt,excel转成pdf文件

需调用office组件,将word,ppt,excel转成pdf文件.

C#实现将excel转换成pdf的三种方法

总结了excel转pdf的三种方法

weixin_45487021的博客 1万+

C#EXCEL转PDF,word转PDF

C# 调用office2010组件,将EXCEL转PDF和word转PDF,其中解决了excel文档列过多的问题,需要安装office2010。。

winForm word转pdf excel转pdf

winForm word转pdf excel转pdf winForm word转pdf excel转pdf winForm word转pdf excel转pdf winForm word转pdf excel转pdf

C# word、excel转PDF,读取json数据

使用json using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using MSWord = Microsoft.Office.Interop.Word; using Microsoft.Office.Interop.Excel; using System.IO; using System.Reflect..

weixin_45084986的博客 541

Winform 实现Word/Excel转换为PDF文件

Winform 轻松实现Word/Excel转换为PDF文件

c#使用spire 把word、excel、pdf、ppt转图片

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Spire.Pdf; using Spire.Xls; using S.

qq_42016346的博客 2600

word转成pdf以及excel转成pdf案例,下载直接就能用

word转pdf,excel转pdf案例

PCB板质量测试和PCB板

PCB板质量测试和板厂

weixin_51370842的博客 510

[转]C#.net word excel powerpoint (ppt) 转换成 pdf 文件

C#.net word excel powerpoint (ppt) 转换成 pdf 文件 2010年12月31日 星期五 上午 11:29 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;us...

weixin_30706507的博客 275

二维“能量源”(电场、磁场、功率)入射边界设置方法

comsol光学仿真设计相关学习笔记和经验 1. 二维“能量源”(电场、磁场、功率)入射边界设置方法 2.边界条件设置薄膜材料

VJCP_90909Bu0的博客 5170

Word、Excel、PPT文件转PDF文件(C#)

使用C#将Word、Excel、PPT文件转成PDF文件

我想遇见你,在最美的季节 740

别再只看成交量了!用通达信这个VOL监测公式,一眼识别主力吸筹与出货信号

本文详细介绍了通达信VOL监测公式的实战应用,通过彩色量柱和换手率分层技术,帮助投资者识别主力吸筹与出货信号。该公式结合动态量能标准化和多周期均线组合,提供可视化主力行为图谱,提升交易决策的准确性。

weixin_28693447的博客 647

别再只会插卡了!用示波器实测SIM卡上电时序与通信波形(附故障排查)

本文深入解析了SIM卡通信的电气信号与时序规则,通过示波器实测波形揭示SIM卡上电时序与通信协议的关键细节。从VCC、CLK、RST、DATA四路信号的协同工作,到ATR(Answer to Reset)的解析,再到常见故障的波形诊断与排查方法,为硬件工程师提供了实用的调试指南。文章还分享了高级调试技巧与现代化工具链搭建建议,帮助提升物联网设备SIM卡通信的可靠性与效率。

weixin_42567279的博客 313

c# word ppt excel转pdf

命名空间“Microsoft”中不存在类型或命名空间名称“Office ”(是缺少程序集引用吗?)  解决办法:  添加引用/com/Microsoft Office 11.0 Object Library  然后在程序中 C#代码   1.using Microsoft.Office.Interop.Excel;   using Microsoft.Office.In

g710710的专栏 3594

C# wps转pdf(word、ppt、excel),在线预览pdf

wps转pdf   注:我是在wps试用期专业版,windows10系统 vs2019 webform(.net framework4.5)测试。   前提:需要下载安装wps专业版、企业版。   项目中需要引用wps的com组件     com组件Upgrade WPS Spreadsheets 3.0 Object Library (Beta) ,对应“Excel”,C:\WIN...

一劍傾城 1805

YOLOv11火焰识别:从环境搭建到GUI部署的完整工程实践

目标检测是计算机视觉的核心任务之一,它通过定位和分类图像中的物体,为机器赋予“看懂”世界的能力。其原理通常基于深度学习模型,如YOLO系列,通过卷积神经网络提取特征并预测边界框与类别。这项技术的价值在于将算法能力转化为实际生产力,广泛应用于安防监控、工业质检、自动驾驶等场景。在工程实践中,环境配置的稳定性与数据质量往往决定项目成败,而模型部署的易用性直接影响最终用户体验。本文以火焰识别这一具体应用为切入点,详细解析了如何利用YOLOv11完成从零到产品化的全链路流程,其中涉及Conda虚拟环境管理、PyTo

weixin_29483277的博客 232

AutoSar标准官网下载

AutoSar文档下载和文档命名格式。。。

m0_56208280的博客 3496

c# 文件类型转换汇总(word/excel/ppt/txt/pdf 转HTML&word/ppt 转swf)

最近项目中要用到文件预览,分为用HTML预览和用swf预览两种模式,所以要将用户上传的文件转成可以预览的格式。 首先是转HTML部分,word、excel、ppt、txt都是调用Microsoft.Office来完成的,全部类似,所以只贴一个word的代码: public static bool WordToHtml(string path) { ...

weixin_30439067的博客 245

[特殊字符] 2026终极实战:Python爬取得物App球鞋实时交易价格 —— 从签名破解到数据可视化全栈指南

价格敏感度:热门鞋款(如Air Jordan 1、Yeezy 350、Dunk SB)价格每分钟都在波动,短线套利空间巨大。稀缺性:限量发售、联名款、地区限定,导致供需关系实时变化。数据壁垒:得物App使用自研WMS签名算法设备指纹绑定动态请求头加密,是爬虫工程师的“练兵场”。本博客将手把手带你从零搭建一套生产级得物球鞋价格爬虫系统,涵盖:抓包与逆向分析(mitmproxy + Frida)签名算法逆向(Python复刻)设备模拟与心跳保活(ADB + uiautomator2)

2201_76125261的博客 1111

C#实现office文档转换为PDF或xps

需要安装office 2007 还有一个office2007的插件OfficeSaveAsPDFandXPS 下载地址 http://www.microsoft.com/downloads/details.aspx?FamilyId=4D951911-3E7E-4AE6-B059-A2E79ED87041&displaylang=en 这是一个微软官方出的office插件。 如果无...

weixin_30315723的博客 97

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

上一篇: 为图片添加边框(卡片式边框)
a48702668
博客等级 码龄10年 0粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值