tnblog
首页
视频
资源
登录

基于office 把word,wordx,ppt,pptx,xls,xlsx转为pdf在线预览

7372人阅读 2019/7/3 9:23 总访问:51026 评论:0 收藏:1 手机
分类: .net
  1.   //,要在服务器安装office,需要引用using Microsoft.Office.Interop.Word;using Microsoft.Office.Interop.PowerPoint;using Microsoft.Office.Interop.Excel;的dll
  2. public class FileConvertHelper
  3.     {
  4.         /// <summary>
  5.         /// Ppt转pdf文件
  6.         /// </summary>
  7.         /// <param name="sourcePath"></param>
  8.         /// <param name="targetPath"></param>
  9.         /// <param name="targetFileType"></param>
  10.         /// <returns></returns>
  11.         public static bool PptToPDF(string sourcePath, string targetPath, PpSaveAsFileType targetFileType = PpSaveAsFileType.ppSaveAsPDF)
  12.         {
  13.             bool result;
  14.             object missing = Type.Missing;
  15.             Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
  16.             Presentation persentation = null;
  17.             try
  18.             {
  19.                 application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
  20.                 persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
  21.                 persentation.SaveAs(targetPath, targetFileType, MsoTriState.msoTrue);
  22.                 result = true;
  23.             }
  24.             catch (Exception e)
  25.             {
  26.                 string aa = e.Message;
  27.                 result = false;
  28.             }
  29.             finally
  30.             {
  31.                 if (persentation != null)
  32.                 {
  33.                     persentation.Close();
  34.                     persentation = null;
  35.                 }
  36.                 if (application != null)
  37.                 {
  38.                     application.Quit();
  39.                     application = null;
  40.                 }
  41.                 GC.Collect();
  42.                 GC.WaitForPendingFinalizers();
  43.                 GC.Collect();
  44.                 GC.WaitForPendingFinalizers();
  45.             }
  46.             return result;
  47.         }
  48.         /// <summary>
  49.         /// Word转换成pdf
  50.         /// </summary>
  51.         /// <param name="sourcePath">源文件路径</param>
  52.         /// <param name="targetPath">目标文件路径</param>
  53.         /// <returns>true=转换成功</returns>
  54.         public static bool WordToPDF(string sourcePath, string targetPath)
  55.         {
  56.             bool result = false;
  57.             if (File.Exists(targetPath))
  58.             {
  59.                 result = true;
  60.                 return result;
  61.             }
  62.             string targetDic = Path.GetDirectoryName(targetPath);
  63.             if (!Directory.Exists(targetDic))
  64.             {
  65.                 Directory.CreateDirectory(targetDic);
  66.             }
  67.             WdExportFormat exportFormat = WdExportFormat.wdExportFormatPDF;
  68.             object paramMissing = Type.Missing;
  69.             Microsoft.Office.Interop.Word.Application wordApplication = new Microsoft.Office.Interop.Word.Application();
  70.             Document wordDocument = null;
  71.             try
  72.             {
  73.                 object paramSourceDocPath = sourcePath;
  74.                 string paramExportFilePath = targetPath;
  75.                 WdExportFormat paramExportFormat = exportFormat;
  76.                 bool paramOpenAfterExport = false;
  77.                 WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
  78.                 WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
  79.                 int paramStartPage = 0;
  80.                 int paramEndPage = 0;
  81.                 WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
  82.                 bool paramIncludeDocProps = true;
  83.                 bool paramKeepIRM = true;
  84.                 WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
  85.                 bool paramDocStructureTags = true;
  86.                 bool paramBitmapMissingFonts = true;
  87.                 bool paramUseISO19005_1 = false;
  88.                 wordDocument = wordApplication.Documents.Open(
  89.                     ref paramSourceDocPath, ref paramMissing, ref paramMissing,
  90.                     ref paramMissing, ref paramMissing, ref paramMissing,
  91.                     ref paramMissing, ref paramMissing, ref paramMissing,
  92.                     ref paramMissing, ref paramMissing, ref paramMissing,
  93.                     ref paramMissing, ref paramMissing, ref paramMissing,
  94.                     ref paramMissing);
  95.                 if (wordDocument != null)
  96.                     wordDocument.ExportAsFixedFormat(paramExportFilePath,
  97.                         paramExportFormat, paramOpenAfterExport,
  98.                         paramExportOptimizeFor, paramExportRange, paramStartPage,
  99.                         paramEndPage, paramExportItem, paramIncludeDocProps,
  100.                         paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
  101.                         paramBitmapMissingFonts, paramUseISO19005_1,
  102.                         ref paramMissing);
  103.                 result = true;
  104.             }
  105.             catch (Exception ex)
  106.             {
  107.                 result = false;
  108.                 throw new ApplicationException(ex.Message);
  109.             }
  110.             finally
  111.             {
  112.                 if (wordDocument != null)
  113.                 {
  114.                     wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
  115.                     wordDocument = null;
  116.                 }
  117.                 if (wordApplication != null)
  118.                 {
  119.                     wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
  120.                     wordApplication = null;
  121.                 }
  122.                 GC.Collect();
  123.                 GC.WaitForPendingFinalizers();
  124.             }
  125.             return result;
  126.         }
  127.         /// <summary>
  128.         /// 把Excel文件转换成PDF格式文件  
  129.         /// </summary>
  130.         /// <param name="sourcePath">源文件路径</param>
  131.         /// <param name="targetPath">目标文件路径</param>
  132.         /// <returns>true=转换成功</returns>
  133.         public static bool ExcelToPDF(string sourcePath, string targetPath)
  134.         {
  135.             bool result = false;
  136.             if (File.Exists(targetPath))
  137.             {
  138.                 result = true;
  139.                 return result;
  140.             }
  141.             string targetDic = Path.GetDirectoryName(targetPath);
  142.             if (!Directory.Exists(targetDic))
  143.             {
  144.                 Directory.CreateDirectory(targetDic);
  145.             }
  146.             XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF;
  147.             object missing = Type.Missing;
  148.             Microsoft.Office.Interop.Excel.Application application = null;
  149.             Workbook workBook = null;
  150.             try
  151.             {
  152.                 application = new Microsoft.Office.Interop.Excel.Application();
  153.                 object target = targetPath;
  154.                 object type = targetType;
  155.                 workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
  156.                     missing, missing, missing, missing, missing, missing, missing, missing, missing);
  157.                 workBook.ExportAsFixedFormat(targetType, target, XlFixedFormatQuality.xlQualityStandard, truefalse, missing, missing, missing, missing);
  158.                 result = true;
  159.             }
  160.             catch (Exception ex)
  161.             {
  162.                 result = false;
  163.                 throw new ApplicationException(ex.Message);
  164.             }
  165.             finally
  166.             {
  167.                 if (workBook != null)
  168.                 {
  169.                     workBook.Close(true, missing, missing);
  170.                     workBook = null;
  171.                 }
  172.                 if (application != null)
  173.                 {
  174.                     application.Quit();
  175.                     application = null;
  176.                 }
  177.                 GC.Collect();
  178.                 GC.WaitForPendingFinalizers();
  179.             }
  180.             return result;
  181.         }


评价

基于open office 各种类型转为pdf在线预览

//需要在服务器安装openoffice,引用cli_basetypes,cli_cppuhelper,cli_oootypes,cli_ure,cli_uretypes publicboolCon...

基于接口的Sort排序方法原理

集合提供了Sort,我们可以实现IComparer接口,来实现自定义的对象排序方法:例如:根据年龄来排序publicclassMySort:ICompa...

基于angular的HttpClient封装_HttpClient

基于angular的HttpClient封装_HttpClient,直接复制源码即可用包含常用的get、post、patch、delete和put请求;import{HttpC...

Elasticsearch 基于词项和基于全文的搜索

Elasticsearch 基于词项和基于全文的搜索[TOC] 基于 Term 的查询Term 的重要性Term 是表达语意的最小单位。搜索和利用语言...

Centos7安装k8s(基于kubeadm)

Centos7安装k8s(基于kubeadm)[TOC] 下载相关包 链接:https://pan.baidu.com/s/1H9MuZsf28f_3uyVHBUS8jQ提取码:w9tv ...

.net core调用基于GPT-3.5的ChatGPT接口

基于GPT-3.5的接口OpenAI 已经开放和官网版本的ChatGPT相同的模型 gpt-3.5-turbo GPT-3.5和3.0调用方式几乎一样,修改一下...

Pytorch 基于经典网络架构训练图像分类模型

Pytorch 基于经典网络架构训练图像分类模型[TOC] 数据预处理部分:数据增强:torchvision中transforms模块自带功能,比较...

基于Ollama创建Gemma定制化AI模型

基于Ollama创建Gemma定制化AI模型[TOC] 什么是Gemma?Gemma模型使用了和Gemini同源的技术,总共有20亿参数和70亿参数两种...

Asp.net MVC 利用(aspose+pdfobject.js) 实现在线预览office文档

Aspose.Total是Aspose公司旗下的最全的一套office文档管理方案,通过它,我们可以有计划地操纵一些商业中最流行的文件格式...

office2010安装与激活过程

去下载Office2010https://pan.baidu.com/s/1r929BBEzeJ-zxOac9z9SkA#list/path=/sharelink231672117-146812709623164/offic...

office 2010 安装剪贴画不可用

打开控制面板进入控制页面中心点击卸载程序进行程序修改与卸载点击鼠标右键进行修改选中添加与删除功能点击继续office 共享...

c 根据数字一个集合拆分成多个子集合

有时间需要根据一个数子把一个集合拆分成多个子集合后在进行操作,就可以使用下面的代码。List&lt;List&lt;string&gt;&gt;l...

c集合根据某个数字进去分组

实现一个根据某个数字分组的扩展方法这种方法的作用可以用在批量添加的时候分组去批量添加,还有其他情况需要分段解析等///...

layui 弹窗遮罩层弹窗挡住问题

代码:layer.open({ type:1, area:[&quot;300px&quot;,&quot;300px&quot;], content:$(&quot;#addvieww&quot;) });分析...

msdn英文修改成中文

非常简单,so easy只需要把地址栏的zh-cn修改成zh-cn即可
老子许灵灵,写字第一名
排名
45
文章
7
粉丝
3
评论
2
基于open office 把各种类型转为pdf在线预览
剑轩 : 都是些高大上的问题!
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术