• 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="350" Width="525">
        <Grid>
            <Button Content="打开" HorizontalAlignment="Left" Height="32" Margin="370,64,0,0" VerticalAlignment="Top" Width="103" Click="Button_Click"/>
            <Button Content="开始转成PDF" HorizontalAlignment="Left" Height="40" Margin="48,205,0,0" VerticalAlignment="Top" Width="103" Click="Button_Click_1"/>
            <Label Content="PPT路径" HorizontalAlignment="Left" Height="32" Margin="34,64,0,0" VerticalAlignment="Top" Width="77"/>
            <TextBox Name="txtBoxPath" HorizontalAlignment="Left" Height="35" Margin="129,66,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="227"/>
            <Button  Content="开始转成图片" HorizontalAlignment="Left" Height="40" Margin="307,205,0,0" VerticalAlignment="Top" Width="117" 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", "
    " + 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('.')[0] + ".pdf"))
                        {
                            continue;
                        }
                        try
                        {
                            Presentation pres = new Presentation(pptInfo.FullName);
    
                            pres.Save(pptInfo.FullName.Split('.')[0] + ".pdf", Aspose.Slides.Export.SaveFormat.Pdf);
    
                            Console.WriteLine(pptInfo.FullName.Split('.')[0] + ".pdf");
                        }
                        catch (Exception)
                        {
    
                            //throw;
                            Console.WriteLine("无法生成" + pptInfo.FullName.Split('.')[0] + ".pdf");
                            ShowLog("无法生成" + pptInfo.FullName.Split('.')[0] + ".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('.')[0]);
                        //open document
                        Document pdfDoc = new Document(pdfInfo.FullName);
                        for (int i = 0; i < pdfDoc.Pages.Count; i++)
                        {
                            string imgPath = pdfInfo.FullName.Split('.')[0] + @"" + (i + 1).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(300);
                                //默认是3000x2250尺寸,100是画质最高
                                JpegDevice jpegDevice = new JpegDevice(960, 720, resolution, 90);
                                //convert a particular page and save the image to stream
                                jpegDevice.Process(pdfDoc.Pages[i + 1], imageStream);
                                //close stream
                                imageStream.Close();
                            }
                        }
                    }
                    MessageBox.Show("转换图片完成!");
                }
                else
                {
                    MessageBox.Show("路径为空或不存在!");
                }
            }
        }
    }
  • 相关阅读:
    正则表达式详解<一>
    multimap详讲
    map详讲<二>
    map详解<一>
    priority_queue详解
    容器适配器(一):queue
    用 input() 函数返回的数据是字符串类型
    学习python的基本了解
    学习oracle的SQL语句 练习
    oracle 练习题
  • 原文地址:https://www.cnblogs.com/Jacklovely/p/7264954.html
Copyright © 2020-2023  润新知