C#/VB.NET 將PPT或PPTX轉換為影像

C#/VB.NET 將PPT或PPTX轉換為影像

 

由於大多數攜帶型設備支援瀏覽圖片而不支援瀏覽PowerPoint 文件,所以相比較而言,影像對於用戶而言更加友好。除此之外,將PowerPoint文檔轉換為影像也可以防止對內容做出修改。在本文中,我將展示如何使用 Spire.Presentation for .NET 在C#/VB.NET程式中,將PowerPoint(PPT 和 PPTX)轉換為 PNG 或 SVG。

 

 

將PPT或PPTX轉換為PNG

將PPT或PPTX轉換為SVG

 

 

安裝 Spire.Presentation for .NET

 

首先,我們需要將 Spire.Presentation for .NET 包中包含的 DLL 文件添加為 .NET 項目中的引用。可以從此鏈接下載 DLL 文件,也可以通過NuGet 安裝 DLL 文件。

 

PM> Install-Package Spire.Presentation

 

將PPT或PPTX轉換為PNG

 

[C#]

using Spire.Presentation;
using System;
using System.Drawing;
using System.Drawing.Imaging;
 
namespace ConvertPowerPointToPng
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Presentation實例
            Presentation presentation = new Presentation();

            //載入一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx");

            //遍歷PowerPoint文檔中的幻燈片並保存為PNG圖片
            for (int i = 0; i < presentation.Slides.Count; i++)
            {
                Image image = presentation.Slides[i].SaveAsImage();
                String fileName = String.Format("圖片{0}.png", i);
                image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png);
            }
        }
    }
}

[VB.NET]

Imports Spire.Presentation
Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
 
Namespace ConvertPowerPointToPng
    Class Program
        Shared  Sub Main(ByVal args() As String)
            '初始化Presentation實例
            Dim presentation As Presentation =  New Presentation() 
 
            '載入一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx")
 
            '遍歷PowerPoint文檔中的幻燈片並保存為PNG圖片
            Dim i As Integer
            For  i = 0 To  presentation.Slides.Count- 1  Step  i + 1
                Dim image As Image =  presentation.Slides(i).SaveAsImage() 
                Dim fileName As String =  String.Format("圖片{0}.png",i) 
                image.Save(fileName, System.Drawing.Imaging.ImageFormat.Png)
            Next
        End Sub
    End Class
End Namespace

 

將PPT或PPTX轉換為SVG

 

 

[C#]

using System.Collections.Generic;
using System.IO;
namespace PPTtoSVG
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Presentation實例
            Presentation presentation = new Presentation();

            //載入一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx");

            //將PowerPoint轉換為SVG影像並以位元組形式存儲在列隊中
            Queue<byte[]> svgBytes = presentation.SaveToSVG();

            //獲取列隊中位元組數組生成SVG文件
            int len = svgBytes.Count;
            for (int i = 0; i < len; i++)
            {
                FileStream fs = new FileStream(string.Format("圖片-{0}.svg", i), FileMode.Create);
                byte[] bytes = svgBytes.Dequeue();
                fs.Write(bytes, 0, bytes.Length);
                presentation.Dispose();
            }
        }
    }
}

 

[VB.NET]

Imports System.Collections.Generic
Imports System.IO
Namespace PPTtoSVG
    Class Program
        Shared  Sub Main(ByVal args() As String)
            '初始化Presentation實例
            Dim presentation As Presentation =  New Presentation() 
 
            '載入一個PowerPoint文檔
            presentation.LoadFromFile("模板.pptx")
 
            '將PowerPoint轉換為SVG影像並以位元組形式存儲在列隊中
            Dim svgBytes()> As Queue<byte =  presentation.SaveToSVG() 
 
            '獲取列隊中位元組數組生成SVG文件
            Dim len As Integer =  svgBytes.Count 
            Dim i As Integer
            For  i = 0 To  len- 1  Step  i + 1
                Dim fs As FileStream =  New FileStream(String.Format("圖片-{0}.svg",i),FileMode.Create) 
                Dim bytes() As Byte =  svgBytes.Dequeue() 
                fs.Write(bytes, 0, bytes.Length)
                presentation.Dispose()
            Next
        End Sub
    End Class
End Namespace