C#操作Office.word(三)

前面两篇博客讲解了怎么通过程序控制word的生成,包括生成文字、添加表格、添加图片等。这篇博客主要说一下怎么把word图片转换成pdf。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Core;

namespace PDFTest
{
    class PDFUtil
    {
        /// <summary>
        /// 把Word文件转换成为PDF格式文件
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public static bool WordToPDF(string sourcePath, string targetPath)
        {
            bool result = false;
            Microsoft.Office.Interop.Word.WdExportFormat exportFormat = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
            Microsoft.Office.Interop.Word.ApplicationClass application = null;

            Microsoft.Office.Interop.Word.Document document = null;
            try
            {
                application = new Microsoft.Office.Interop.Word.ApplicationClass();
                application.Visible = false;
                document = application.Documents.Open(sourcePath);
                document.SaveAs2();
                document.ExportAsFixedFormat(targetPath, exportFormat);
                result = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = false;
            }
            finally
            {
                if (document != null)
                {
                    document.Close();
                    document = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }

        /// <summary>
        /// 把Microsoft.Office.Interop.Excel文件转换成PDF格式文件
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        //public static bool ExcelToPDF(string sourcePath, string targetPath)
        //{
        //    bool result = false;
        //    Microsoft.Office.Interop.Excel.XlFixedFormatType targetType = Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF;
        //    object missing = Type.Missing;
        //    Microsoft.Office.Interop.Excel.ApplicationClass application = null;
        //    Microsoft.Office.Interop.Excel.Workbook workBook = null;
        //    try
        //    {
        //        application = new Microsoft.Office.Interop.Excel.ApplicationClass();
        //        application.Visible = false;
        //        workBook = application.Workbooks.Open(sourcePath);
        //        workBook.SaveAs();
        //        workBook.ExportAsFixedFormat(targetType, targetPath);
        //        result = true;
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine(e.Message);
        //        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">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        //public static bool PowerPointToPDF(string sourcePath, string targetPath)
        //{
        //    bool result;
        //    Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType targetFileType = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
        //    object missing = Type.Missing;
        //    Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
        //    Microsoft.Office.Interop.PowerPoint.Presentation persentation = null;
        //    try
        //    {
        //        application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();
        //        //application.Visible = MsoTriState.msoFalse;
        //        persentation = application.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
        //        persentation.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);

        //        result = true;
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine(e.Message);
        //        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>
        /// 把Visio文件转换成PDF格式文件
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        //public static bool VisioToPDF(string sourcePath, string targetPath)
        //{
        //    bool result;
        //    Microsoft.Office.Interop.Visio.VisFixedFormatTypes targetType = Microsoft.Office.Interop.Visio.VisFixedFormatTypes.visFixedFormatPDF;
        //    object missing = Type.Missing;
        //    Microsoft.Office.Interop.Visio.ApplicationClass application = null;
        //    Microsoft.Office.Interop.Visio.Document document = null;
        //    try
        //    {
        //        application = new Microsoft.Office.Interop.Visio.ApplicationClass();
        //        application.Visible = false;
        //        document = application.Documents.Open(sourcePath);
        //        document.Save();
        //        document.ExportAsFixedFormat(targetType, targetPath, Microsoft.Office.Interop.Visio.VisDocExIntent.visDocExIntentScreen, Microsoft.Office.Interop.Visio.VisPrintOutRange.visPrintAll);
        //        result = true;
        //    }
        //    catch (Exception e)
        //    {
        //        Console.WriteLine(e.Message);
        //        result = false;
        //    }
        //    finally
        //    {
        //        if (application != null)
        //        {
        //            application.Quit();
        //            application = null;
        //        }
        //        GC.Collect();
        //        GC.WaitForPendingFinalizers();
        //        GC.Collect();
        //        GC.WaitForPendingFinalizers();
        //    }
        //    return result;
        //}

    }
}

程序中包含了各种office格式转换pdf的代码,在使用之前,一定要加载对应的COM库进来,然后才可以使用。

时间: 2024-08-10 12:49:18

C#操作Office.word(三)的相关文章

C#操作Office.word(二)

在上一篇文章"C#操作Office.word(一)"中我们讲述了如何使用VS2010引用COM中Miscrosoft Word 14.0 Object Library实现创建文档,而这篇文章将讲述如何添加表格和图片,因为我在C#联系数据库做销售系统中需要打印表单,,我想以图表形式显示在word中,同时生成相应的饼状图或柱状图,所以才有查阅了相关资料,完成文章,供大家分享.其中使用openFileDialog控件也是希望大家学习了解下. 一. 界面设置 在界面上增加一个按钮,点击这个按钮

章鱼哥出品—VB.NET Office操作之Word(四)

本文是在 章鱼哥出品-VB.NET Office操作之Word(二)中添加内容的具体实现,读者可以借鉴看下,注意本文应该与三结合在一起使用,是在三的基础上添加了几种功能的实现. 实现窗体: 代码实现:代码直接复制到上文的窗体类中 '********************************************************************* '作者:章鱼哥,QQ:3107073263 群:309816713     '如有疑问或好的建议请联系我,大家一起进步   '*

Office操作:Word一分钟制作表格

表格在我们的日常工作中是必不可少的,Word对表格的制作提供了很好的支持,那么如何在Word上快速建立一个合适的表格呢?表格制作又需要涉及到哪些技巧呢?这里和大家一同了解表格制作的步骤.只需要通过简单的几个步骤即可实现表格的插入操作: 表格制作的方法: 第一种方法适合制作最简单的表格: 首先打开word文档,直接点击"插入"菜单下方的"表格".第一个插入表格,鼠标放下去自然就会出现表格,按需要选择行列的数量便可以得到自己想要的表格了. Office操作:Word一分

章鱼哥—VB.NET Office操作之Word(二)

这篇文章在 章鱼哥-VB.NET Office操作之Word(一) 的基础上,给类添加了光标操作的内容,包括获取word 文档中光标的位置,将光标跳至指定行,上下左右移动光标等操作.该文中给定的方法直接复制到上篇文中的类中即可.不懂得可以联系我.下篇文章将给出这个类具体实现的代码,所有代码都经过笔者的测试,可直接使用. '********************************************************************* '作者:章鱼哥,QQ:3107073

Atitit.office&#160;word&#160;&#160;excel&#160;&#160;ppt&#160;pdf&#160;的web在线预览方案与html转换方案&#160;attilax&#160;总结

Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求1 1.1. 显示效果要好1 1.2. 可以自定义显示界面1 1.3. 不需要控件,兼容性好1 1.4. 支持编辑操作1 2. 纯html预览解决之道(自由的格式)1 3. 转换swf flash方案2 4. 转换pdf方式..更多的浏览器已经直接支持pdf格式查看2 5. 控件方式2 6. Hyb

使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结

[超详细教程]使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 去年就知道有这个功能,不过没去深究总结过,最近有写网络博客的欲望了,于是又重新拾起这玩意儿. 具体到底是用Windows Live Writer 2012还是用Word 2013,个人觉得看个人,因为这2个软件各有优点,各有缺点. 1.首先用LiveWriter发博客显然更专业,发布后的效果也与本地最接近,但是在编辑功能上肯定大不如Word,另外一个最大缺点是它本地保存

DCOM 找不到 office word 的解决方法

1. 在运行里面  输入     comexp.msc -32 2.在“DCOM配置”中,为IIS账号配置操作Word(其他Office对象也一样)的权限. 具体操作:“组件服务(Component Service)”->计算机(Computers)->我的电脑(My Computer)->DCOM配置(DCOM Config)->Microsoft Office Word 97 - 2003 文档,右击“Microsoft Office Word 97 - 2003 文档”,选择

Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑",如果是两个字那就是"巨坑"=>因为这个需求一开始并不是这样子的,且听我漫漫道来: 一开始客户与我们商量的是将office和PDF上传,将此类文件解析成html格式,在APP端调用内置server直接以html"播放" 经历一个月~,两个月~,三个月~~~

Office Word 2013 的十大优点1

Office Word 2013 的十大优点 1.发现改进的搜索和导航体验. 利用 Word 2013,可更加便捷地查找信息.现在,利用新增的改进查找体验,您可以按照图形.表.脚注和注释来查找内容.改进的导航窗格为您提供了文档的直观表示形式,这样就可以对所需内容进行快速浏览.排序和查找. 2.与他人同步工作. Word 2013 重新定义了人们一起处理某个文档的方式.利用共同创作功能,您可以编辑论文,同时与他人分享您的思想观点.1 对于企业和组织来说,与 Office Communicator