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;
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
2014-05-16 JSON入门实例
2014-05-16 WCF入门教程(vs2010)
2014-05-16 数据契约(DataContract)
2014-05-16 Guid.NewGuid()
2014-05-16 手动配置WCF宿主的.config文件遇到的几种错误