zoukankan      html  css  js  c++  java
  • winform实现word转换为PDF(.doc)

    注意:实现word转换为PDF文件,本人安装Office为2013;

    word以后缀为.doc为例实现文件类型转换,具体方式如下所示:

    实现步骤:

      1.添加命名空间引用——using Microsoft.Office.Interop.Word;

      2.添加WordConvertPdf方法——方法实现请阅读文件后续内容

      3.WordConvertPdf方法的使用

    详细如下所示;

    2.添加WordConvertPdf方法==》

    ==》

    private bool WordConvertPdf(string sourcePath, string targetPath, WdExportFormat exportFormat)
    {
    bool result = false;
    object paramMissing = Type.Missing;
    Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();
    Document wordDocument = null;
    try
    {
    object paramSourceDocPath = sourcePath;
    string paramExportFilePath = targetPath;

    WdExportFormat paramExportFormat = exportFormat;
    bool paramOpenAfterExport = false;
    WdExportOptimizeFor paramExportOptimizeFor =
    WdExportOptimizeFor.wdExportOptimizeForPrint;
    WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
    int paramStartPage = 0;
    int paramEndPage = 0;
    WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
    bool paramIncludeDocProps = true;
    bool paramKeepIRM = true;
    WdExportCreateBookmarks paramCreateBookmarks =
    WdExportCreateBookmarks.wdExportCreateWordBookmarks;
    bool paramDocStructureTags = true;
    bool paramBitmapMissingFonts = true;
    bool paramUseISO19005_1 = false;

    wordDocument = wordApplication.Documents.OpenNoRepairDialog(
    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
    {
    return false;
    }
    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();
    GC.Collect();
    GC.WaitForPendingFinalizers();
    }
    return result;
    }

    3.WordConvertPdf方法的使用

    ==》

     OpenFileDialog opg = new OpenFileDialog();
     opg.Filter = "word(*.doc)|*.doc";
     if (opg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
      WordConvertPdf(opg.FileName, pp, WdExportFormat.wdExportFormatPDF);
    }

     注意:

    如果出现如下图所示异常:

    解决方式如下:

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    6-4.粗体标签
    [Unity3D] 如何实现点击按钮退出游戏
    [Unity3D] 载入游戏地图时背景图片随机切换 & 数字百分比进度条
    [Unity3D] 鼠标点击图片移动效果
    [3DMAX]如何将骨骼与模型绑定在一起(蒙皮) & 如何实现自动化人物模型蒙皮
    [Unity 3D]用鼠标滚轮实现镜头放大和缩放,并添加距离限制
    [Unity3D] 如何实现围绕旋转
    [Unity3D] 如何实现注视旋转
    Css 图片自适应
    Scss 定义内层class的简单写法
  • 原文地址:https://www.cnblogs.com/YYkun/p/5703930.html
Copyright © 2011-2022 走看看