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

本文介绍如何使用C#对PPT进行背景色和字体色的统一设置,并实现PPT到图片的批量转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:最近由于项目需求,需要将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);//打开PPT
int size = 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转图片
string imgPath = "";
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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值