ppt或pdf转成swf文件的工具

2015-03-04 22:34:10  访问(1844) 赞(0) 踩(0)


相关下载:SlowX.PptToSwfApp[code].zip  SlowX.PptToSwfApp[release].zip  (本地)SlowX.PptToSwfApp[code]  (本地)SlowX.PptToSwfApp[release]     

源码cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using Microsoft.Office.Interop.PowerPoint;

namespace SlowX.PptToSwfApp
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_HomePage_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.slowx.net");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton_生成_Click(object sender, EventArgs e)
        {

            string fileName = comboBox_文件.Text.Trim();

            if (fileName.Length == 0)
            {
                MessageBox.Show("请选择文件。");
                comboBox_文件.Select();
                comboBox_文件.Focus();
                return;
            }



            FileInfo upFile = new FileInfo(fileName);
            FileInfo pdfFile = null;

            fileName = upFile.FullName;
            string lowerFileName = fileName.ToLower();

            if (lowerFileName.EndsWith(".ppt") || lowerFileName.EndsWith(".pptx"))
            {

                string onlyName = upFile.Name;

                int idx = onlyName.LastIndexOf('.');

                if (idx != -1)
                    onlyName = onlyName.Substring(0, idx);

                pdfFile = new FileInfo(upFile.Directory.FullName + "\\" + onlyName + ".pdf");

                string theResult = ToPdf(fileName, pdfFile.FullName);

                if (theResult != null && theResult.Length > 0)
                {
                    MessageBox.Show(theResult);
                    comboBox_文件.Select();
                    comboBox_文件.Focus();
                    return;
                }
            }
            else if (lowerFileName.EndsWith(".pdf"))
                pdfFile = upFile;

            if (pdfFile == null)
            {
                MessageBox.Show("请选择有效的文件。");
                comboBox_文件.Select();
                comboBox_文件.Focus();
                return;
            }

            string swfFile = FileChangeExtension(pdfFile.FullName, ".swf");

            pdfToSwfClick(pdfFile.FullName, swfFile);

        }

        /// <summary>
        /// <para>文件更改后缀</para>
        /// <para>比如:C:\\one\\test.doc 更改成 C:\\one\\test.pdf</para>
        /// </summary>
        /// <param name="fileName">文件名,如C:\\one\\test.doc</param>
        /// <param name="newExtension">后缀名,带.号,如.pdf</param>
        /// <returns></returns>
        public string FileChangeExtension(string fileName, string newExtension)
        {
            if (fileName == null || fileName.Length == 0)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "fileName == null || fileName.Length == 0"
                    );
            }

            int idx = fileName.LastIndexOf('.');

            if (idx == -1)
                return fileName + newExtension;
            else
                return fileName.Substring(0, idx) + newExtension;
        }



        /// <summary>
        /// <para>ppt转成jpg的方法</para>
        /// <para>添加两个引用,Microsoft.Office.Interop.PowerPoint;并在引用中把该引用的“嵌入互操作类型”属性改成false,还有office引用,就可以了!这是在windowform写的,web也是一样</para>
        /// <para>添加后还是编译错误,我后面把office全装了,而不是选择安装就可以了</para>
        /// <para>office里面有开发组件用安装,才能编译通过</para>
        /// </summary>
        /// <param name="pptFileName"></param>
        /// <returns></returns>
        public string ToPdf(string pptFileName, string pdfFile)
        {
            string theResult = "";

            object missing = Type.Missing;
            Microsoft.Office.Interop.PowerPoint.ApplicationClass application = null;
            Presentation persentation = null;

            try
            {
                // 要转换的文件物理路径
                string savePath = pdfFile;

                application = new Microsoft.Office.Interop.PowerPoint.ApplicationClass();

                persentation = application.Presentations.Open
                    (
                        pptFileName,
                        Microsoft.Office.Core.MsoTriState.msoTrue,
                        Microsoft.Office.Core.MsoTriState.msoFalse,
                        Microsoft.Office.Core.MsoTriState.msoFalse
                    );

                persentation.SaveAs
                    (
                        savePath, 
                        PpSaveAsFileType.ppSaveAsPDF, 
                        Microsoft.Office.Core.MsoTriState.msoTrue
                    );
            }
            catch (Exception err)
            {
                theResult = "转换异常:" + err.Message;
            }

            finally
            {
                if (persentation != null)
                {
                    persentation.Close();
                    persentation = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }

            return theResult;
        }
 


        /// <summary>
        /// 
        /// </summary>
        /// <param name="pdfFullName"></param>
        /// <param name="swfFullName"></param>
        protected void pdfToSwfClick(string pdfFullName, string swfFullName)
        {
            try
            {



                //切记,使用pdf2swf.exe 打开的文件名之间不能有空格,否则会失败 
                string cmdStr = comboBox_pdf2swf.Text.Trim();

                string sourcePath = @"""" + pdfFullName + @"""";
                string targetPath = @"""" + swfFullName + @"""";

                //@"""" 四个双引号得到一个双引号,如果你所存放的文件所在文件夹名有空格的话,要在文件名的路径前后加上双引号,才能够成功 
                // -t 源文件的路径 
                // -s 参数化(也就是为pdf2swf.exe 执行添加一些窗外的参数(可省略)) 
                string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;

                ExcutedCmd(cmdStr, argsStr);

                MessageBoxShowFileNameYesNoCancel(swfFullName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("发生异常:" + ex.Message);
            }
        }

        /// <summary>
        /// 保存成功后的提示操作
        /// </summary>
        /// <param name="dirName"></param>
        public void MessageBoxShowFileNameYesNoCancel
            (
                string fileName
            )
        {
            DialogResult dg = MessageBox.Show
                (
                       "保存成功,是否复制文件或打开文件(点No)?",
                       "选择操作",
                       MessageBoxButtons.YesNoCancel
                );

            if (dg == DialogResult.Cancel)
                return;

            if (dg == DialogResult.No)
            {
                System.Diagnostics.Process.Start("explorer.exe", fileName);

                return;
            }

            if (dg == DialogResult.Yes)
            {

                System.Collections.Specialized.StringCollection stringCollectionValue
                    = new System.Collections.Specialized.StringCollection();

                stringCollectionValue.Add(fileName);

                Clipboard.SetFileDropList(stringCollectionValue);
            }
        }



        private static void ExcutedCmd(string cmd, string args)
        {
            using (Process p = new Process())
            {
                ProcessStartInfo psi = new ProcessStartInfo(cmd, args);
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                p.StartInfo = psi;
                p.Start();

                p.WaitForExit();
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            comboBox_pdf2swf.Text = @"C:\MyProgram\pdf2swf\pdf2swf.exe";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            if (fd.ShowDialog() != DialogResult.OK)
                return;

            comboBox_pdf2swf.Text = fd.FileName;
        }

        private void button_打开文件_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();

            if (fd.ShowDialog() != DialogResult.OK)
                return;

            comboBox_文件.Text = fd.FileName;
        }


    }
}


源码Designer.cs

namespace SlowX.PptToSwfApp
{
    partial class MainForm
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.toolStripTextBox1 = new System.Windows.Forms.ToolStripTextBox();
            this.toolStripButton_pdf文件转swf文件 = new System.Windows.Forms.ToolStripButton();
            this.toolStripButton_HomePage = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.toolStripButton_生成 = new System.Windows.Forms.ToolStripButton();
            this.button_打开文件 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox_文件 = new System.Windows.Forms.ComboBox();
            this.button1 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.comboBox_pdf2swf = new System.Windows.Forms.ComboBox();
            this.toolStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripTextBox1,
            this.toolStripButton_pdf文件转swf文件,
            this.toolStripButton_HomePage,
            this.toolStripSeparator2,
            this.toolStripButton_生成});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(613, 25);
            this.toolStrip1.TabIndex = 61;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // toolStripTextBox1
            // 
            this.toolStripTextBox1.Name = "toolStripTextBox1";
            this.toolStripTextBox1.Size = new System.Drawing.Size(0, 25);
            // 
            // toolStripButton_pdf文件转swf文件
            // 
            this.toolStripButton_pdf文件转swf文件.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton_pdf文件转swf文件.Name = "toolStripButton_pdf文件转swf文件";
            this.toolStripButton_pdf文件转swf文件.Size = new System.Drawing.Size(111, 22);
            this.toolStripButton_pdf文件转swf文件.Text = "ppt文件转swf文件";
            // 
            // toolStripButton_HomePage
            // 
            this.toolStripButton_HomePage.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.toolStripButton_HomePage.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton_HomePage.Name = "toolStripButton_HomePage";
            this.toolStripButton_HomePage.Size = new System.Drawing.Size(171, 22);
            this.toolStripButton_HomePage.Text = "霜叶工作室(www.slowx.net)";
            this.toolStripButton_HomePage.Click += new System.EventHandler(this.toolStripButton_HomePage_Click);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(6, 25);
            // 
            // toolStripButton_生成
            // 
            this.toolStripButton_生成.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.toolStripButton_生成.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.toolStripButton_生成.Name = "toolStripButton_生成";
            this.toolStripButton_生成.Size = new System.Drawing.Size(36, 22);
            this.toolStripButton_生成.Text = "生成";
            this.toolStripButton_生成.Click += new System.EventHandler(this.toolStripButton_生成_Click);
            // 
            // button_打开文件
            // 
            this.button_打开文件.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.button_打开文件.Location = new System.Drawing.Point(557, 75);
            this.button_打开文件.Name = "button_打开文件";
            this.button_打开文件.Size = new System.Drawing.Size(31, 23);
            this.button_打开文件.TabIndex = 64;
            this.button_打开文件.Text = "…";
            this.button_打开文件.UseVisualStyleBackColor = true;
            this.button_打开文件.Click += new System.EventHandler(this.button_打开文件_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(31, 80);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 62;
            this.label1.Text = "文件:";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // comboBox_文件
            // 
            this.comboBox_文件.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBox_文件.FormattingEnabled = true;
            this.comboBox_文件.Location = new System.Drawing.Point(78, 77);
            this.comboBox_文件.Name = "comboBox_文件";
            this.comboBox_文件.Size = new System.Drawing.Size(473, 20);
            this.comboBox_文件.TabIndex = 63;
            // 
            // button1
            // 
            this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.button1.Location = new System.Drawing.Point(557, 36);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(31, 23);
            this.button1.TabIndex = 67;
            this.button1.Text = "…";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(13, 41);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(59, 12);
            this.label2.TabIndex = 65;
            this.label2.Text = "pdf2swf:";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // comboBox_pdf2swf
            // 
            this.comboBox_pdf2swf.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBox_pdf2swf.FormattingEnabled = true;
            this.comboBox_pdf2swf.Location = new System.Drawing.Point(78, 38);
            this.comboBox_pdf2swf.Name = "comboBox_pdf2swf";
            this.comboBox_pdf2swf.Size = new System.Drawing.Size(473, 20);
            this.comboBox_pdf2swf.TabIndex = 66;
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(613, 379);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.comboBox_pdf2swf);
            this.Controls.Add(this.button_打开文件);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.comboBox_文件);
            this.Controls.Add(this.toolStrip1);
            this.Name = "MainForm";
            this.Text = "www.slowx.net - ppt文件转swf文件";
            this.Load += new System.EventHandler(this.MainForm_Load);
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.ToolStrip toolStrip1;
        private System.Windows.Forms.ToolStripTextBox toolStripTextBox1;
        private System.Windows.Forms.ToolStripButton toolStripButton_pdf文件转swf文件;
        private System.Windows.Forms.ToolStripButton toolStripButton_HomePage;
        private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
        private System.Windows.Forms.Button button_打开文件;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboBox_文件;
        private System.Windows.Forms.ToolStripButton toolStripButton_生成;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboBox_pdf2swf;
    }
}


上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)