zoukankan      html  css  js  c++  java
  • word 转换成 pdf

             /// <summary>
            /// 把word文件变成PDF文件(这个是单个word文件变成pdf格式)
            /// </summary>
            /// <param name="sourceFileName">源文件</param>
            /// <param name="targetFileName">目标文件</param>
            /// <returns></returns>
            public bool ExportToPdf(string sourceFileName, string targetFileName)
            {
                docApplication = new Microsoft.Office.Interop.Word.Application();
                try
                {
    if (!File.Exists(sourceFileName))
                    {
                        _message = "需要转换PDF格式的源文件不存在!";
                        //CommonLibrary.Logger.Trace("当前进度:转换pdf的源word文档不存在!");
                        return false;
                    }
                    if (File.Exists(targetFileName))
                    {
                        File.Delete(targetFileName);
                    }
                    Word.Document document = null;
                    object missing = System.Reflection.Missing.Value;
                    object FileName = sourceFileName;
                    object readOnly = false;
                    object isVisible = false;                
                    //doc = ExOffice.OpenDocument(docApplication, fileName, false, false);
                    document = docApplication.Documents.Open(ref FileName, ref missing, ref readOnly,
                       ref missing, ref missing, ref missing, ref missing, ref missing,
                       ref missing, ref missing, ref missing, ref isVisible, ref missing,
                       ref missing, ref missing, ref missing);
    
                    string version = document.Application.Version;
                    version = version.Substring(0, version.IndexOf('.'));
                    if (int.Parse(version) >= 12)//版本号是12或者以上
                    {
                        object FixedFormatExtClassPtr = System.Type.Missing;
                        document.ExportAsFixedFormat(targetFileName,
                        Word.WdExportFormat.wdExportFormatPDF,
                        false,
                        Word.WdExportOptimizeFor.wdExportOptimizeForOnScreen,
                        Word.WdExportRange.wdExportAllDocument,
                        0,
                        0,
                        Word.WdExportItem.wdExportDocumentWithMarkup,
                        true,
                        true,
                        Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks,
                        true,
                        true,
                        true,
                        ref FixedFormatExtClassPtr);
                        _message = "转换成功!";
                        return true;
    
                    }
                    else
                    {
                        _message = "word版本太低,不能转换PDF格式!";
                        return false;
                    }
    
                }
                catch (System.Runtime.InteropServices.COMException exception)
                {
                    _message = exception.Message;
                    //UE.Library.FrmMessage.Show(exception.Message+"您可能没有安装word转pdf的插件!");
                    CommonLibrary.Logger.Trace(exception);
                    return false;
                }
                catch (Exception ex)
                {
                    //UE.Library.FrmMessage.Show(ex.Message);
                    _message = ex.Message;
                    CommonLibrary.Logger.Trace(ex);
                    return false;
                }
                finally
                {
                    if(docApplication!=null)//因为单击取消按钮,此方法还是会执行完,但是此时docApplication已经释放了,所以要加此if
                    {
                        if(!hasCancel)
                        {                        
                            ReleaseWordResource();
                        }
                    }
                }            
            }
  • 相关阅读:
    Git 命令 stash cherry-pick reset rebase
    【操作系统】不同语言的线程实现机制对比及数据库锁问题
    【数据结构】搜索二叉树(BST)和普通二叉树的序列化与反序列化
    【自制编译器】读书笔记 -- JavaCC使用的JJ文件格式
    leetcode 874 Robot Simulation
    hihocoder 编程挑战赛75
    浪漫主义的起源--以赛亚柏林
    【美团笔试 2018-4-20 】编程题-1 测例100%通过 欧拉函数求解gcd
    【POJ SOJ HDOJ】HDU 2196 Computer 树中的最长路径
    【Java 核心】多线程笔记
  • 原文地址:https://www.cnblogs.com/chengjunwei/p/2725566.html
Copyright © 2011-2022 走看看