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库进来,然后才可以使用。

转载于:https://www.cnblogs.com/stemon/p/4617561.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/288450.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

exchange2013 owa-outlook界面语言

环境&#xff1a;Exchange 2013客户需求&#xff1a;在海外有分支机构&#xff0c;希望这些人的owa和outlook都是英文一般在登陆owa时&#xff0c;通过用户名和密码验证完成后会提示选择语言和时区&#xff0c;根据客户端的浏览器里面的语言会有一个默认选项英文系统中文系统Ou…

ADO.NET提供的Connection类总结

各种数据接口的连接方式 一、ADO连接 1、连接access数据库 set conn=Server.CreateObject("ADODB.Connection") conn.open "Driver={Microsoft Access Driver (*.mdb)};dbq="&Server.Mappath("dbname(路径全名)") 2、连接SQL Server数据库…

测试网速_使用Speedtest CLI测试你的网速

在本文中我们将使用一个名为speedtest-cli的命令行工具来测试网速。它是用Python语言编写的工具&#xff0c;系统通过向speedtest.net网站上传和下载数据来测试带宽。环境Centos7安装speedtest-cli工具在安装speedtest-cli之前&#xff0c;我们需要安装先安装python3-pip包管理…

回文数之一(大概)

若一个数&#xff08;首位不为0&#xff09;从左到右读与从右到左读都是一样&#xff0c;这个数就叫做回文数&#xff0c;例如12521就是一个回文数。 给定一个正整数&#xff0c;把它的每一个位上的数字倒过来排列组成一个新数&#xff0c;然后与原数相加&#xff0c;如果是回文…

C# IEnumerator IEnumerable接口

前言使用linq的时候大家应该都知道IEnumerable和IEnumerator接口&#xff01;解释一下IEnumerable 它利用 GetEnumerator() 返回 IEnumerator 集合访问器,声明实现该接口的class是“可枚举(enumerable)”的 通俗点说就是可进行迭代操作的类型。IEnumerator解释&#xff1a;它是…

linux中mpich的运行线程,贝叶斯法构建进化树:MrBayes

1. 简介使用贝叶斯法构建进化树的软件有很多。在这里简要介绍MrBayes的安装和使用。以下介绍是对几种贝叶斯法构建进化树软件的简介&#xff1a;MrBayes is a program for Bayesian inference and model choice across a wide range of phylogenetic and evolutionary models. …

linux c之gcc -g file.c -o file提示warning: no newline at end of file

1 问题 编译c文件的时候&#xff0c;命令如下 gcc -g file.c -o file 错误提示如下 warning: no newline at end of file 2 解决办法 原因&#xff1a;源文件的最后一行没有回车符造成 我们用vim编辑文件&#xff0c;然后到文件最后一行回车就行 vi和vim操作&#xff0…

php strtoupper 和 array_change_key_case 字符串转大写,小写

strtoupper &#xff1a;字符串转为大写。 array_change_key_case 则是把数组的所有的键转换为大写字母。 strtoupper&#xff1a; <?phpecho strtoupper("Hello world!"); 输出&#xff1a; HELLO WORLD! strtolower &#xff1a;字符串转为小写。 <?…

用Excel VBA连接SQL Server数据库并将表中的数据显示到Excel中

本实例使用VB语言和OLEDB类连接SQL Server 2005数据库。 在sheet1上右键→查看代码&#xff0c;加入如下代码&#xff1a; Sub admin()Dim conn, xRsSet conn CreateObject("ADODB.Connection")conn.Open "ProviderSQLOLEDB.1;Persist Security InfoFalse;Use…

filter与servlet对照

最近在开java物自&#xff0c;还记得刚开始使用servlet这是一个调试ajax什么时候&#xff0c;然后&#xff0c;我不知道怎么用&#xff0c;你知道写的路径来调用&#xff0c;总是提示404错&#xff0c;&#xff0c;到最后自己一点点的调通了&#xff0c;知道servlet是须要serve…

vc++ 提取网页上的文字_网页内容不让你复制?老师傅教的这招太绝了,1键提取全网文字...

在网上冲浪&#xff0c;看到了一些不错的文字&#xff0c;想要复制下来&#xff0c;可是网站就不让你复制&#xff0c;非要你注册登录账号&#xff0c;更有甚者要你购买VIP才能复制文字&#xff0c;这不是天方夜谭吗&#xff1f;今天老师傅就教你一招&#xff0c;学会了网页文字…

使用 C# 生成 EMF 矢量图形文件

前言公众号上有网友询问我如何生成 EMF 文件的问题&#xff1a;本以为非常简单&#xff0c;我快速给出了解决方案&#xff1a;var bitmap new Bitmap(640, 480);var g Graphics.FromImage(bitmap);g.DrawString("My IO", new Font(FontFamily.GenericSerif, 10), B…

php register_shutdown_function响应error 配合error_get_last 捕获错误

register_shutdown_function 函数,当我们的脚本执行完成或意外死掉导致PHP执行即将关闭时,我们的这个函数将会 被调用&#xff0c;可以配合 error_get_last 使用&#xff0c;代码如下&#xff0c;注释已经解释了代码了&#xff1a; error_get_last 稍后解释 <?php //a bo…

嵌入式开发linux工具,嵌入式Linux开发入门之MfgTool工具的使用

介绍嵌入式linux开发的环境搭建&#xff1a;一、PC端环境搭建2、Ubuntu装好后&#xff0c;在终端安装minicom工具&#xff0c;安装命令&#xff1a;sudo apt-get install minicom注&#xff1a;该工具在后面烧录开发板可以调试使用&#xff0c;对以后的嵌入式程序开发也是不可获…

SQL Server 2005 cmd工具的使用

您可以使用 sqlcmd 实用工具(Microsoft Win32 命令提示实用工具)来运行特殊的 Transact-SQL 语句和脚本。若要以交互方式使用 sqlcmd,或要生成可使用 sqlcmd 来运行的脚本文件,则需要了解 Transact-SQL。通常以下列方式使用 sqlcmd 实用工具: 在 sqlcmd 环境中,以交互的方…

剑指offer之按层打印树节点

1 问题 按层打印树节点&#xff0c;比如我们有树如下 23 5 1 4 2 3 这样打印&#xff1a;2 3 5 1 4 2 3 2 分析 队列&#xff1a;先进后出&#xff0c;这里我们先打印2&#xff0c;然后再打印3和5&#xff0c;我们这里可以使用队列&#xff0c;我们先把2入队…

基于key/value+Hadoop HDFS 设计的存储系统的shell命令接口

对于hadoop HDFS 中的全部命令进行解析&#xff08;当中操作流程是自己的想法有不允许见欢迎大家指正&#xff09; 接口名称 功能 操作流程 get 将文件拷贝到本地文件系统 。假设指定了多个源文件&#xff0c;本地目的端必须是一个文件夹。 &#xff08;1&#xff09;依照…

【足迹C++primer】52、,转换和继承虚函数

转换和继承,虚函数 Understanding conversions between base and derived classes is essential to understanding how object-oriented programming works in C. 理解基类和派生类之间的转换是不可缺少的 理解面向对象编程在。 Like built-in pointers, the smart pointer cla…

python人脸识别训练模型生产_深度学习-人脸识别DFACE模型pytorch训练(二)

首先介绍一下MTCNN的网络结构&#xff0c;MTCNN有三种网络&#xff0c;训练网络的时候需要通过三部分分别进行&#xff0c;每一层网络都依赖前一层网络产生训练数据供当前训练网络&#xff0c;这样也推动了两个网络之间的最小损耗。PnetRnetOnetMTCNN的人脸模型按照以上结构按照…

聊一聊Redis的离线分析

背景 Redis 或许是我们大部分场景都会用到的一个利器&#xff0c;虽然是利器&#xff0c;用的姿势不对的话&#xff0c;终究还是会整出幺蛾子的。比较常见的问题&#xff0c;不外乎内存暴增&#xff0c;慢查询等情况。那么对于内存问题&#xff0c;可以借助redis自带的bigkey分…