C# PPT Operator
来自:http://blog.csdn.net/lxzh12345/article/details/47047491
最近在写一个工具,设计到将界面内容到处到PPT中,且导出的内容能够编辑。网上搜了很多C#导出到PPT的方法,无非都是官方文档稍微改改到处传。因此结合MSDN的文档外加自己的摸索,将对PPT的操作封装了一下,里面包含几个常用的方法:添加文本框、直线、箭头、矩形、图片。后面有机会再继续扩展。
注:这里只给出了封装的类,直接使用可能会有问题,记得添加Office2007对应组件的引用。
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Office.Core;
- using PPT = Microsoft.Office.Interop.PowerPoint;
- using System.Windows;
- using System.Collections;
- using System.Windows.Forms;
- using System.IO;
- using System.Reflection;
- using Tools.functionModel.file;
- using System.Drawing;
- using System.Drawing.Imaging;
- //using System.Windows.Controls;
- namespace Tools.baseModel.common {
- /// <summary>
- /// PPT文档操作实现类.
- /// </summary>
- public class pptBase {
- private string temPath = "";
- private string pptPath = "";
- #region=========基本的参数信息=======
- PPT.Application pptApp; //PPT应用程序变量
- public PPT.Application PptApp {
- get { return pptApp; }
- set { pptApp = value; }
- }
- PPT.Presentation pptDoc; //PPT文档变量
- PPT.Slides pptSlides = null;
- PPT.Slide pptSlide = null;
- private int pageCount=0;
- #endregion
- public PPT.Shapes Shapes {
- get { return pptSlide.Shapes; }
- }
- public pptBase(string path) {
- this.temPath = commonPath.fiberFolder + "/other/template.pot";
- this.pptPath = path;
- //如果已存在,则删除
- if (File.Exists((string)pptPath)) {
- File.Delete((string)pptPath);
- }
- FileInfo file = new FileInfo(this.temPath);
- pptApp = new PPT.Application(); //初始化
- pptApp.Visible = MsoTriState.msoTrue;
- pptDoc = pptApp.Presentations.Open(file.FullName, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
- pptSlides = pptDoc.Slides;
- //pptDoc = pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
- }
- public void AddPage() {
- pageCount++;
- //pptDoc.Slides.Add(pageCount, PPT.PpSlideLayout.ppLayoutText);
- pptSlide = pptSlides.Add(pageCount, PPT.PpSlideLayout.ppLayoutTitleOnly);
- }
- public void InsertPage(int index) {
- PPT.CustomLayout ppLayout = pptSlide.CustomLayout;
- pptSlide = pptSlides.AddSlide(index, ppLayout);
- pageCount++;
- }
- #region 添加文本框
- public PPT.Shape drawText(PPT.Shapes shapes, pptText textBox) {
- PPT.Shape shape = null;
- if (textBox == null || textBox.Location.IsEmpty || textBox.FrameSize.IsEmpty)
- return shape;
- shape = shapes.AddTextbox(textBox.Orientation, textBox.X, textBox.Y, textBox.Width, textBox.Height);
- shape.TextFrame.HorizontalAnchor = textBox.HorizontalAnchor;
- shape.TextFrame.VerticalAnchor = textBox.VerticalAnchor;
- shape.TextFrame.TextRange.Font.Color.RGB = colorFormat(textBox.ForeColor);
- shape.TextFrame.TextRange.Font.Bold = textBox.Font.Bold ? MsoTriState.msoTrue : MsoTriState.msoFalse;
- shape.TextFrame.TextRange.Font.Italic = textBox.Font.Italic ? MsoTriState.msoTrue : MsoTriState.msoFalse;
- shape.TextFrame.TextRange.Font.Underline = textBox.Font.Underline ? MsoTriState.msoTrue : MsoTriState.msoFalse;
- shape.TextFrame.TextRange.Font.Size = textBox.Font.Size;
- shape.TextFrame.TextRange.Font.Name = textBox.Font.Name;
- shape.TextFrame.MarginLeft = 0;
- shape.TextFrame.MarginRight = 0;
- if (textBox.BackColor == Color.Transparent) {
- shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
- } else {
- shape.Fill.BackColor.RGB = colorFormat(textBox.BackColor);
- }
- shape.Line.Weight = textBox.BoardWeight;
- if (textBox.BoardColor == Color.Transparent) {
- shape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;
- } else {
- shape.Line.BackColor.RGB = colorFormat(textBox.BoardColor);
- }
- shape.Line.DashStyle = textBox.BoardStyle;
- shape.TextFrame.TextRange.Text = textBox.Text;
- return shape;
- }
- #endregion
- #region 添加基本图形
- //画直线
- public PPT.Shape drawLine(PPT.Shapes shapes, float beginX, float beginY, float endX, float endY) {
- PPT.Shape shape = shapes.AddLine(beginX, beginY, endX, endY);
- return shape;
- }
- //画直线
- public PPT.Shape drawLine(PPT.Shapes shapes, float beginX, float beginY, float endX, float endY, float weight, Color foreColor) {
- PPT.Shape shape = shapes.AddLine(beginX, beginY, endX, endY);
- shape.Line.ForeColor.RGB = colorFormat(foreColor); //线条颜色
- shape.Line.Weight = weight; //线条粗细
- return shape;
- }
- //画矩形
- public PPT.Shape drawRectangle(PPT.Shapes shapes, float beginX, float beginY, float width, float height) {
- PPT.Shape shape = shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, beginX, beginY, width, height);
- return shape;
- }
- //画矩形
- public PPT.Shape drawRectangle(PPT.Shapes shapes, float beginX, float beginY, float width, float height, float weight, Color foreColor, Color backColor) {
- PPT.Shape shape = shapes.AddShape(MsoAutoShapeType.msoShapeRectangle, beginX, beginY, width, height);
- shape.Line.ForeColor.RGB = colorFormat(foreColor); //线条颜色
- shape.Fill.BackColor.RGB = colorFormat(backColor); //填充颜色
- shape.Line.Weight = weight; //线条粗细
- return shape;
- }
- //画箭头
- public PPT.Shape drawArrow(PPT.Shapes shapes, float beginX, float beginY, float endX, float endY,float weight) {
- float width=(float)Math.Sqrt(Math.Pow(endX-beginX,2)+Math.Pow(endY-beginY,2));
- float startX = (beginX + endX) / 2-width/2;
- float startY = (beginY + endY) / 2;
- float angle = endX==beginX?(endY>beginY?90:-90):(float)Math.Atan((endY - beginY) / (endX - beginX));
- angle = (float)(angle / Math.PI * 180.0);
- angle = angle < 0 ? 180.0f + angle : angle;
- PPT.Shape shape = shapes.AddShape(MsoAutoShapeType.msoShapeRightArrow, startX, startY, width, weight);
- shape.Rotation = angle;
- return shape;
- }
- //画箭头
- public PPT.Shape drawRightArrow(PPT.Shapes shapes, float beginX, float beginY, float endX, float endY, float weight, Color foreColor, Color backColor) {
- PPT.Shape shape = drawArrow(shapes, beginX, beginY, endX, endY, weight);
- shape.Line.ForeColor.RGB = colorFormat(foreColor); //线条颜色
- shape.Fill.BackColor.RGB = colorFormat(backColor); //填充颜色
- return shape;
- }
- #endregion
- #region 添加图片
- public PPT.Shape AddPicture(PPT.Shapes shapes, string picPath, float beginX, float beginY, float width, float height) {
- PPT.Shape shape = null;
- if (!File.Exists(picPath)) {
- throw new Exception("图片文件不存在!");
- } else {
- shape = shapes.AddPicture(picPath, MsoTriState.msoFalse, MsoTriState.msoTrue, beginX, beginY, width, height);
- }
- return shape;
- }
- public PPT.Shape AddPicture(PPT.Shapes shapes, Bitmap bitmap, float beginX, float beginY, float width, float height) {
- PPT.Shape shape = null;
- string picPath=System.IO.Path.GetTempPath()+"bitmap.bmp";
- bitmap.Save(picPath, ImageFormat.Bmp);
- shape = shapes.AddPicture(picPath, MsoTriState.msoFalse, MsoTriState.msoTrue, beginX, beginY, width, height);
- return shape;
- }
- #endregion
- public void Close() {
- try {
- //WdSaveFormat为PPT文档的保存格式
- PPT.PpSaveAsFileType format = PPT.PpSaveAsFileType.ppSaveAsDefault;
- //将pptDoc文档对象的内容保存为PPT文档
- pptDoc.SaveAs(pptPath, format, Microsoft.Office.Core.MsoTriState.msoFalse);
- //关闭pptDoc文档对象
- pptDoc.Close();
- //关闭pptApp组件对象
- pptApp.Quit();
- } catch (Exception ex) {
- outPrint.appendText("保存或关闭PPT出错,错误信息:" + ex.Message);
- }
- }
- /// <summary>
- /// 系统颜色转换为PPT支持的颜色值
- /// </summary>
- private int colorFormat(System.Drawing.Color color) {
- int value = ((color.B * 256 + color.G) * 256) + color.R;//Office RGB与 System.Drawing.Color.RGB顺序相反
- return value;
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFFICECORE = Microsoft.Office.Core;
using POWERPOINT = Microsoft.Office.Interop.PowerPoint;
using System.Windows;
using System.Collections;
using System.Windows.Controls;
namespace PPTDraw.PPTOperate
{
/// <summary>
/// PPT文档操作实现类.
/// </summary>
public class OperatePPT
{
#region=========基本的参数信息=======
POWERPOINT.Application objApp = null;
POWERPOINT.Presentation objPresSet = null;
POWERPOINT.SlideShowWindows objSSWs;
POWERPOINT.SlideShowTransition objSST;
POWERPOINT.SlideShowSettings objSSS;
POWERPOINT.SlideRange objSldRng;
bool bAssistantOn;
double pixperPoint = 0;
double offsetx = 0;
double offsety = 0;
#endregion
#region===========操作方法==============
/// <summary>
/// 打开PPT文档并播放显示。
/// </summary>
/// <param name="filePath">PPT文件路径</param>
public void PPTOpen(string filePath)
{
//防止连续打开多个PPT程序.
if (this.objApp != null) { return; }
try
{
objApp = new POWERPOINT.Application();
//以非只读方式打开,方便操作结束后保存.
objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
//Prevent Office Assistant from displaying alert messages:
bAssistantOn = objApp.Assistant.On;
objApp.Assistant.On = false;
objSSS = this.objPresSet.SlideShowSettings;
objSSS.Run();
}
catch (Exception ex)
{
this.objApp.Quit();
}
}
/// <summary>
/// 自动播放PPT文档.
/// </summary>
/// <param name="filePath">PPTy文件路径.</param>
/// <param name="playTime">翻页的时间间隔.【以秒为单位】</param>
public void PPTAuto(string filePath, int playTime)
{
//防止连续打开多个PPT程序.
if (this.objApp != null) { return; }
objApp = new POWERPOINT.Application();
objPresSet = objApp.Presentations.Open(filePath, OFFICECORE.MsoTriState.msoCTrue, OFFICECORE.MsoTriState.msoFalse, OFFICECORE.MsoTriState.msoFalse);
// 自动播放的代码(开始)
int Slides = objPresSet.Slides.Count;
int[] SlideIdx = new int[Slides];
for (int i = 0; i < Slides; i++) { SlideIdx[i] = i + 1; };
objSldRng = objPresSet.Slides.Range(SlideIdx);
objSST = objSldRng.SlideShowTransition;
//设置翻页的时间.
objSST.AdvanceOnTime = OFFICECORE.MsoTriState.msoCTrue;
objSST.AdvanceTime = playTime;
//翻页时的特效!
objSST.EntryEffect = POWERPOINT.PpEntryEffect.ppEffectCircleOut;
//Prevent Office Assistant from displaying alert messages:
bAssistantOn = objApp.Assistant.On;
objApp.Assistant.On = false;
//Run the Slide show from slides 1 thru 3.
objSSS = objPresSet.SlideShowSettings;
objSSS.StartingSlide = 1;
objSSS.EndingSlide = Slides;
objSSS.Run();
//Wait for the slide show to end.
objSSWs = objApp.SlideShowWindows;
while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(playTime * 100);
this.objPresSet.Close();
this.objApp.Quit();
}
/// <summary>
/// PPT下一页。
/// </summary>
public void NextSlide()
{
if (this.objApp != null)
this.objPresSet.SlideShowWindow.View.Next();
}
/// <summary>
/// PPT上一页。
/// </summary>
public void PreviousSlide()
{
if (this.objApp != null)
this.objPresSet.SlideShowWindow.View.Previous();
}
/// <summary>
/// 对当前的PPT页面进行图片插入操作。
/// </summary>
/// <param name="alImage">图片对象信息数组</param>
/// <param name="offsetx">插入图片距离左边长度</param>
/// <param name="pixperPoint">距离比例值</param>
/// <returns>是否添加成功!</returns>
public bool InsertToSlide(List<PPTOBJ> listObj)
{
bool InsertSlide = false;
if (this.objPresSet != null)
{
this.SlideParams();
int slipeint = objPresSet.SlideShowWindow.View.CurrentShowPosition;
foreach (PPTOBJ myobj in listObj)
{
objPresSet.Slides[slipeint].Shapes.AddPicture(
myobj.Path, //图片路径
OFFICECORE.MsoTriState.msoFalse,
OFFICECORE.MsoTriState.msoTrue,
(float)((myobj.X - this.offsetx) / this.pixperPoint), //插入图片距离左边长度
(float)(myobj.Y / this.pixperPoint), //插入图片距离顶部高度
(float)(myobj.Width / this.pixperPoint), //插入图片的宽度
(float)(myobj.Height / this.pixperPoint) //插入图片的高度
);
}
InsertSlide = true;
}
return InsertSlide;
}
/// <summary>
/// 计算InkCanvas画板上的偏移参数,与PPT上显示图片的参数。
/// 用于PPT加载图片时使用
/// </summary>
private void SlideParams()
{
double slideWidth = this.objPresSet.PageSetup.SlideWidth;
double slideHeight = this.objPresSet.PageSetup.SlideHeight;
double inkCanWidth = SystemParameters.PrimaryScreenWidth;//inkCan.ActualWidth;
double inkCanHeight = SystemParameters.PrimaryScreenHeight;//inkCan.ActualHeight ;
if ((slideWidth / slideHeight) > (inkCanWidth / inkCanHeight))
{
this.pixperPoint = inkCanHeight / slideHeight;
this.offsetx = 0;
this.offsety = (inkCanHeight - slideHeight * this.pixperPoint) / 2;
}
else
{
this.pixperPoint = inkCanHeight / slideHeight;
this.offsety = 0;
this.offsetx = (inkCanWidth - slideWidth * this.pixperPoint) / 2;
}
}
/// <summary>
/// 关闭PPT文档。
/// </summary>
public void PPTClose()
{
//装备PPT程序。
if (this.objPresSet != null)
{
//判断是否退出程序,可以不使用。
//objSSWs = objApp.SlideShowWindows;
//if (objSSWs.Count >= 1)
//{
if (MessageBox.Show("是否保存修改的笔迹!", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
this.objPresSet.Save();
//}
//this.objPresSet.Close();
}
if (this.objApp != null)
this.objApp.Quit();
GC.Collect();
}
#endregion
}
}
主要实现将ppt转换成html的功能,方法很多
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Reflection; namespace WritePptDemo
{
class Program
{
static void Main(string[] args)
{
string path; //文件路径变量 PPT.Application pptApp; //PPT应用程序变量
PPT.Presentation pptDoc; //PPT文档变量 PPT.Presentation pptDoctmp; path = @"C:\MyPPT.ppt"; //路径
pptApp = new PPT.ApplicationClass(); //初始化 //如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
} //由于使用的是COM库,因此有许多变量需要用Nothing代替
Object Nothing = Missing.Value;
pptDoc = pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
pptDoc.Slides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutText); string text = "示例文本"; foreach (PPT.Slide slide in pptDoc.Slides)
{
foreach (PPT.Shape shape in slide.Shapes)
{
shape.TextFrame.TextRange.InsertAfter(text);
}
} //WdSaveFormat为Excel文档的保存格式
PPT.PpSaveAsFileType format = PPT.PpSaveAsFileType.ppSaveAsDefault; //将excelDoc文档对象的内容保存为XLSX文档
pptDoc.SaveAs(path, format, Microsoft.Office.Core.MsoTriState.msoFalse); //关闭excelDoc文档对象
pptDoc.Close(); //关闭excelApp组件对象
pptApp.Quit(); Console.WriteLine(path + " 创建完毕!"); Console.ReadLine(); string pathHtml = @"c:\MyPPT.html"; PPT.Application pa = new PPT.ApplicationClass(); pptDoctmp = pa.Presentations.Open(path, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoFalse);
PPT.PpSaveAsFileType formatTmp = PPT.PpSaveAsFileType.ppSaveAsHTML;
pptDoctmp.SaveAs(pathHtml, formatTmp, Microsoft.Office.Core.MsoTriState.msoFalse);
pptDoctmp.Close();
pa.Quit();
Console.WriteLine(pathHtml + " 创建完毕!");
}
}
}
C# PPT Operator的更多相关文章
- 46张PPT讲述JVM体系结构、GC算法和调优
本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.(内嵌iframe,建议使用电脑浏览) 好东西当然要分享,PPT已上传可供下载 ...
- C#向PPT文档插入图片以及导出图片
PowerPoint演示文稿是我们日常工作中常用的办公软件之一,而图片则是PowerPoint文档的重要组成部分,那么如何向幻灯片插入图片以及导出图片呢?本文我将给大家分享如何使用一个免费版Power ...
- Impress.js上手 - 抛开PPT、制作Web 3D幻灯片放映
前言: 如果你已经厌倦了使用PPT设置路径.设置时间.设置动画方式来制作动画特效.那么Impress.js将是你一个非常好的选择. 用它制作的PPT将更加直观.效果也是嗷嗷美观的. 当然,如果用它来装 ...
- Linux环境下常见漏洞利用技术(培训ppt+实例+exp)
记得以前在drops写过一篇文章叫 linux常见漏洞利用技术实践 ,现在还可以找得到(https://woo.49.gs/static/drops/binary-6521.html), 不过当时开始 ...
- 《学技术练英语》PPT分享
之前做的一个PPT,分享给博客园的同学. 下载地址: 学技术练英语.pdf 技术是靠自己去学的,学技术不能仅仅是看书看博客,最好是有实践,不管是做实验去验证,还是写各种代码去玩各种特性,还是造轮子都是 ...
- Cleaver快速制作网页PPT
原文首发链接:http://www.jeffjade.com/2015/10/15/2015-10-16-cleaver-make-ppt/ 写在开始之前 互联网时代,以浏览器作为入口,已经有越来越多 ...
- 微软Ignite大会我的Session(SQL Server 2014 升级面面谈)PPT分享
我在首届微软技术大会的Session分享了一个关于SQL Server升级的主题,现在将PPT分享出来. 您可以点击这里下载PPT. 也非常感谢微软中国邀请我进行这次分享.
- C# 复制幻灯片(包括格式、背景、图片等)到同/另一个PPT文档
C# 复制幻灯片(包括格式.背景.图片等)到同/另一个PPT文档 复制幻灯片是使用PowerPoint过程中的一个比较常见的操作,在复制一张幻灯片时一般有以下两种情况: 在同一个PPT文档内复制 从一 ...
- 在C#里面给PPT添加注释
平常开会或者做总结报告的时候我们通常都会用到PowerPoint演示文稿,我们可以在单个幻灯片或者全部幻灯片里面添加注释,这样观众可以从注释内容里面获取更多的相关信息. 有些朋友不清楚如何在幻灯片里面 ...
随机推荐
- MVC4做网站后台:用户管理 —用户
这块进行用户管理,可以浏览.查询已注册的用户,修改用户资料,删除用户等.没有做添加用户,不知是否必要.列表页还是使用easyui的datagrid.这个思路跟用户组的方式差不多. 1.接口Interf ...
- ECMAScript5之Object
在ECMAScript5中对Object新增的些方法,以前没注意的同志们,嘻嘻,下面我们再一起来边看边学. 1.Object之create Create单词意为创造嘛,作为Object的静态方法,不言 ...
- Hibernate学习之——搭建log4j日志环境
昨天讲了Hibernate开发环境的搭建以及实现一个Hibernate的基础示例,但是你会发现运行输出只有sql语句,很多输出信息都看不见.这是因为用到的是slf4j-nop-1.6.1.jar的实现 ...
- HTML5 Audio/Video 标签,属性,方法,事件汇总
HTML5 Audio/Video 标签,属性,方法,事件汇总 (转) 2011-06-28 13:16:48 <audio> 标签属性:src:音乐的URLpreload:预加载au ...
- DEBIAN下中文显示
转:http://www.cppblog.com/colorful/archive/2012/05/28/176516.aspx 一.首先检查LOCALE情况 说明:DEBIAN因为基于GNU所以,对 ...
- C/C++:提升_指针的指针和指针的引用
C/C++:提升_指针的指针和指针的引用 写在前面 今天在使用指针的时候我发现了一个自己的错误.
- hibernate笔记--组件映射方法
假设我们需要保存学生student的信息,student中有一个address属性,我们知道像这种信息其值可能会有多个,就像一个人会有两个以上的手机号,这种情况在hibernate中应该这样配置: 新 ...
- sql 索引 的建立
(From:http://54laobaixing.blog.163.com/blog/static/57843681200952411133121/) 假设你想找书中的某一个句子.你可以一页一页地逐 ...
- SQL Server基础之索引
索引用于快速找出在某个列中有某一特定值的行,不使用索引,数据库必须从第一条记录开始读完整个表,直到找出相关的行.表越大,查询数据所花费的时间越多,如果表中查询的列有一个索引,数据库能快速到达一个位置 ...
- 【原创】Kafka console consumer源代码分析(一)
上一篇中分析了Scala版的console producer代码,这篇文章为读者带来一篇console consumer工作原理分析的随笔.其实不论是哪个consumer,大部分的工作原理都是类似的. ...