今回は、PowerPointファイル内のフォントを違うフォントで置換します。
”MS Pゴシック”に変更してみます。
/// <summary>
/// ファイルのフォントを置換する。
/// </summary>
/// <param name="file"></param>
/// <param name="file2"></param>
public void replaceSlideFont( string file, string file2 )
{
Microsoft.Office.Interop.PowerPoint.Application app = null;
Microsoft.Office.Interop.PowerPoint.Presentation ppt = null;
try {
// PPTのインスタンス作成
app = new Microsoft.Office.Interop.PowerPoint.Application();
ppt = app.Presentations.Open(file,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoFalse);
// 全体での指定したフォントの置換
try {
// "MS ゴシック" → "MS Pゴシック"に
ppt.Fonts.Replace("MS ゴシック", "MS Pゴシック");
}
catch ( Exception ex ) {
Debug.WriteLine(ex.Message);
}
// オブジェクトごとのフォントの置換
if ( ppt.Slides.Count > 0 ) {
for ( int i = 1; i <= ppt.Slides.Count; i++ ) {
Debug.WriteLine("Slide:" + i.ToString() + "ページ");
foreach ( Shape shape in ppt.Slides[i].Shapes ) {
replaceShapeFont(shape);
}
}
}
// ファイルに保存
ppt.SaveAs(file2,
PpSaveAsFileType.ppSaveAsDefault,
Microsoft.Office.Core.MsoTriState.msoFalse);
}
finally {
if ( ppt != null ) {
ppt.Close();
}
if ( app != null ) {
app.Quit();
}
}
return ;
}
/// <summary>
/// オブジェクトのフォント置換
/// </summary>
/// <param name="shape"></param>
private void replaceShapeFont( Shape shape )
{
if ( shape.HasTextFrame == Microsoft.Office.Core.MsoTriState.msoTrue
&& shape.TextFrame.HasText == Microsoft.Office.Core.MsoTriState.msoTrue ) {
if ( shape.TextFrame.TextRange.Text != "" ) {
// フォント置換。
Microsoft.Office.Interop.PowerPoint.Font font;
font = shape.TextFrame.TextRange.Font;
font.Name = "MS Pゴシック";
font.NameFarEast = "MS Pゴシック";
font.NameOther = "+mn-ea";
font.NameComplexScript = "+mn-cs";
}
}
// 再帰
if ( shape.Type == Microsoft.Office.Core.MsoShapeType.msoGroup ) {
foreach ( Shape childShape in shape.GroupItems ) {
replaceShapeFont(childShape);
}
}
return;
}
Tags: C#, PowerPoint, パワーポイント
今回は、PowerPointファイルのスライドショーをC#から呼び出してみます。
http://support.microsoft.com/kb/303718/ja
を参考にさせていただきました。
/// <summary>
/// スライドショー
/// </summary>
/// <param name="filename"></param>
public void slideShow( string filename )
{
Microsoft.Office.Interop.PowerPoint.Application app = null;
Microsoft.Office.Interop.PowerPoint.Presentation ppt = null;
try {
// PPTのインスタンス作成
app = new Microsoft.Office.Interop.PowerPoint.Application();
// 表示する
app.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
// オープン
ppt = app.Presentations.Open(filename,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoTrue);
// スライドショーのページの指定
int[] SlideIdx = new int[ppt.Slides.Count];
for ( int i = 0; i < SlideIdx.Length; i++ ) {
SlideIdx[i] = i + 1;
}
Microsoft.Office.Interop.PowerPoint.SlideRange range;
range = ppt.Slides.Range(SlideIdx);
range.SlideShowTransition.AdvanceOnTime = Microsoft.Office.Core.MsoTriState.msoTrue;
range.SlideShowTransition.AdvanceTime = 3;
bool assistant_on = app.Assistant.On;
app.Assistant.On = false;
// 設定
Microsoft.Office.Interop.PowerPoint.SlideShowSettings settings;
settings = ppt.SlideShowSettings;
settings.StartingSlide = 1;
settings.EndingSlide = SlideIdx[SlideIdx.Length - 1];
// スライドショーの開始
settings.Run();
// 待機する
while ( app.SlideShowWindows.Count >= 1 ) {
System.Threading.Thread.Sleep(100);
}
//Reenable Office Assisant, if it was on:
if ( assistant_on ) {
app.Assistant.On = true;
app.Assistant.Visible = false;
}
}
finally {
// 終了
if ( ppt != null ) {
ppt.Close();
}
// PPTを閉じる
if ( app != null ) {
app.Quit();
}
}
}
Tags: C#, PowerPoint, パワーポイント
今回は、PowerPointファイルをスライドごとにJPEGファイルとして保存します。
#region JPEG保存
/// <summary>
/// PPTをページごとにJPEGとして保存する。
/// </summary>
/// <param name="ppt_file">PPTファイル</param>
/// <param name="output_dir">出力ディレクトリ</param>
public void saveAsJPEG( string ppt_file, string output_dir )
{
// ディレクトリ作成
Directory.CreateDirectory(output_dir);
Microsoft.Office.Interop.PowerPoint.Application app = null;
Microsoft.Office.Interop.PowerPoint.Presentation ppt = null;
try {
// PPTのインスタンス作成
app = new Microsoft.Office.Interop.PowerPoint.Application();
// オープン
ppt = app.Presentations.Open(
ppt_file
, Microsoft.Office.Core.MsoTriState.msoTrue
, Microsoft.Office.Core.MsoTriState.msoFalse
, Microsoft.Office.Core.MsoTriState.msoFalse
);
// 一括で保存する場合 ----------------
ppt.SaveAs(output_dir,
PpSaveAsFileType.ppSaveAsJPG,
Microsoft.Office.Core.MsoTriState.msoFalse);
// 1ページずつ保存する場合 -----------------
int width = (int)ppt.PageSetup.SlideWidth;
int height = (int)ppt.PageSetup.SlideHeight;
string file2;
for ( int i = 1; i <= ppt.Slides.Count; i++ ) {
// JPEGとして保存
file2 = output_dir + String.Format("\\slide{0:0000}.jpg", i);
ppt.Slides[i].Export(file2, "jpg", width, height);
}
}
finally {
// 終了
if ( ppt != null ) {
ppt.Close();
}
// PPTを閉じる
if ( app != null ) {
app.Quit();
app = null;
}
}
}
#endregion
一括で保存する処理は、元々のPPTファイルの画像サイズに対して
解像度が72dpi→96dpiとして変換された画像サイズで保存されます。
Tags: C#, PowerPoint, パワーポイント
今回は、PowerPointファイルから、各スライドのノートの内容を抽出します。
/// <summary>
/// スライドのノートをページごとに取得します。
/// </summary>
/// <param name="file">PowerPointファイル名</param>
/// <returns>ページごとのノート</returns>
public List<string> getSlideNotes( string file )
{
List<string> notes = new List<string>();
Microsoft.Office.Interop.PowerPoint.Application app = null;
Microsoft.Office.Interop.PowerPoint.Presentation ppt = null;
try {
// PPTのインスタンス作成
app = new Microsoft.Office.Interop.PowerPoint.Application();
// ファイルオープン
ppt = app.Presentations.Open(
file,
Microsoft.Office.Core.MsoTriState.msoTrue, // 読み取り専用
Microsoft.Office.Core.MsoTriState.msoTrue,
Microsoft.Office.Core.MsoTriState.msoFalse
);
string txt;
// スライドのインデックスは1から
for ( int i = 1; i <= ppt.Slides.Count; i++ ) {
Debug.WriteLine("slide: " + i.ToString());
txt = ppt.Slides[i].NotesPage.Shapes.Placeholders[2].TextFrame.TextRange.Text;
notes.Add(txt);
}
}
finally {
// ファイルを閉じる
if ( ppt != null ) {
ppt.Close();
ppt = null;
}
// PPTを閉じる
if ( app != null ) {
app.Quit();
app = null;
}
}
return notes;
}
Tags: C#, PowerPoint, パワーポイント
C#でPowerPointファイルの操作をしてみます。
開発環境
(1) まずはVisual Studio 2008で、新規プロジェクトを作成します。
(2) メニューの「プロジェクト」ー「参照の追加」でCOMタブの
「Microsoft PowerPoint 12.0 Object Library」追加します。
(PowerPointのバージョンによってライブラリのバージョンも異なります。)

これで、プロジェクトの参照設定に
が追加されました。

以上で準備は完了です。
次回に続きます。
Tags: C#, PowerPoint, パワーポイント