zoukankan      html  css  js  c++  java
  • 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;
    }

    }

  • 相关阅读:
    【SQL】语句/函数汇总
    【SVN】提交报错:×××文件is not under version control
    【AnjularJS系列1】样式相关的指令
    【Javescript】DOM(文档对象模型)
    【HTML】框架集(Framesets)
    【HTML】<!DOCTYPE html>作用
    vue2.0移动端自定义性别选择提示框
    微信小程序踩坑记
    网页里如何使用js禁用F12事件
    网页里如何使用js屏蔽鼠标右击事件
  • 原文地址:https://www.cnblogs.com/dachuang/p/9046509.html
Copyright © 2011-2022 走看看