小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c# 文件類型轉(zhuǎn)換匯總(word/excel/ppt/txt/pdf 轉(zhuǎn)HTML&word/ppt 轉(zhuǎn)swf)

 實(shí)力決定地位 2015-03-24

c# 文件類型轉(zhuǎn)換匯總(word/excel/ppt/txt/pdf 轉(zhuǎn)HTML&word/ppt 轉(zhuǎn)swf)


 

 

最近項(xiàng)目中要用到文件預(yù)覽,分為用HTML預(yù)覽和用swf預(yù)覽兩種模式,所以要將用戶上傳的文件轉(zhuǎn)成可以預(yù)覽的格式。

首先是轉(zhuǎn)HTML部分,word、excel、ppt、txt都是調(diào)用Microsoft.Office來完成的,全部類似,所以只貼一個(gè)word的代碼:

public static bool WordToHtml(string path)
        {
            bool ret = true;
            Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = null;
            // 缺省參數(shù) 
            object Unknown = Type.Missing;
            try
            {

                // 指定原文件和目標(biāo)文件 
                object Source = path;
                object Target = path.Substring(0, path.LastIndexOf(".")) + ".html";
                //為了保險(xiǎn),只讀方式打開 
                object readOnly = true;
                // 指定另存為格式(html) 
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
                // 打開doc文件 
                doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                // 轉(zhuǎn)換格式 
                doc.SaveAs(ref Target, ref format,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
            }
word轉(zhuǎn)HTML

excel、ppt分別調(diào)用自己的application類來完成,txt用的類和word一樣。

比較麻煩的是PDF轉(zhuǎn)HTML的,最后我采用的是在stackoverflow上找到的支持命令行的pdftohtml.exe,然后在程序里開啟process調(diào)用就行了:

public static bool PdfToHtml(string path)
        {
            string convertPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "PDFtoHTML\\pdftohtml.exe";
            Process p = new Process();
            p.StartInfo.FileName = convertPath;
            p.StartInfo.Arguments = " -enc GBK " + path;
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            bool isStart = p.Start();
            p.WaitForExit();
            p.Close();
            return isStart;
        }
pdf轉(zhuǎn)html

再來是轉(zhuǎn)swf部分,flashpaper不支持64位,print2flash又需要花錢解鎖,所以只好走別的路了。

最后使用了先轉(zhuǎn)成pdf,再用swftools把pdf轉(zhuǎn)成swf的方式

其中轉(zhuǎn)pdf的模式也是調(diào)用Microsoft.Office來完成,以word為例:

private static bool wordToPdf(string path, string outpath = "")
        {
            bool ret = true;
            Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = null;
            // 缺省參數(shù) 
            object Unknown = Type.Missing;
            try
            {

                // 指定原文件和目標(biāo)文件 
                object Source = path;
                object Target = path.Substring(0, path.LastIndexOf(".")) + ".pdf";
                if (outpath != "")
                    Target = outpath;
                //為了保險(xiǎn),只讀方式打開 
                object readOnly = true;
                // 指定另存為格式(html) 
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;


                // 打開doc文件 
                doc = newApp.Documents.Open(ref Source, ref Unknown, ref readOnly,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                // 轉(zhuǎn)換格式 
                doc.SaveAs(ref Target, ref format,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
                    ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
            }
            catch (Exception e)
            {
                ret = false;
            }
            finally
            {
                if (doc != null)
                {
                    // 關(guān)閉文檔和Word程序 
                    object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;
                    doc.Close(ref saveChanges, ref Unknown, ref Unknown);
                }
                newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
                GC.Collect();
            }
           
word轉(zhuǎn)pdf

然后是調(diào)用swftools來轉(zhuǎn)換成swf,但是在服務(wù)器上不管怎么嘗試,直接使用process來啟動(dòng)pdf2swf.exe都轉(zhuǎn)換失敗,原因不明,最后使用把啟動(dòng)pdf2swf寫到BAT里,然后用process去調(diào)用BAT,就可以成功轉(zhuǎn)換了。

c:
cd C:\Program Files (x86)\SWFTools\
pdf2swf.exe -f -T 9 -t "%1" -o "%2"
調(diào)用pdf2swf.exe

其中一定要記得有-T的參數(shù),后面的數(shù)字是flash的版本,如果對(duì)不上的話是轉(zhuǎn)換不了的。

然后就是在程序里調(diào)用這個(gè)BAT,把路徑傳參進(jìn)去:

private static bool ChangeToSwf(string filePath, string swfPath, string basePath)
        {
            bool isStart = false;
            string type = filePath.Split('.').Last().ToLower();

            if (type != "pdf")
            {
                string tempPdf = basePath + "/swffiles/" + "temp" + Guid.NewGuid().ToString() + ".pdf";//臨時(shí)pdf的路徑
                if (!FileToTempPdf(filePath, tempPdf))//先把文件轉(zhuǎn)換成PDF
                {
                    return false;
                }
                else
                {
                    filePath = tempPdf;
                }
            }

            try
            {
                Process p = new Process();
                string path = basePath + "/plugin/ChangeSwf.bat";//bat路徑
                ProcessStartInfo pi = new ProcessStartInfo(path, filePath + " " + swfPath);//第二個(gè)參數(shù)為傳入的參數(shù),string類型以空格分隔各個(gè)參數(shù)
                pi.UseShellExecute = false;
                pi.RedirectStandardOutput = true;
                p.StartInfo = pi;
                isStart = p.Start();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return isStart;
        }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多