PowerPoint to JPEG convertor
最近做毕业设计答辩需要到一个比较好的模板,虽然我收集了数千个模板,但是PPT自动缩略图所得的效果太差,要查看PPT都需要自己手动打开,很麻烦,费事情。今天用C#写了一个小程序,实现了PowerPoint到JPEG的转换,可用于速查PPT、PPTX、POT的内容。简单方便实用。代码稍显混乱。
请在这之前安装OFFICE 2007。
PS:第一版程序忘了p.Close(),也就是关闭当前打开的幻灯片,导致最终内存占用达到1.5G,运行速度爆满,超级寒~~~
请在这之前安装OFFICE 2007。
PS:第一版程序忘了p.Close(),也就是关闭当前打开的幻灯片,导致最终内存占用达到1.5G,运行速度爆满,超级寒~~~
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
namespace PPT_to_JPEG
{
class Program
{
[STAThread]
static void Main(string[] args)
{
ApplicationClass ac=new ApplicationClass();
string srcRoot;
string dstRoot;
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description="Select root folder";
if(fbd.ShowDialog()!=DialogResult.OK) return;
srcRoot = fbd.SelectedPath;
fbd.Description="Select destination folder";
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() != DialogResult.OK) return;
dstRoot = fbd.SelectedPath;
string[] pptFiles = Directory.GetFiles(srcRoot, "*.ppt", SearchOption.AllDirectories);
string[] potFiles = Directory.GetFiles(srcRoot, "*.pot", SearchOption.AllDirectories);
string[] pptxFiles = Directory.GetFiles(srcRoot, "*.pptx", SearchOption.AllDirectories);
Console.WriteLine("Find " + (
pptxFiles.GetUpperBound(0) + 1 +
potFiles.GetUpperBound(0) + 1 +
pptxFiles.GetUpperBound(0) + 1)+ "Files");
foreach(string ppt in pptFiles)
{
string dir=ppt.Substring(srcRoot.Length+1);
string fullDir = Path.Combine(dstRoot, dir);
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting PPT File ("+ fullDir+")");
Presentation p = ac.Presentations.Open(ppt,MsoTriState.msoCTrue,MsoTriState.msoCTrue,MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
foreach (string pot in potFiles)
{
string dir = pot.Substring(srcRoot.Length+1);
string fullDir = Path.Combine(dstRoot, dir);
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting POT File (" + fullDir + ")");
Presentation p = ac.Presentations.Open(pot,MsoTriState.msoCTrue,MsoTriState.msoCTrue,MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
foreach (string pptx in pptxFiles)
{
string dir = pptx.Substring(srcRoot.Length+1);
string fullDir = Path.Combine(dstRoot, Path.GetFileName(dir));
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting PPTX File (" + fullDir + ")");
Presentation p = ac.Presentations.Open2007(pptx,MsoTriState.msoCTrue,MsoTriState.msoCTrue,MsoTriState.msoFalse,MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
Console.WriteLine("All done, press enter key to exit");
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.PowerPoint;
namespace PPT_to_JPEG
{
class Program
{
[STAThread]
static void Main(string[] args)
{
ApplicationClass ac=new ApplicationClass();
string srcRoot;
string dstRoot;
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description="Select root folder";
if(fbd.ShowDialog()!=DialogResult.OK) return;
srcRoot = fbd.SelectedPath;
fbd.Description="Select destination folder";
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() != DialogResult.OK) return;
dstRoot = fbd.SelectedPath;
string[] pptFiles = Directory.GetFiles(srcRoot, "*.ppt", SearchOption.AllDirectories);
string[] potFiles = Directory.GetFiles(srcRoot, "*.pot", SearchOption.AllDirectories);
string[] pptxFiles = Directory.GetFiles(srcRoot, "*.pptx", SearchOption.AllDirectories);
Console.WriteLine("Find " + (
pptxFiles.GetUpperBound(0) + 1 +
potFiles.GetUpperBound(0) + 1 +
pptxFiles.GetUpperBound(0) + 1)+ "Files");
foreach(string ppt in pptFiles)
{
string dir=ppt.Substring(srcRoot.Length+1);
string fullDir = Path.Combine(dstRoot, dir);
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting PPT File ("+ fullDir+")");
Presentation p = ac.Presentations.Open(ppt,MsoTriState.msoCTrue,MsoTriState.msoCTrue,MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
foreach (string pot in potFiles)
{
string dir = pot.Substring(srcRoot.Length+1);
string fullDir = Path.Combine(dstRoot, dir);
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting POT File (" + fullDir + ")");
Presentation p = ac.Presentations.Open(pot,MsoTriState.msoCTrue,MsoTriState.msoCTrue,MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
foreach (string pptx in pptxFiles)
{
string dir = pptx.Substring(srcRoot.Length+1);
string fullDir = Path.Combine(dstRoot, Path.GetFileName(dir));
fullDir = fullDir.Substring(0, fullDir.Length - 4);
Directory.CreateDirectory(fullDir);
Console.WriteLine("Converting PPTX File (" + fullDir + ")");
Presentation p = ac.Presentations.Open2007(pptx,MsoTriState.msoCTrue,MsoTriState.msoCTrue,MsoTriState.msoFalse,MsoTriState.msoFalse);
p.SaveAs(fullDir, PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoCTrue);
p.Close();
}
Console.WriteLine("All done, press enter key to exit");
Console.ReadLine();
}
}
}