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

     注意:

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

    解决方式如下:

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    端口映射到公网工具
    C# app.config文件配置和修改
    C#基本知识点-Readonly和Const的区别
    C#知识点-StopWatch-计时
    python with open as f写中文乱码
    程序员不要去这样的公司
    关于老系统的重构和优化选择
    .Net Remoting 调用远程对象
    自定义的Config节点及使用
    前台线程和后台线程区别与使用技巧
  • 原文地址:https://www.cnblogs.com/YYkun/p/5703930.html
Copyright © 2011-2022 走看看