教你如何学习51CTO的文档预览功能(.NET版)

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://liangxiao.blog.51cto.com/3626612/1337374

     Every day is a new day.Hello,technology curtilages.

     最近接了几个小项目,也是要备战考试,所以就没写什么大项目了,只是一些功能模块。前几日发现51CTO的下载技术文档区域有了预览功能感觉不错哦,虽然不知道怎么写的,但是感觉很是方便呢。也没怎么留意代码,直到接到“活儿”发现要做一个同样的功能,我当时就感觉傻眼了,这我哪会?但是没办法,接下来的东西不能退!硬着头皮上呗,当天我在网上搜索了近百篇网页,发现现在流行的有三种:Print2Flash,FlexPaper+SWFTools,flash2paper.

    首先说flash2paper,这个版本的支持32位系统,不支持64位,但是在以前通用的比较广泛,现在已经是“XP”的阶段了。PASS!然后是Print2Flash,目前用的还是比较多的,但是有个缺点是它会自动在系统中生成以恶搞虚拟的打印机,到时候你办公的时候需要重新修改设置打印机,很是麻烦,如果你是小白的话。PASS!最后我们选择的是FlexPaper+SWFTools,和51CTO的一样。找了一个世界,最终还是回到故乡。很亲切!C_0004.gif

     在线预览文档原理:把你上传的任何格式的文档统一转换成flash播放器能够识别的格式,也就是swf播放出来,效果就是文档预览了。道理都懂,但是真正去做的话,大牛也未必能一招就可以破敌。后期的调试我深有体会,虽然做这个东西我两天就倒持出来了,但是基本上没吃饭没喝水,过程很艰辛。因为没有接触过这样的东西,所以学习的过程是自己的事儿。

     下面开始教程,首先我看了看51CTO的书写格式:

104000710.jpg

     熟悉一下FlexPaper的属性设置,找到传值的url,如下图,

104640840.png

    这样我们大概就会明白FlexPaper的配置了,接着来看SWFTools,这个东西呢是统一把pdf文档转换为swf,这样的话,思路就出来了,上传的时候给个判断,

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if( fileExt == ".doc" || fileExt == ".docx")
{
    office2pdf.DOCConvertToPDF(sourcePath, targetPath);
}
if( fileExt == ".ppt" ||fileExt == ".pptx")
{
    office2pdf.PPTConvertToPDF(sourcePath, targetPath);
}
if( fileExt == ".xlsx" || fileExt == ".xls")
{
    office2pdf.XLSConvertToPDF(sourcePath, targetPath);
}
if (fileExt == ".pdf")
{
    //不做处理
}

      把office的文档格式统一成pdf就可以了,然后利用swftools转换为swf,就可以很好预览文档。 说起来似乎很简单,但是做的话,问题也会出现不少。

     接下来我给你介绍office2pdf转换类的写法:你需要引用四个dll,分别是Microsoft.Office.Interop.Excel,Microsoft.Office.Interop.PowerPoint,Microsoft.Office.Interop.Word,office。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
namespace Document_preview.commonClass
{
    /// <summary>
    /// Office2Pdf 将Office文档转化为pdf
    /// </summary>
    public class Office2Pdf
    {
        public Office2Pdf()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// Word转换成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public 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,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, 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, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        /// <summary>
        /// 把Excel文件转换成PDF格式文件
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public 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 target = targetPath;
                object type = targetType;
                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        ///<summary>    
        /// 把PowerPoint文件转换成PDF格式文件   
        ///</summary>    
        ///<param name="sourcePath">源文件路径</param> 
        ///<param name="targetPath">目标文件路径</param>
        ///<returns>true=转换成功</returns>
        public bool PPTConvertToPDF(string sourcePath, string targetPath)
        {
            bool result;
            PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
            object missing = Type.Missing;
            PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new PowerPoint.ApplicationClass();
                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;
        }
    }
}

      写法类后就是调用,格式为office2pdf.DOCConvertToPDF(sourcePath, targetPath)。

完成office2pdf的转换,就开始pdf2swf,利用swftools.

 

1
2
3
4
5
6
7
8
9
string fileName = ViewState["fileName"].ToString();
                    string cmdStr = HttpContext.Current.Server.MapPath("~/SWFTools/pdf2swf.exe");
                    string savePath = HttpContext.Current.Server.MapPath("~/WordManage/word/");
                    //string aa = DateTime.Now.ToString("yyyymmddhhmmss");
                    string sourcePath = @"""" + savePath + TextBox11.Text.ToString() + ".pdf" + @"""";
                    string targetPath = @"""" + savePath + TextBox11.Text.ToString() + ".swf" + @"""";
                                                                                                             
                    string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
                    ExcutedCmd(cmdStr, argsStr);

      这样上传完成后,我们会在根目录下找到如下图的文件,

110527596.png

      最后结合Bootstrap的弹出层,可以做出如下图的效果,

110659684.jpg

    不知道你感觉如何,反正我倒持了两天,感觉能出来这样的效果,我觉得可以交差了,呵呵。我是一个比较容易满足的人。最后谢谢我的导师每天问我吃喝拉撒的琐事儿,让她操心,亦师亦母啊!可令天下父母心,唯有努力前进才能成为甄嬛,呵呵。(随后我会把项目独立出来把demo给大家上传)

110919270.jpg

demo下载地址:http://down.51cto.com/data/1052309

本文出自 “梁肖技术中心” 博客,请务必保留此出处http://liangxiao.blog.51cto.com/3626612/1337374

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://liangxiao.blog.51cto.com/3626612/1337374

     Every day is a new day.Hello,technology curtilages.

     最近接了几个小项目,也是要备战考试,所以就没写什么大项目了,只是一些功能模块。前几日发现51CTO的下载技术文档区域有了预览功能感觉不错哦,虽然不知道怎么写的,但是感觉很是方便呢。也没怎么留意代码,直到接到“活儿”发现要做一个同样的功能,我当时就感觉傻眼了,这我哪会?但是没办法,接下来的东西不能退!硬着头皮上呗,当天我在网上搜索了近百篇网页,发现现在流行的有三种:Print2Flash,FlexPaper+SWFTools,flash2paper.

    首先说flash2paper,这个版本的支持32位系统,不支持64位,但是在以前通用的比较广泛,现在已经是“XP”的阶段了。PASS!然后是Print2Flash,目前用的还是比较多的,但是有个缺点是它会自动在系统中生成以恶搞虚拟的打印机,到时候你办公的时候需要重新修改设置打印机,很是麻烦,如果你是小白的话。PASS!最后我们选择的是FlexPaper+SWFTools,和51CTO的一样。找了一个世界,最终还是回到故乡。很亲切!C_0004.gif

     在线预览文档原理:把你上传的任何格式的文档统一转换成flash播放器能够识别的格式,也就是swf播放出来,效果就是文档预览了。道理都懂,但是真正去做的话,大牛也未必能一招就可以破敌。后期的调试我深有体会,虽然做这个东西我两天就倒持出来了,但是基本上没吃饭没喝水,过程很艰辛。因为没有接触过这样的东西,所以学习的过程是自己的事儿。

     下面开始教程,首先我看了看51CTO的书写格式:

104000710.jpg

     熟悉一下FlexPaper的属性设置,找到传值的url,如下图,

104640840.png

    这样我们大概就会明白FlexPaper的配置了,接着来看SWFTools,这个东西呢是统一把pdf文档转换为swf,这样的话,思路就出来了,上传的时候给个判断,

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if( fileExt == ".doc" || fileExt == ".docx")
{
    office2pdf.DOCConvertToPDF(sourcePath, targetPath);
}
if( fileExt == ".ppt" ||fileExt == ".pptx")
{
    office2pdf.PPTConvertToPDF(sourcePath, targetPath);
}
if( fileExt == ".xlsx" || fileExt == ".xls")
{
    office2pdf.XLSConvertToPDF(sourcePath, targetPath);
}
if (fileExt == ".pdf")
{
    //不做处理
}

      把office的文档格式统一成pdf就可以了,然后利用swftools转换为swf,就可以很好预览文档。 说起来似乎很简单,但是做的话,问题也会出现不少。

     接下来我给你介绍office2pdf转换类的写法:你需要引用四个dll,分别是Microsoft.Office.Interop.Excel,Microsoft.Office.Interop.PowerPoint,Microsoft.Office.Interop.Word,office。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Word = Microsoft.Office.Interop.Word;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Microsoft.Office.Core;
namespace Document_preview.commonClass
{
    /// <summary>
    /// Office2Pdf 将Office文档转化为pdf
    /// </summary>
    public class Office2Pdf
    {
        public Office2Pdf()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        /// <summary>
        /// Word转换成pdf
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public 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,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, ref paramMissing,
                    ref paramMissing, ref paramMissing, 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, ref paramMissing);
                    wordDocument = null;
                }
                if (wordApplication != null)
                {
                    wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
                    wordApplication = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        /// <summary>
        /// 把Excel文件转换成PDF格式文件
        /// </summary>
        /// <param name="sourcePath">源文件路径</param>
        /// <param name="targetPath">目标文件路径</param>
        /// <returns>true=转换成功</returns>
        public 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 target = targetPath;
                object type = targetType;
                workBook = application.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
                    missing, missing, missing, missing, missing, missing, missing, missing, missing);
                workBook.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
                result = true;
            }
            catch
            {
                result = false;
            }
            finally
            {
                if (workBook != null)
                {
                    workBook.Close(true, missing, missing);
                    workBook = null;
                }
                if (application != null)
                {
                    application.Quit();
                    application = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            return result;
        }
        ///<summary>    
        /// 把PowerPoint文件转换成PDF格式文件   
        ///</summary>    
        ///<param name="sourcePath">源文件路径</param> 
        ///<param name="targetPath">目标文件路径</param>
        ///<returns>true=转换成功</returns>
        public bool PPTConvertToPDF(string sourcePath, string targetPath)
        {
            bool result;
            PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
            object missing = Type.Missing;
            PowerPoint.ApplicationClass application = null;
            PowerPoint.Presentation persentation = null;
            try
            {
                application = new PowerPoint.ApplicationClass();
                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;
        }
    }
}

      写法类后就是调用,格式为office2pdf.DOCConvertToPDF(sourcePath, targetPath)。

完成office2pdf的转换,就开始pdf2swf,利用swftools.

 

1
2
3
4
5
6
7
8
9
string fileName = ViewState["fileName"].ToString();
                    string cmdStr = HttpContext.Current.Server.MapPath("~/SWFTools/pdf2swf.exe");
                    string savePath = HttpContext.Current.Server.MapPath("~/WordManage/word/");
                    //string aa = DateTime.Now.ToString("yyyymmddhhmmss");
                    string sourcePath = @"""" + savePath + TextBox11.Text.ToString() + ".pdf" + @"""";
                    string targetPath = @"""" + savePath + TextBox11.Text.ToString() + ".swf" + @"""";
                                                                                                             
                    string argsStr = "  -t " + sourcePath + " -s flashversion=9 -o " + targetPath;
                    ExcutedCmd(cmdStr, argsStr);

      这样上传完成后,我们会在根目录下找到如下图的文件,

110527596.png

      最后结合Bootstrap的弹出层,可以做出如下图的效果,

110659684.jpg

    不知道你感觉如何,反正我倒持了两天,感觉能出来这样的效果,我觉得可以交差了,呵呵。我是一个比较容易满足的人。最后谢谢我的导师每天问我吃喝拉撒的琐事儿,让她操心,亦师亦母啊!可令天下父母心,唯有努力前进才能成为甄嬛,呵呵。(随后我会把项目独立出来把demo给大家上传)

110919270.jpg

demo下载地址:http://down.51cto.com/data/1052309

本文出自 “梁肖技术中心” 博客,请务必保留此出处http://liangxiao.blog.51cto.com/3626612/1337374

posted @ 2015-12-03 18:53  印象钟情  阅读(165)  评论(0编辑  收藏  举报