C# 獲取PPT幻燈片背景型別和顏色

Mia張發表於2022-01-27

PPT中可設定幻燈片背景,如顏色填充,漸變色填充,圖片或紋理填充,無填充等等。對PPT中已設定的背景,也可以通過後端程式判斷並讀取,下面是通過C#程式碼來實現讀取PPT幻燈片背景的方法和步驟。

引入 dll

1. 通過NuGet 安裝dll (2 種方法)

  1.1可以在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“管理NuGet包”,然後搜尋Spire.Presentation,點選安裝。

  1.2將以下內容複製到PM控制檯安裝。

Install-Package Spire.Presentation -Version 6.12.4

2. 手動新增dll 引用

可通過手動下載Jar包,然後解壓,找到BIN資料夾下的Spire.Presentation.dll。在Visual Studio中開啟“解決方案資源管理器”,滑鼠右鍵點選“引用”,“新增引用”將本地路徑BIN資料夾下的dll檔案新增引用至程式。

獲取幻燈片背景型別和顏色

C#

using Spire.Presentation;
using Spire.Presentation.Drawing;
using System;
 
namespace GetBackgroundTypeAndColor
{
    class Program
    {
        static void Main(string[] args)
        {
            //載入PPT幻燈片
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("test.pptx");
            
            //獲取第一張幻燈片
            ISlide slide = ppt.Slides[0];
 
            //獲取背景
            FillFormat background = slide.DisplaySlideBackground.GetBackgroundFillFormat(slide as ActiveSlide);
            
            //獲取背景型別和顏色
                switch (background.FillType)
                {
                    case FillFormatType.Solid:
                        Console.WriteLine("【型別】:" + background.FillType + "\n【顏色】:" + background.SolidColor.Color);
                        Console.ReadLine();
                        break;
 
                    case FillFormatType.Gradient:
                    for (int i = 0; i < background.Gradient.GradientStops.Count; i++)
                        {
                            GradientStop stop = background.Gradient.GradientStops[i];                                            
                            Console.WriteLine("【型別】:"+ background.FillType + "\n【位置】:" + stop.Position + "\n【顏色】:" + stop.Color.Color);
                            Console.ReadLine();
                    }
                        break;
                default:
                        Console.WriteLine("type:" + background.FillType);
                        break;
                }
 
        }
    }
}

VB.NET

Imports Spire.Presentation
Imports Spire.Presentation.Drawing
 
Namespace GetBackgroundTypeAndColor
         Class Program
                   Private Shared Sub Main(args As String())
                            '載入PPT幻燈片
                            Dim ppt As New Presentation()
                            ppt.LoadFromFile("test.pptx")
 
                            '獲取第一張幻燈片
                            Dim slide As ISlide = ppt.Slides(0)
 
                            '獲取背景
                            Dim background As FillFormat = slide.DisplaySlideBackground.GetBackgroundFillFormat(TryCast(slide, ActiveSlide))
 
                            '獲取背景型別和顏色
                            Select Case background.FillType
                                     Case FillFormatType.Solid
                                               Console.WriteLine("【型別】:" + background.FillType + vbLf & "【顏色】:" + background.SolidColor.Color)
                                               Console.ReadLine()
                                               Exit Select
 
                                     Case FillFormatType.Gradient
                                               For i As Integer = 0 To background.Gradient.GradientStops.Count - 1
                                                        Dim [stop] As GradientStop = background.Gradient.GradientStops(i)
                                                        Console.WriteLine("【型別】:" + background.FillType + vbLf & "【位置】:" + [stop].Position + vbLf & "【顏色】:" + [stop].Color.Color)
                                                        Console.ReadLine()
                                               Next
                                               Exit Select
                                     Case Else
                                               Console.WriteLine("type:" + background.FillType)
                                               Exit Select
                            End Select
 
                   End Sub
         End Class
End Namespace

背景顏色讀取結果:


注:檔案路徑可自定義。



—End—

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31499788/viewspace-2854209/,如需轉載,請註明出處,否則將追究法律責任。

相關文章