winform把Office转成PDF文件
作者:springsnow 发布时间:2023-03-29 01:09:32
标签:C#,winform,Office,PDF,文件
先要把word或ppt转换为pdf; 以pdf的格式展示,防止文件拷贝。
转换方法
1、安装Word、Excel、PowerPoint组件
注意:需安装Microsoft.Office.Interop.Word\Excel\PowerPoint组件。
程序集如下:
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
0
投稿
猜你喜欢
- seata-1.4.0安装及使用 1、简介Seata 是一款开源的分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务。
- 这篇文章主要介绍了Java实现发送手机短信语音验证功能代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 获取resources文件路径背景在SpringBoot项目中,需要获取resources文件的路径,网上找了好多文章都不行,后面还是结合官
- 建造者模式(Builder Pattern)使用多个简单的对象一步一步构建成一个复杂的对象。这种类型的设计模式属于创建型模式,它提供了一种创
- 简单看一下描述,例子最重要。1、getPath():返回定义时的路径,(就是你写什么路径,他就返回什么路径)2、getAbsolutePat
- 工作笔记(在不知道json的key时如何获取当前json的keys)String json="{'name':
- 前言本文主要给大家介绍了关于Spring4自定义@Value功能的相关内容,使用的Spring版本4.3.10.RELEASE,下面话不多说
- mybatis在持久层框架中还是比较火的,一般项目都是基于ssm。虽然mybatis可以直接在xml中通过SQL语句操作数据库,很是灵活。但
- 1、前言当提及如何终止一个线程时,部分读者通常立马想到的方法肯定是stop(),但是stop()方法并不被推荐使用(很多规范中是禁止使用的)
- 一.导入Netty依赖<dependency> <groupId>io.netty</group
- 图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码目前限chrome浏览器使用首先以um-edit
- 这两天因为要做一个随机的地图生成系统,所以一直在研究随机迷宫生成算法,好吧,算是有一点小小的成果。随机迷宫生成我自己的理解简而言之分为以下几
- 执行引擎也只有几个概念, JVM方法调用和执行的基础数据结构是 栈帧, 是内存区域中 虚拟机栈中的栈元素, 每一个方法的执行就对应着一个栈帧
- 一、基本特点1. 开始时是乐观锁, 如果锁冲突频繁, 就转换为悲观锁.2. 开始是轻量级锁实现, 如果锁被持有的时间较长, 就转换成重量级锁
- 最近“全网域(Web Scale)”一词被炒得火热,人们也正在通过扩展他们的应用程序架构来使他们的系统变得更加“全网域”。但是究竟什么是全网
- 一、前言尽管Unity有一个像样的脚本编辑器(Mono),但很多人喜欢使用另一个编辑器。这篇短文解释了如何更改脚本编辑器,并介绍了Mono的
- 基于Java的简单的用户管理系统,供大家参考,具体内容如下此系统功能和方法都比较简单本次系统通过控制台输入商品的基本信息,加入管理员的登录与
- 在javaweb中写了一个图片的链接,可以打开预览,另外提供一个下载功能。以下是预览代码,没什么好说的;href若连接的是一个压缩包文件之类
- 什么是队列结构一种线性结构,具有特殊的运算法则【只能在一端(队头)删除,在另一端(队尾)插入】。分类:顺序队列结构链式队列结构基本操作:入队
- 本文实例讲述了Android+SQLite数据库实现的生词记事本功能。分享给大家供大家参考,具体如下:主activity命名为Dict:代码