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;
}

}

 

posted @   卡萨丁·周  阅读(3081)  评论(6编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
历史上的今天:
2014-05-16 JSON入门实例
2014-05-16 WCF入门教程(vs2010)
2014-05-16 数据契约(DataContract)
2014-05-16 Guid.NewGuid()
2014-05-16 手动配置WCF宿主的.config文件遇到的几种错误
点击右上角即可分享
微信分享提示