C# 保存word到pdf或xps

/// <summary>
    /// 保存word到pdf或xps
    /// </summary>
    public class FileSaveAs
    {
        /// <summary>
        /// 目标文件格式(pdf或xps)
        /// </summary>
        public enum TarFileFormat
        {
            PDF,//PDF格式
            XPS//XPS格式
        }

        #region 保存word到pdf或xps
        /// <summary>
        /// 保存word到pdf或xps
        /// </summary>
        /// <param name="paramSourceDocPath">word文件路径(例如:"C:\\SourceWord.doc")</param>
        /// <param name="paramExportFilePath">目标文件的路径(例如:"C:\\TargetFile.pdf")</param>
        /// <param name="fileFormat">文件格式</param>
        private static void WordSave(object paramSourceDocPath, string paramExportFilePath, TarFileFormat fileFormat)
        {
            Word.Application wordApplication = new Word.Application();
            Word.Document wordDocument = new Word.Document();
            object paramMissing = Type.Missing;
            Word.WdExportFormat paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;
            bool paramOpenAfterExport = false;
            Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
            Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;

            //格式
            switch (fileFormat)
            {
                case TarFileFormat.PDF:
                    {
                        paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;
                    }
                    break;
                case TarFileFormat.XPS:
                    {
                        paramExportFormat = Word.WdExportFormat.wdExportFormatXPS;
                    }
                    break;
                default:
                    {
                        paramExportFormat = Word.WdExportFormat.wdExportFormatPDF;
                    }
                    break;
            }

            try
            {
                // Open the source document.
                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);

                // Export it in the specified format.
                if (wordDocument != null)
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                        paramExportFormat, paramOpenAfterExport,
                        paramExportOptimizeFor, paramExportRange, paramStartPage,
                        paramEndPage, paramExportItem, paramIncludeDocProps,
                        paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                        paramBitmapMissingFonts, paramUseISO19005_1,
                        ref paramMissing);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                // Close and release the Document object.
                if (wordDocument != null)
                {
                    wordDocument.Close(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordDocument = null;
                }

                // Quit Word and release the ApplicationClass object.
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing,
                        ref paramMissing);
                    wordApplication = null;
                }

                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        /// <summary>
        /// 保存word到pdf或xps
        /// </summary>
        /// <param name="paramSourceDocPath">word文件路径(例如:"C:\\SourceWord.doc")</param>
        /// <param name="paramExportFilePath">目标文件的路径(例如:"C:\\TargetFile.pdf")</param>
        /// <param name="fileFormat">文件格式</param>
        /// <returns>true,成功;false,失败</returns>
        public static bool WordSaveAs(object paramSourceDocPath, string paramExportFilePath, TarFileFormat fileFormat)
        {
            bool isSuccess = false;
            try
            {
                WordSave(paramSourceDocPath, paramExportFilePath, fileFormat);
                isSuccess = true;
            }
            catch (System.Exception ex)
            {
                //save error;
            }

            return isSuccess;
        }
        #endregion
    }

知识共享许可协议
《C# 保存word到pdf或xps》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     157.24ms