C#でPowerPointファイルを画像として保存する

Microsoft PowerPoint 11.0 Object Library を利用すると、PowerPointファイルの各スライドを画像として保存することが比較的容易に実現できます。画像形式としては、bmp, png jpg, gif, tif, wmf などが使えるようです。画像サイズも指定できます。なお、メタファイル(wmf)形式を指定するとベクトルデータとして出力が可能です。ただし、ファイルの画像サイズは無視されるようです。

サンプルコードは以下のような感じになります。エラー処理はあまりまじめにやっていないので、適宜追加してください。

 using System;
 using System.IO;
 using Microsoft.Office.Interop.PowerPoint;
 using Microsoft.Office.Core;
 
 namespace ppt2img
 {
     class Program
     {
         static void Main(string[] args)
         {
             if (args.Length != 4)
             {
                 Console.WriteLine("PowerPointファイルを画像に変換します");
                 Console.WriteLine();
                 Console.WriteLine("ppt2img [PowerPointファイル] [画像形式] [幅] [高さ]");
                 Console.WriteLine("例) ppt2img test.ppt png 640 480");
                 return;
             }
             // 引数を読み込む
             string filename = Path.GetFullPath(args[0]);
             string filetype = args[1];
             int width = Convert.ToInt32(args[2]);
             int height = Convert.ToInt32(args[3]);
 
             // PowerPointファイルを開く
             ApplicationClass app = new ApplicationClass();
             Presentation ppt = app.Presentations.Open(filename,
                 MsoTriState.msoFalse,
                 MsoTriState.msoFalse,
                 MsoTriState.msoFalse);
 
             // スライド枚数を取得
             int slides = ppt.Slides.Count;
 
             // 保存先作成
             string outputPath = Directory.GetCurrentDirectory() + @"output";
             Directory.CreateDirectory(outputPath);
 
             // 画像として出力
             for (int i = 1; i <= slides; i++)
             {
                 string outputName = outputPath + "p"
                     +i.ToString().PadLeft(5,'0') + "." + filetype;
                 ppt.Slides[i].Export(outputName, filetype, width, height);
             }
 
             // 後処理
             ppt.Close();
         }
     }
 }

PowerPoint 2003 での動作を確認していますが、他のバージョンでの動作は未確認です。

サンプルのダウンロード (Visual C# 2008 Express, サンプルの実行には .NET Framework 2.0, 3.0, 3.5 のいずれかが必要です)
ボックス1 (2008/06/30) ppt2img v1.0
 初出: 2008/06/30 00:11 ささお * 内容は随時追記・更新しています。
| 技術メモ::ソフトウェア | comments (1) | trackback (0) |このエントリーを含むはてなブックマーク このエントリーをはてなブックマークする

  
AMAZON
CALENDAR
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30     
<<   04 - 2024   >>
TRACBACK
PROFILE
LOGIN
現在のモード: ゲストモード
USER ID:
PASS:
OTHERS
LINK
POWERED BY
POWERED BY
ぶろぐん
SKIN BY
ゲットネット...¥