winform把Office转成PDF文件

作者:springsnow 时间:2023-03-29 01:09:32 

先要把word或ppt转换为pdf; 以pdf的格式展示,防止文件拷贝。

转换方法

1、安装Word、Excel、PowerPoint组件

注意:需安装Microsoft.Office.Interop.Word\Excel\PowerPoint组件。

winform把Office转成PDF文件

程序集如下:

winform把Office转成PDF文件

2、转换代码

(1)将Word转换为pdf: 

using Microsoft.Office.Core;
using System;
using System.IO;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Word = Microsoft.Office.Interop.Word;

namespace WindowsFormsApp4
{
   public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

private void Form1_Load(object sender, EventArgs e)
       {
           bool isSuccess = DOCConvertToPDF(Directory.GetCurrentDirectory() + "\\aa.docx", Directory.GetCurrentDirectory() + "\\aa.pdf");
           if (isSuccess)
           {
               pdfViewer1.LoadFromFile(Directory.GetCurrentDirectory() + "\\aa.pdf");
           }
       }

/// <summary>
       /// Word转换成pdf
       /// </summary>
       /// <param name="sourcePath">源文件路径</param>
       /// <param name="targetPath">目标文件路径</param>
       /// <returns>true=转换成功</returns>
       public static bool DOCConvertToPDF(string sourcePath, string targetPath)
       {
           bool result = false;
           Word.Application app = new Word.Application();
           Word.Document doc = null;
           object missing = System.Reflection.Missing.Value;
           object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
           try
           {
               app.Visible = false;
               doc = app.Documents.Open(sourcePath);
               doc.ExportAsFixedFormat(targetPath, Word.WdExportFormat.wdExportFormatPDF);
               result = true;
           }
           catch (Exception ex)
           {
               result = false;
               throw new ApplicationException(ex.Message);
           }
           finally
           {
               if (doc != null)
               {
                   doc.Close(ref saveChanges, ref missing, ref missing);
                   doc = null;
               }
               if (app != null)
               {
                   app.Quit(ref missing, ref missing, ref missing);
                   app = null;
               }
               GC.Collect();
               GC.WaitForPendingFinalizers();
           }
           return result;
       }

}
}

(2)把Excel文件转换成PDF格式文件

/// <summary>
/// 把Excel文件转换成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool XLSConvertToPDF(string sourcePath, string targetPath)
{
   bool result = false;
   Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
   object missing = Type.Missing;
   Excel.Application app = null;
   Excel.Workbook book = null;
   try
   {
       app = new Excel.Application();
       object target = targetPath;
       object type = targetType;
       book = app.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
       missing, missing, missing, missing, missing, missing, missing, missing, missing);
       book.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
       result = true;
   }
   catch (Exception ex)
   {
       result = false;
       throw new ApplicationException(ex.Message);
   }
   finally
   {
       if (book != null)
       {
           book.Close(true, missing, missing);
           book = null;
       }
       if (app != null)
       {
           app.Quit();
           app = null;
       }
       GC.Collect();
       GC.WaitForPendingFinalizers();
   }
   return result;
}

(3)把PowerPoint文件转换成PDF格式文件

///<summary>
/// 把PowerPoint文件转换成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路径</param>
///<param name="targetPath">目标文件路径</param>
///<returns>true=转换成功</returns>
public static bool PPTConvertToPDF(string sourcePath, string targetPath)
{
   bool result = false;

PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
   object missing = Type.Missing;
   PowerPoint.Application app = null;
   PowerPoint.Presentation pres = null;
   try
   {
       app = new PowerPoint.Application();
       pres = app.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
       pres.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
       result = true;
   }
   catch (Exception ex)
   {
       result = false;
       throw new ApplicationException(ex.Message);
   }
   finally
   {
       if (pres != null)
       {
           pres.Close();
           pres = null;
       }
       if (app != null)
       {
           app.Quit();
           app = null;
       }
       GC.Collect();
       GC.WaitForPendingFinalizers();
   }
   return result;
}

来源:https://www.cnblogs.com/springsnow/p/13305794.html

标签:C#,winform,Office,PDF,文件
0
投稿

猜你喜欢

  • C#仿QQ实现简单的截图功能

    2021-10-11 16:43:15
  • Android UI控件之Gallery实现拖动式图片浏览效果

    2023-02-06 13:22:01
  • Java cookie和session会话技术介绍

    2021-12-30 06:51:22
  • intellij idea中安装、配置mybatis插件Free Mybatis plugin的教程详解

    2022-10-22 08:01:46
  • Java基础之重载(Overload)与重写(Override)详解

    2023-07-31 08:10:25
  • 解析Java继承中方法的覆盖和重载

    2021-09-02 02:02:32
  • Android仿IOS ViewPager滑动进度条

    2022-10-31 08:27:38
  • java加载properties文件的六种方法总结

    2023-09-20 05:24:54
  • Java 实战项目之CRM客户管理系统的实现流程

    2022-12-01 22:50:54
  • java Swing组件setBounds()简单用法实例分析

    2023-11-23 13:35:54
  • 详解Java多态对象的类型转换与动态绑定

    2021-10-12 06:59:59
  • Java设计模式之享元模式示例详解

    2022-12-08 22:19:46
  • Android简单使用PopupWindow的方法

    2023-09-13 19:51:22
  • java实现ftp上传 如何创建文件夹

    2021-06-10 10:49:17
  • android使用handlerthread创建线程示例

    2023-07-05 17:29:57
  • DWR异常情况处理常见方法解析

    2022-10-14 02:10:50
  • java开发之MD5加密算法的实现

    2022-05-13 23:44:35
  • Spring中的REST分页的实现代码

    2023-03-16 01:06:46
  • 你什么是Elastic Stack(ELK)

    2022-12-02 20:12:43
  • Eclipse 2020-06 汉化包安装步骤详解(附汉化包+安装教程)

    2021-05-31 09:26:37
  • asp之家 软件编程 m.aspxhome.com