C#学习笔记(23)——C#将PPT批量转为JPG(aspose方法)

说明(2017-7-31 18:30:25):

1. 最主要的是下载到aspose的破解文件,我在这里下载的http://www.lenosoft.net/down/10205.htm,如果不差钱可以买正版,也就一万多。有试用版,不过转换完有水印,水印很大,很大。

2. aspose没有给出直接将PPT转为图片的方法,只能是先将PPT转为PDF,再将PDF转为图片。所以只用到了两个dll文件,Aspose.Slides和Aspose.Pdf。

3. 其实有个网站上有教程,是aspose的中国代理,里面的英文视频教学免费,但中文的收费,还有一些文档是免费的,还是比较厚道的。http://training.evget.com/video/5975

4. 但是总感觉这种方法还是麻烦,虽然aspose号称最大的优势是不用安装office也能操作,但是你既然要做这个功能电脑不装office不是有病?觉得微软应该有一套自己的解决方案,再研究研究,毕竟两个dll拖家带口的也好几十兆,一堆方法用起来也是累。

5. 网上找资料的时候,博客里基本上都是那一篇公司项目里的部分代码,最关键的部分居然封装到他们自己的类里面了,而且没有贴上这个类的代码,这你让我们用个鬼!

6. 我发现自己做一个功能的时候,看到网上博客里长篇大论,总感觉不至于这么难吧?应该很少代码就能实现了,然后就自己写,果然少写不少,难道是我太聪明了?又或许是我代码不够严谨?去他娘的,能用就行!出了事再找问题。

xaml:

<Window x:Class="PPTtoJPG.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid>
<Button Content="打开" HorizontalAlignment="Left" Height="" Margin="370,64,0,0" VerticalAlignment="Top" Width="" Click="Button_Click"/>
<Button Content="开始转成PDF" HorizontalAlignment="Left" Height="" Margin="48,205,0,0" VerticalAlignment="Top" Width="" Click="Button_Click_1"/>
<Label Content="PPT路径" HorizontalAlignment="Left" Height="" Margin="34,64,0,0" VerticalAlignment="Top" Width=""/>
<TextBox Name="txtBoxPath" HorizontalAlignment="Left" Height="" Margin="129,66,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width=""/>
<Button Content="开始转成图片" HorizontalAlignment="Left" Height="" Margin="307,205,0,0" VerticalAlignment="Top" Width="" Click="Button_Click_2"/> </Grid>
</Window>

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Aspose.Slides;
using System.Reflection;
using Aspose.Pdf;
using System.Drawing.Imaging;
using System.Drawing;
using Aspose.Pdf.Devices; namespace PPTtoJPG
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
File.Delete("log.txt"); }
private void ShowLog(string log)
{
File.AppendAllText("log.txt", "\r\n" + DateTime.Now.ToString() + ": " + log);
}
private void Button_Click(object sender, RoutedEventArgs e)
{ }
//PPT转成PDF
private void Button_Click_1(object sender, RoutedEventArgs e)
{ //Crack();
string pptPath = txtBoxPath.Text;
if (pptPath != "" && Directory.Exists(pptPath))
{
DirectoryInfo pptInfos = new DirectoryInfo(pptPath); foreach (FileInfo pptInfo in pptInfos.GetFiles("*.ppt*"))
{
//如果已经存在这个pdf就跳过
if (File.Exists(pptInfo.FullName.Split('.')[] + ".pdf"))
{
continue;
}
try
{
Presentation pres = new Presentation(pptInfo.FullName); pres.Save(pptInfo.FullName.Split('.')[] + ".pdf", Aspose.Slides.Export.SaveFormat.Pdf); Console.WriteLine(pptInfo.FullName.Split('.')[] + ".pdf");
}
catch (Exception)
{ //throw;
Console.WriteLine("无法生成" + pptInfo.FullName.Split('.')[] + ".pdf");
ShowLog("无法生成" + pptInfo.FullName.Split('.')[] + ".pdf");
//MessageBox.Show("无法生成" + pptInfo.FullName.Split('.')[0] + ".pdf");
continue;
} }
MessageBox.Show("转换pdf完成!");
}
else
{
MessageBox.Show("路径为空或不存在!");
}
} //PDF转成JPG
private void Button_Click_2(object sender, RoutedEventArgs e)
{
string pdfPath = txtBoxPath.Text;
if (pdfPath != "" && Directory.Exists(pdfPath))
{
DirectoryInfo pdfInfos = new DirectoryInfo(pdfPath); foreach (FileInfo pdfInfo in pdfInfos.GetFiles("*.pdf"))
{ Directory.CreateDirectory(pdfInfo.FullName.Split('.')[]);
//open document
Document pdfDoc = new Document(pdfInfo.FullName);
for (int i = ; i < pdfDoc.Pages.Count; i++)
{
string imgPath = pdfInfo.FullName.Split('.')[] + @"\" + (i + ).ToString() + ".jpg";
using (FileStream imageStream = new FileStream(imgPath, FileMode.Create))
{
//Width, Height, Resolution, Quality
//Quality [0-100], 100 is Maximum
//create Resolution object
//300是分辨率
Resolution resolution = new Resolution();
//默认是3000x2250尺寸,100是画质最高
JpegDevice jpegDevice = new JpegDevice(, , resolution, );
//convert a particular page and save the image to stream
jpegDevice.Process(pdfDoc.Pages[i + ], imageStream);
//close stream
imageStream.Close();
}
}
}
MessageBox.Show("转换图片完成!");
}
else
{
MessageBox.Show("路径为空或不存在!");
}
}
}
}
上一篇:Java安全技术


下一篇:js之无缝轮播图