1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > c# 操作ppt 设置背景色 字体颜色 PPT转图片

c# 操作ppt 设置背景色 字体颜色 PPT转图片

时间:2020-02-06 12:43:30

相关推荐

c# 操作ppt 设置背景色 字体颜色 PPT转图片

前言:最近由于项目需求,需要将ppt的背景色和字体色统一设置成一种颜色(详细需求不再赘述)、PPT转图片,通过这几天的查资料和怼微软的API总结如下。本文主要总结C#使用office组件操作office全家桶。

1.操作PPT(设置PPT的背景色为白色【需要注意渐进色】,字体色为黑色)(1)需要添加的引用:

(2)代码 - 代码中的注解很详细,不再赘述Microsoft.Office.Interop.PowerPoint.Application pptApp;//PPT应用程序变量Microsoft.Office.Interop.PowerPoint.Presentation pptDoc;//PPT文档变量pptApp = new Microsoft.Office.Interop.PowerPoint.Application();//初始化pptDoc = pptApp.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);//打开PPTintsize = pptDoc.Slides.Count;//ppt的幻灯片数量int i = 0;foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in pptDoc.Slides){slide.FollowMasterBackground = MsoTriState.msoFalse;//不采用模板的背景色 slide.Background.BlackWhiteMode = MsoBlackWhiteMode.msoBlackWhiteAutomatic;//设置背景色为黑白模式 slide.Background.Fill.OneColorGradient(MsoGradientStyle.msoGradientHorizontal, 1, 1.0f);//去掉渐进色(只采用黑白模式无法去除渐进色) slide.Background.Fill.ForeColor.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(255, 255, 255));//设置前景色为白色 slide.Background.Fill.BackColor.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(255, 255, 255));//设置背景色为白色

//遍历每个幻灯片的方块 foreach (Microsoft.Office.Interop.PowerPoint.Shape shape in slide.Shapes) {shape.BlackWhiteMode = MsoBlackWhiteMode.msoBlackWhiteAutomatic; if (shape.TextFrame.HasText == MsoTriState.msoTrue)//判断方块是否有字体,如果没有不设置,不然报异常 {if (shape.TextFrame.TextRange != null) {if (shape.TextFrame.TextRange.Text != null) {if (shape.TextFrame.TextRange.Text != "") {shape.TextFrame.TextRange.Font.Color.RGB = System.Drawing.ColorTranslator.ToWin32(System.Drawing.Color.FromArgb(0, 0, 0));//设置字体为黑色 } } } } }}//WdSaveFormat为PPT文档的保存格式Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType format = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault;//保存(另存为)pptDoc.SaveAs(pptSavePath, format, Microsoft.Office.Core.MsoTriState.msoFalse);//关闭excelDoc文档对象pptDoc.Close();//关闭excelApp组件对象pptApp.Quit();

2.PPT转图片stringimgPath = "";int width = 0;int height = 0;String imgType = "png";String baseName = "test";

Microsoft.Office.Interop.PowerPoint.Application pptApp;//PPT应用程序变量Microsoft.Office.Interop.PowerPoint.Presentation pptDoc;//PPT文档变量pptApp = new Microsoft.Office.Interop.PowerPoint.Application();//初始化pptDoc = pptApp.Presentations.Open(pptPath, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse);int size = pptDoc.Slides.Count;int i = 0;foreach (Microsoft.Office.Interop.PowerPoint.Slide slide in pptDoc.Slides){String outName = String.Format(@"{0}{1}_slide{2}.{3}", imgPath, baseName, i++, imgType); try {slide.Export(outName, imgType, width, height);//导出图片 } catch (Exception ex) {}}//WdSaveFormat为PPT文档的保存格式Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType format = Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsDefault;//保存pptDoc.SaveAs(pptSavePath, format, Microsoft.Office.Core.MsoTriState.msoFalse);//关闭excelDoc文档对象pptDoc.Close();//关闭excelApp组件对象pptApp.Quit();

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。