winform显示word、ppt和pdf,用一个控件显示
思路:都以pdf的格式展示,防止文件拷贝,所以要把word和ppt转换为pdf;展示用第三方组件O2S.Components.PDFView4NET.dll,破解版的下载链接:https://pan.baidu.com/s/18bsNnnaFFWiZdAqDIHVP4w 密码:c8x3。还有这个组件的官方实例,地址:https://pan.baidu.com/s/1BjetUgLCIv5DPN2u9tMz_w 密码:n1fy
1.首先word和ppt转换为pdf
注意:需引用
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;这个是引用Office
/// <summary>
/// Office2Pdf 将Office文档转化为pdf
/// </summary>
public class OfficeToPdf
{
/// <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;
if (File.Exists(targetPath))
{
result = true;
return result;
}
string targetDic = Path.GetDirectoryName(targetPath);
if (!Directory.Exists(targetDic) && targetDic!="")
{
Directory.CreateDirectory(targetDic);
}
Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
object paramMissing = Type.Missing;
Word.Application wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
catch (Exception ex)
{
result = false;
throw new ApplicationException(ex.Message);
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
/// <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;
if (File.Exists(targetPath))
{
result = true;
return result;
}
string targetDic = Path.GetDirectoryName(targetPath);
if (!Directory.Exists(targetDic))
{
Directory.CreateDirectory(targetDic);
}
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.ApplicationClass application = null;
Excel.Workbook workBook = null;
try
{
application = new Excel.ApplicationClass();
object target = targetPath;
object type = targetType;
workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
workBook.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 (workBook != null)
{
workBook.Close(true, missing, missing);
workBook = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
///<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;
if (File.Exists(targetPath))
{
result = true;
return result;
}
string targetDic = Path.GetDirectoryName(targetPath);
if (!Directory.Exists(targetDic))
{
Directory.CreateDirectory(targetDic);
}
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
PowerPoint.ApplicationClass application = null;
PowerPoint.Presentation persentation = null;
try
{
application = new PowerPoint.ApplicationClass();
persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse); persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch (Exception ex)
{
result = false;
throw new ApplicationException(ex.Message);
}
finally
{
if (persentation != null)
{
persentation.Close();
persentation = null;
}
if (application != null)
{
application.Quit();
application = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}
2.展示
我做成个用户控件,方便以后使用。把拖到工具箱中,用里面的pdfPageView和pdfDocument控件,pdfPageView用于展示,pdfDocument是关联显示的文件
界面:
后台代码:
public partial class FrmPDFShow : UserControl
{
public PDFDocument PdfDocument;
public PDFPageView PdfPageView;
public FrmPDFShow()
{
InitializeComponent();
PdfDocument = this.pdfDocument;
PdfPageView = this.pdfPageView;
}
public string LoadFilePath
{
set
{
LoadFile(value);
}
}
//加载pdf文件
private void LoadFile(string filepath)
{
try
{
string pdfPath = Path.GetDirectoryName(filepath) + "\\" + Path.GetFileNameWithoutExtension(filepath) + ".pdf";
switch (Path.GetExtension(filepath).ToLower())
{
case ".doc":
OfficeToPdf.DOCConvertToPDF(filepath, pdfPath);
break;
case ".xls":
OfficeToPdf.XLSConvertToPDF(filepath, pdfPath);
break;
case ".ppt":
OfficeToPdf.PPTConvertToPDF(filepath, pdfPath);
break;
}
pdfDocument.Load(pdfPath);
}
catch (Exception ex)
{
MessageBox.Show("转换pdf错误"+ex.Message);
}
}
private void tbtnZoomFitHeight_Click_1(object sender, EventArgs e)
{
pdfPageView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.FitHeight;
}
//
private void tbtnZoomFitWidth_Click(object sender, EventArgs e)
{
pdfPageView.ZoomMode = O2S.Components.PDFView4NET.PDFZoomMode.FitWidth;
}
//缩小
private void tbynZoomOut_Click(object sender, EventArgs e)
{
pdfPageView.WorkMode = O2S.Components.PDFView4NET.UserInteractiveWorkMode.ZoomOut;
}
//放大
private void tbtnZoomIn_Click(object sender, EventArgs e)
{
pdfPageView.WorkMode = O2S.Components.PDFView4NET.UserInteractiveWorkMode.ZoomIn;
}
//鼠标拖动文件
private void tbtnPanScan_Click(object sender, EventArgs e)
{
this.pdfPageView.Cursor = Cursors.Hand;
pdfPageView.WorkMode = UserInteractiveWorkMode.PanAndScan;
}
}
winform显示word、ppt和pdf,用一个控件显示的更多相关文章
- Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php
Atitit.dwr3 不能显示错误具体信息的解决方式,控件显示错误具体信息的解决方式 java .net php 1. Keyword/subtitle 1 2. 使用dwr3的异常convert处 ...
- C# 鼠标移动Winform窗体内或者panel容器内的控件 显示虚线/实现虚线框来确定位置
C# 鼠标移动WinForm窗体或者panel容器内的控件 移动虚线/实现虚线框来确定位置 1.用到的方法介绍 今天,根据领导指示指导移动容器内的控件,生成虚线框,使用 ControlPaint.Dr ...
- Aspose office (Excel,Word,PPT),PDF 在线预览
前文: 做个备份,拿的是试用版的 Aspose,功能见标题 代码: /// <summary> /// Aspose office (Excel,Word,PPT),PDF 在线预览 // ...
- 使用 WebView2 封装一个生成 PDF 的 WPF 控件
使用 WebView2 封装一个生成 PDF 的 WPF 控件 最近在迁移项目到 .net6,发现项目中用的 PDF 库不支持 .net6,于是想着换一个库.结果找了一大圈,发现不是版本不支持,就是收 ...
- [C#]Winform下回车或Tab键自动切换下一个控件焦点
满足用户体验,在数据录入时,能在输入完一个信息后通过回车或Tab键自动的切换到下一个控件(字段). 在界面控件设计时,默认可以通过设置控件的TabIndex来实现.但在布局调整时或者是对输入的内容有选 ...
- winform中如何在多线程中更新UI控件--ListView实时显示执行信息
1.在winform中,所有对UI的操作,都得回到UI线程(主线程)上来,才不会报错 线程间操作无效: 从不是创建控件的线程访问它. 2.在winform中,允许通过Control.invoke对控件 ...
- winform快速开发平台 -> 快速绑定ComboBox数据控件
通常我们在处理编辑窗体时.往往会遇到数据绑定.例如combobox控件绑定数据字典可能是我们经常用到的.然而在我的winform快速开发平台中我是如何处理这个频繁的操作呢? 首先,我们要绑定combo ...
- ADO.NET之使用DataGridView控件显示从服务器上获取的数据
今天回顾下ADO.NET中关于使用DataGridiew控件显示数据的相关知识 理论整理: 使用 DataGridView 控件,可以显示和编辑来自多种不同类型的数据源的表格数据. SqlDataAd ...
- 解决Raize日历控件显示的问题
解决Raize日历控件显示的问题 近自己的程序被测试人员发现一个小问题,就是程序中的日历选择框,显示中的“星期一.星期二....”都显示成了“星.....”,我自己看了代码,原来是raize的控件问题 ...
随机推荐
- Tensorflow Mask-RCNN(三)——实时 检测视频
参考:https://www.youtube.com/watch?v=lLM8oAsi32g import cv2 import numpy as np def ran ...
- Oracle 基础1
oracle基础 表空间: Oracle数据库对数据的管理是基于表空间的概念来的, 各种数据的以及存储数据的优化, 实际上也是通过优化表空间来实现的 表空间分类: 永久表空间 用来存放表的数据, 视图 ...
- GRUB&MBR引导
(ubuntu下搜索gnome-disk可以打开磁盘管理) 简单开机过程 : ①按下电源后,计算机自检(POST),如果硬件设备(CPU.内存.硬盘.光驱.各种卡)都没有问题,BIOS会检查各个硬盘的 ...
- 微信小程序自定义分享封面
onShareAppMessage:function(options){ let thas = this; if (options.from === 'button') { // 来自页面内转发按钮 ...
- 游戏引擎UE4详解!
UE4 的全名是 Unreal Engine 4,中文译为“虚幻引擎4”.UE4 是一款由 Epic Games 公司开发的开源.商业收费.学习免费的游戏引擎.那你了解UE4吗?如果还不清楚,就一起来 ...
- 吴裕雄--天生自然MySQL学习笔记:MySQL 选择数据库
连接到 MySQL 数据库后,可能有多个可以操作的数据库,所以你需要选择你要操作的数据库. 从命令提示窗口中选择MySQL数据库 在 mysql> 提示窗口中可以很简单的选择特定的数据库.可以使 ...
- UML-GRASP前5种模式
1.创建者(Creator) 问题:谁创建类A? 答:来自领域模型.设计模型(交互图.类图) 2.信息专家 问题:给对象分配职责的基本原则是什么? 回答:谁具有完成该职责的信息,谁负责该职责. 因为根 ...
- 基于python的arcgis底图添加(转)
本文翻译自:Qingkai‘s Blog 当使用python的Basemap库绘制地图时,选择一个漂亮的底图会为图片增色不少,但是使用map.bluemarble().map.etopo()或者map ...
- ccs-基础-阴影
1.html代码 <div class="demo demo1">假如生活欺骗了你</div> <div class="demo demo2 ...
- WAMP常用环境配置
自定义网站目录 修改目录位置 如下图,打开httpd.conf文件. 查找DocumentRoot(两处),做如下修改: #demo为自定义网站目录,下面不再说明 DocumentRoot " ...