C# 插入文本框到PPT幻燈片


概述

在文本框中我們可以實現的操作有很多,如插入文字、圖片、設置字體大小、顏色、文本框背景填充、邊框設置等。下面的示例中,將介紹通過C# 在PPT幻燈片中插入幻燈片的方法。

示例中包含了以下要點:

  • 插入文本到文本框
  • 設置邊框顏色、粗細
  • 文本框背景色填充
  • 設置文本框旋轉
  • 設置文本框陰影效果

 

使用工具:Free Spire.Presentation for .NET 3.3(免費版)

注:安裝后,注意在程序中添加引用Spire.Presentaton.dll(dll可在安裝路徑下的Bin文件夾中獲取)

C# 代碼(供參考)

步驟 1 :初始化Presentation類,加載測試文檔

 Presentation presentation = new Presentation();
 presentation.LoadFromFile("test.pptx");

步驟 2 :獲取幻燈片

ISlide slide = presentation.Slides[0];

步驟 3 :添加指定大小的文本框(shape)到幻燈片,並寫入文本

IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(80, 50, 420, 350));
string textString = "萬有引力的發現,是17世紀自然科學最偉大的成果之一。" +
                    "它把地面上的物體運動的規律和天體運動的規律統一了起來,對以后物理學和天文學的發展具有深遠的影響。" +
                    "它第一次揭示了自然界中一種基本相互作用的規律,在人類認識自然的歷史上樹立了一座里程碑。" +
                    "牛頓的萬有引力概念是所有科學中最實用的概念之一。牛頓認為萬有引力是所有物質的基本特征,這成為大部分物理科學的理論基石。";
shape.AppendTextFrame(textString);

步驟 4 :設置文本框邊框樣式、填充樣式、陰影效果、旋轉度等

//設置shape線條顏色和寬度
shape.Line.FillType = FillFormatType.Solid;
shape.Line.Width = 2;
shape.Line.SolidFillColor.Color = Color.White;

//設置shape填充顏色為漸變色
shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient;
shape.Fill.Gradient.GradientShape = Spire.Presentation.Drawing.GradientShapeType.Linear;
shape.Fill.Gradient.GradientStops.Append(1f, KnownColors.LightGray);
shape.Fill.Gradient.GradientStops.Append(0, KnownColors.LightBlue);            

//設置shape陰影
Spire.Presentation.Drawing.OuterShadowEffect shadow = new Spire.Presentation.Drawing.OuterShadowEffect();
shadow.BlurRadius = 20;
shadow.Direction = 30;
shadow.Distance = 8;
shadow.ColorFormat.Color = Color.LightGray;
shape.EffectDag.OuterShadowEffect = shadow;

//設置shape向右旋轉5度(向左旋轉設置數值為負即可)
shape.Rotation = 5;

步驟 5 :保存文檔

presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);

完成代碼后,調試程序,生成文檔。文本框添加效果如下圖所示:

全部代碼:

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.Drawing;

namespace InsertTextbox_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //實例化Presentation類對象,加載文檔並獲取第一個幻燈片
            Presentation presentation = new Presentation();
            presentation.LoadFromFile("test.pptx");
            ISlide slide = presentation.Slides[0];

            //添加一個文本框(shape)到第一張幻燈片並添加文字。
            IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(80, 50, 420, 350));
            string textString = "萬有引力的發現,是17世紀自然科學最偉大的成果之一。" +
                                "它把地面上的物體運動的規律和天體運動的規律統一了起來,對以后物理學和天文學的發展具有深遠的影響。" +
                                "它第一次揭示了自然界中一種基本相互作用的規律,在人類認識自然的歷史上樹立了一座里程碑。" +
                                "牛頓的萬有引力概念是所有科學中最實用的概念之一。牛頓認為萬有引力是所有物質的基本特征,這成為大部分物理科學的理論基石。";
            shape.AppendTextFrame(textString);

            //設置shape線條顏色和寬度
            shape.Line.FillType = FillFormatType.Solid;
            shape.Line.Width = 2;
            shape.Line.SolidFillColor.Color = Color.White;

            //設置shape填充顏色為漸變色
            shape.Fill.FillType = Spire.Presentation.Drawing.FillFormatType.Gradient;
            shape.Fill.Gradient.GradientShape = Spire.Presentation.Drawing.GradientShapeType.Linear;
            shape.Fill.Gradient.GradientStops.Append(1f, KnownColors.LightGray);
            shape.Fill.Gradient.GradientStops.Append(0, KnownColors.LightBlue);            

            //設置shape陰影
            Spire.Presentation.Drawing.OuterShadowEffect shadow = new Spire.Presentation.Drawing.OuterShadowEffect();
            shadow.BlurRadius = 20;
            shadow.Direction = 30;
            shadow.Distance = 8;
            shadow.ColorFormat.Color = Color.LightGray;
            shape.EffectDag.OuterShadowEffect = shadow;

            //設置shape向右旋轉5度(向左旋轉設置數值為負即可)
            shape.Rotation = 5;

            //保存並打開文檔
            presentation.SaveToFile("result.pptx", FileFormat.Pptx2010);
            System.Diagnostics.Process.Start("result.pptx");
        }
    }
}
View Code

 

(本文完)

轉載請注明出處!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM