加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

将office文件转化为swf

发布时间:2020-12-15 17:38:04 所属栏目:百科 来源:网络整理
导读:1. 开发环境和开发工具 Win7 Visual Studio 2008 SaveAsPDFandXPS SwfTools 2. 环境准备 ? ? ? ?FlashPaper是一种不错的工具,但是没能找到破解版,结果只实现txt文件的转化,不得已采用了一种中间办法,使用SaveAsPDFandXPS将先office文件先转化成pdf,然后

1. 开发环境和开发工具

  • Win7
  • Visual Studio 2008
  • SaveAsPDFandXPS
  • SwfTools

2. 环境准备

? ? ? ?FlashPaper是一种不错的工具,但是没能找到破解版,结果只实现txt文件的转化,不得已采用了一种中间办法,使用SaveAsPDFandXPS将先office文件先转化成pdf,然后再使用SwfTools工具将pdf转化成swf,因此需要先安装SaveAsPDFandXPS和SwfTools。
? ? ? ?SaveAsPDFandXPS: ? http://download.csdn.net/detail/white_smile/7279031
? ? ? ?SwfTools: ? ? http://download.csdn.net/detail/white_smile/7279037
? ? ? ?SwfTools安装完成后需要将整个文件夹拷贝到C盘目录下,并设置NETWORK SERVICE的访问权限。将SwfTools目录中的pdf2swf.exe复制到应用程序响应的目录下,如bin。

3. 代码

将功能全部封装在一个Pdf.cs文件中:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Text;
//using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
using System.IO;
using System.Diagnostics;

/// <summary>
///Pdf 的摘要说明
/// </summary>
public class Pdf
{
    int ifsucess = 0;
    int iffail = 0;
    int allcount = 0;
    int num = 0;

    public Pdf()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    //将WORD文件转化为PDF文件
    public static bool DOCConvertToPDF(string sourcePath,string targetPath)
    {
        bool result = false;
        Word.WdExportFormat exportFormat = Word.WdExportFormat.wdExportFormatPDF;
        object paramMissing = Type.Missing;
        Word.ApplicationClass wordApplication = new Word.ApplicationClass();
        Word.Document wordDocument = null;
        try
        {
            object paramSourceDocPath = sourcePath;
            string paramExportFilePath = targetPath;

            Word.WdExportFormat paramExportFormat = exportFormat;
            bool paramOpenAfterExport = false;
            Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
            Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
            int paramStartPage = 0;
            int paramEndPage = 0;
            Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
            bool paramIncludeDocProps = true;
            bool paramKeepIRM = true;
            Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
            bool paramDocStructureTags = true;
            bool paramBitmapMissingFonts = true;
            bool paramUseISO19005_1 = false;

            wordDocument = wordApplication.Documents.Open(
                            ref paramSourceDocPath,ref paramMissing,ref paramMissing);

            if (wordDocument != null)
                wordDocument.ExportAsFixedFormat(paramExportFilePath,paramExportFormat,paramOpenAfterExport,paramExportOptimizeFor,paramExportRange,paramStartPage,paramEndPage,paramExportItem,paramIncludeDocProps,paramKeepIRM,paramCreateBookmarks,paramDocStructureTags,paramBitmapMissingFonts,paramUseISO19005_1,ref paramMissing);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (wordDocument != null)
            {
                wordDocument.Close(ref paramMissing,ref paramMissing);
                wordDocument = null;
            }
            if (wordApplication != null)
            {
                wordApplication.Quit(ref paramMissing,ref paramMissing);
                wordApplication = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }

    //将EXCEL文件转化为PDF文件
    public static bool XLSConvertToPDF(string sourcePath,string targetPath)
    {
        bool result = false;
        Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
        object missing = Type.Missing;
        Excel.ApplicationClass application = null;
        Excel.Workbook workBook = null;
        try
        {

            application = new Excel.ApplicationClass();
            object paramSourceDocPath = sourcePath;
            object target = targetPath;
            //object type = targetType;

            workBook = application.Workbooks.Open(sourcePath,missing,missing); 

            //Response.Write("Hello");
            workBook.ExportAsFixedFormat(targetType,target,Excel.XlFixedFormatQuality.xlQualityStandard,true,false,missing);
            result = true;

        }
        catch
        {
            result = false;
        }
        finally
        {
            if (workBook != null)
            {
                workBook.Close(true,missing);
                workBook = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }

    //将PPT转化成PDF文件
    public static bool PPTConvertToPDF(string sourcePath,string targetPath)
    {
        bool result;
        PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
        object missing = Type.Missing;
        PowerPoint.ApplicationClass application = new PowerPoint.ApplicationClass();
        PowerPoint.Presentation persentation = null;
        try
        {
            persentation = application.Presentations.Open(sourcePath,MsoTriState.msoTrue,MsoTriState.msoFalse,MsoTriState.msoFalse);
            persentation.SaveAs(targetPath,targetFileType,Microsoft.Office.Core.MsoTriState.msoTrue);
            result = true;
        }
        catch
        {
            result = false;
        }
        finally
        {
            if (persentation != null)
            {
                persentation.Close();
                persentation = null;
            }
            if (application != null)
            {
                application.Quit();
                application = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return result;
    }

    //要在添加引用选项中选在Microsoft office object 12 library和Microsoft office web component//
    public static bool PDF2SWF(string pdfPath,string swfPath,int page)
    {
        string exe = HttpContext.Current.Server.MapPath("~/Bin/pdf2swf.exe");
        pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
        swfPath = HttpContext.Current.Server.MapPath(swfPath);
        if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))
        {
            return false;
        }
        StringBuilder sb = new StringBuilder();
        sb.Append(" "" + pdfPath + """);//input
        sb.Append(" -o "" + swfPath + """);//output
        //sb.Append(" -z");
        sb.Append(" -s flashversion=9");//flash version
        //sb.Append(" -s disablelinks");//禁止PDF里面的链接
        sb.Append(" -p " + ""1" + "-" + page + """);//page range
        sb.Append(" -j 100");//SWF中的图片质量
        string Command = sb.ToString();
        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.FileName = exe;
        p.StartInfo.Arguments = Command;
        p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");
        p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序 启动 线程
        //p.StartInfo.RedirectStandardInput = true;
        //p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,pdf2swf.exe的所有输出信息,都为错误输出流,用 StandardOutput是捕获不到任何消息的...
        p.StartInfo.CreateNoWindow = false;//不创建进程窗口
        p.Start();//启动线程
        p.BeginErrorReadLine();//开始异步读取
        p.WaitForExit();//等待完成
        //p.StandardError.ReadToEnd();//开始同步读取
        p.Close();//关闭进程
        p.Dispose();//释放资源
        return true;
    }
    public static bool PDF2SWF2(string pdfPath,string swfPath)
    {
        return PDF2SWF(pdfPath,swfPath,GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)));
    }

    public static int GetPageCount(string pdfPath)
    {
        //try
        //{
        byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
        int length = buffer.Length;
        if (buffer == null)
            return -1;
        if (buffer.Length <= 0)
            return -1;
        string pdfText = Encoding.Default.GetString(buffer);
        System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Types*/Page[^s]");
        System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
        return matches.Count;
        //}
        //catch (Exception ex)
        //{
        //    throw ex;
        //}
    }
}

4. 支持库的引入

? ? ? ? 选中工程名右击——>添加引用选项——>选中COM。在com一栏中选中如下内容,点击确定。然后就可以正常编译Pdf.cs文件了。
? ?

? ??

5. 附件

? ? ? 完整的Web工程,并使用了FlexPaper来显示Swf文件,这里就不多做解释了。
? ? ?? http://download.csdn.net/detail/white_smile/7279087

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读