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


猜你喜欢
- 今天安装了jdk1.8、tomcat8、和maven3.5.2,弄好后在myeclipse新建了一个maven项目,项目默认是jdk1.5,
- 什么是MD5?Message Digest Algorithm MD5(中文名为消息摘要算法第五版)为计算机安全领域广泛使用的一种散列函数,
- 一、模糊查询的几种实现方式1.concat函数和#{}拼接的方式student_name like concat('%',#
- 本文实例讲述了Android编程基于距离传感器控制手机屏幕熄灭的方法。分享给大家供大家参考,具体如下:在现实生活中,打电话的时候手机挨着自己
- Android ToggleButton 详解在Android的开发过程中,对于ToggleButton的使用频率也是相当的高的,下面我就来
- 以在搜索框搜索时,自动补全为例:其中还涉及到一个词,Tokenizer:分词器,分解器。上效果图:MainActivity.java:pac
- 本文实例讲述了C#实现winform自动关闭MessageBox对话框的方法。分享给大家供大家参考。具体实现方法如下:using Syste
- 线上出现了如上的 crash,第一解决反应是在 show dialog 之前做个 isFinish 和 isDestroyed 判断,当我翻
- Android中实现拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据。 还有一种是自己用Camera类和其他相关类实现相机
- java中多种方式读文件 一、多种方式读文件内容。 1、按字节读取文件内容 2、按字符读取文件内容 3、按行读取文件内容 4、随机读取文件内
- 本文实例为大家分享了Java单例模式利用HashMap实现缓存数据的具体代码,供大家参考,具体内容如下一、单例模式是什么?单例模式是一种对象
- 在进行java编程的时候,我们可以生成可运行的jar文件,但是鉴于平台的不同,我们可能需要将jar文件转化为exe格式。今天,小编就用一款叫
- 介绍主要使用了goole的zxing包,下面给出了示例代码,很方便大家的理解和学习,代码都属于初步框架,功能有了,需要根据实际使用情况完善优
- import java.util.ArrayList;import java.util.List;public class Test2 {&
- 自定义starterSpringBoot中的starter是一种非常重要的机制,能够抛弃以前繁杂的配置,将其统一集成进 starter,应用
- java 制作验证码并进行验证实例详解在注册、登录的页面上经常会出现验证码,为了防止频繁的注册或登录行为。下面是我用java制作的一个验证码
- 在网上看到了一个IOS组件PendulumView,实现了钟摆的动画效果。由于原生的进度条确实是不好看,所以想可以自定义View实现这样的效
- 我们都知道EditText与TextView是Android的文本输入框和文本显示框,但是基于手机屏幕的大小因素,如果在需要输入较多文字或者
- 错误Mybatis-Plus (简称MP) 是mybatis的一个增强工具,在mybatis的基础上只做增强不做改变,简化了开发效率。其实就
- 最近刚完成一个简单的网络爬虫,开始的时候很迷茫,不知道如何入手,后来发现了很多的资料,不过真正能达到我需要,有用的资料--代码很难找。所以我