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

     注意:

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

    解决方式如下:

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    01 输出字符串中字符的所有组合
    04 Redis主从同步
    03 Redis发布与订阅
    02 Redis防止入侵
    01 Redis基础
    MySQL索引优化 笔记
    SQL 基础语句整理
    jstl用法 简介
    type=file 上传图片限制 类型和尺寸 方法
    js 判断图片和视频是否加载成功
  • 原文地址:https://www.cnblogs.com/YYkun/p/5703930.html
Copyright © 2011-2022 走看看