• C#基于Microsoft.Office.Interop.Word.dll组件实现Word转PDF功能
  • 发布于 2个月前
  • 562 热度
    0 评论
  • 浅歌
  • 0 粉丝 33 篇博客
  •   

前言:

最近在开发一个医疗病历管理系统,其中有个功能是需要把word格式的病历转换成PDF格式的病历。网上关于word文件转PDF格式的实现方式有很多种,我这边用的是Microsoft.Office.Interop.Word.dll组件来实现的。用这个方法的话需要在服务器上安装office组件,我安装的是我安装的是2010版本的。

word转PDF代码:

/// <summary>
/// 堆代码 duidaima.com
/// 把Word文件转换成为PDF格式文件
/// 日期:2023-04-17
/// </summary>
/// <param name="sourcePath">源文件路径</param>
/// <param name="targetPath">目标文件路径</param>
/// <returns>true=转换成功</returns>
public static bool DOCConvertToPDF(string sourcePath, string targetPath,out string strErrMsg)
{
    bool result = false;
    strErrMsg = "";
    Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
    object paramMissing = Type.Missing;
     
    try
    {
        Microsoft.Office.Interop.Word.ApplicationClass wordApplication = new Microsoft.Office.Interop.Word.ApplicationClass();            
        Microsoft.Office.Interop.Word.Document wordDocument = null;            
     
        object paramSourceDocPath = sourcePath;
        string paramExportFilePath = targetPath;
         
        Microsoft.Office.Interop.Word.WdExportFormat paramExportFormat = exportFormat;
        bool paramOpenAfterExport = false;
        Microsoft.Office.Interop.Word.WdExportOptimizeFor paramExportOptimizeFor = Microsoft.Office.Interop.Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
        Microsoft.Office.Interop.Word.WdExportRange paramExportRange = Microsoft.Office.Interop.Word.WdExportRange.wdExportAllDocument;
        int paramStartPage = 0;
        int paramEndPage = 0;
        Microsoft.Office.Interop.Word.WdExportItem paramExportItem = Microsoft.Office.Interop.Word.WdExportItem.wdExportDocumentContent;
        bool paramIncludeDocProps = true;
        bool paramKeepIRM = true;
        Microsoft.Office.Interop.Word.WdExportCreateBookmarks paramCreateBookmarks = Microsoft.Office.Interop.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);
 
        if (wordDocument != null)
        {
            wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDocument);
            wordDocument = null;
        }
         
        if (wordApplication != null)
        {
            wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApplication);
            wordApplication = null;
        }
         
        result = true;
    }
    catch(Exception ex)
    {
        strErrMsg = ex.Message;
        result = false;
    }
    finally
    {                
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
    }
    return result;
     
}

总结:

以上就是基于office组件实现的word转PDF代码,基于这种方式的word转PDF功能比较完整,但是需要在服务器上安装office组件,部署起来稍微麻烦点。

用户评论