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

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM