C# Word转换成PDF,Excel转换PDF,PowerPoint转换PDF,OneNote转换PDF

nd | | 访问(1590)

  C# Microsoft类库, Word转换成PDF方法,Excel转换PDF方法,PowerPoint转换PDF方法,OneNote转换PDF方法

  这种方式存在局限性,但能按照Office文档的原始格式转换成PDF而不会产生变形问题。

  Word转换成PDF方法 下载Aspose.Words.dll破解版(推荐方法)

using System;
using Microsoft.Office.Core;//引用Offile.dll
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using OneNote = Microsoft.Office.Interop.OneNote;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Interop.OneNote;

namespace Office.Util
{
    public class Converter
    {

        /// <summary>
        /// Word转换成PDF方法 下载Aspose.Words.dll破解版(推荐方法)
        /// </summary>
        /// <param name="sourcePath">Word文件物理路径+文件名称</param>
        /// <param name="targetPath">保存成PDF的物理路径+文件名称.pdf</param>
        /// <returns></returns>
        public static bool ConvertWordToPdf(string sourcePath, string targetPath)
        {
            try
            {
                Aspose.Words.Document dstDoc = new Aspose.Words.Document(sourcePath);
                dstDoc.Save(targetPath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Word转换成PDF方法
        /// </summary>
        /// <param name="sourcePath">Word文件物理路径+文件名称</param>
        /// <param name="targetPath">保存成PDF的物理路径+文件名称.pdf</param>
        /// <returns></returns>
        public static bool ConvertWordToPdf(string sourcePath, string targetPath)
        {
            bool result;
            object paramMissing = Type.Missing;
            Word.ApplicationClass wordApplication = new Word.ApplicationClass();
            Word.Document wordDocument = null;
            try
            {
                object paramSourceDocPath = sourcePath;
                string paramExportFilePath = targetPath;

                Word.WdExportFormat paramExportFormat = 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;

                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.Password = pwd;//跟word文档加密
                    wordDocument.ExportAsFixedFormat(paramExportFilePath,
                            paramExportFormat, paramOpenAfterExport,
                            paramExportOptimizeFor, paramExportRange, paramStartPage,
                            paramEndPage, paramExportItem, paramIncludeDocProps,
                            paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
                            paramBitmapMissingFonts, paramUseISO19005_1,
                            ref paramMissing);

                }
                result = true;
            }
            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;
        }

        /// <summary>
        /// Excel转换PDF方法
        /// </summary>
        /// <param name="sourcePath">Excel文件物理路径+文件名称</param>
        /// <param name="targetPath">保存成PDF的物理路径+文件名称.pdf</param>
        /// <returns></returns>
        public static bool ConvertExcelToPdf(string sourcePath, string targetPath)
        {
            bool result;
            object missing = Type.Missing;
            Excel.ApplicationClass application = null;
            Excel.Workbook workBook = null;
            Excel.XlFixedFormatType targetType = XlFixedFormatType.xlTypePDF;
            try
            {
                application = new Excel.ApplicationClass();
                object target = targetPath;
                object type = targetType;
                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                        missing, missing, missing, missing, missing, missing, missing, missing, missing);

                workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        /// <summary>
        /// PowerPoint转换PDF方法
        /// </summary>
        /// <param name="sourcePath">PowerPoint文件物理路径+文件名称</param>
        /// <param name="targetPath">保存成PDF的物理路径+文件名称.pdf</param>
        /// <returns></returns>
        public static bool ConvertPowerPointToPdf(string sourcePath, string targetPath)
        {
            bool result;
            object missing = Type.Missing;
            PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new PowerPoint.ApplicationClass();
                persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
                persentation.SaveAs(targetPath, PpSaveAsFileType.ppSaveAsPDF, Microsoft.Office.Core.MsoTriState.msoTrue);

                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        /// <summary>
        /// OneNote转换PDF方法
        /// </summary>
        /// <param name="sourcePath">OneNote文件物理路径+文件名称</param>
        /// <param name="targetPath">保存成PDF的物理路径+文件名称.pdf</param>
        /// <returns></returns>
        public static bool ConvertOneNoteToPdf(string sourcePath, string targetPath)
        {
            bool result = false;
            string sectionID;
            OneNote.Application application = new OneNote.Application();
            try
            {
                application.OpenHierarchy(sourcePath, System.String.Empty, out sectionID);
                application.Publish(sectionID, targetPath, PublishFormat.pfWord);
                application.CloseNotebook(sectionID);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (application != null)
                {
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
    }
}