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

简介:

     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









本文转自 吴雨声 51CTO博客,原文链接:,如需转载请自行联系原作者      Every day is a new day.Hello,technology curtilages.


目录
相关文章
|
10天前
|
存储 文字识别 C#
.NET开源免费、功能强大的 Windows 截图录屏神器
今天大姚给大家分享一款.NET开源免费(基于GPL3.0开源协议)、功能强大、简洁灵活的 Windows 截图、录屏、Gif动图制作神器:ShareX。
|
2月前
|
人工智能 缓存 Kubernetes
.NET 9 首个预览版发布:瞄准云原生和智能应用开发
.NET 9 首个预览版发布:瞄准云原生和智能应用开发
|
1月前
|
Windows
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
windows server 2019 安装NET Framework 3.5失败,提示:“安装一个或多个角色、角色服务或功能失败” 解决方案
|
2月前
|
开发框架 前端开发 .NET
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
为了便于大家查找,特将之前开发的.Net Core相关的五大案例整理成文,共计440页,32w字,免费提供给大家,文章底部有PDF下载链接。
32 1
福利来袭,.NET Core开发5大案例,30w字PDF文档大放送!!!
|
2月前
|
C# Windows
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
.NET开源的一个小而快并且功能强大的 Windows 动态桌面软件
|
2月前
|
机器学习/深度学习 存储 编解码
多任务学习新篇章 | EMA-Net利用Cross-Task Affinity实现参数高效的高性能预测
多任务学习新篇章 | EMA-Net利用Cross-Task Affinity实现参数高效的高性能预测
42 0
|
3月前
|
NoSQL 关系型数据库 MongoDB
【DotNetGuide】C#/.NET/.NET Core学习、工作、面试指南
【DotNetGuide】C#/.NET/.NET Core学习、工作、面试指南
105 0
|
3月前
|
开发框架 .NET API
C#/.NET/.NET Core推荐学习书籍(23年12月更新)
C#/.NET/.NET Core推荐学习书籍(23年12月更新)
219 0
|
10天前
|
开发框架 .NET API
C#/.NET/.NET Core推荐学习书籍(已分类)
C#/.NET/.NET Core推荐学习书籍(已分类)
173 0
|
5月前
|
设计模式 程序员 开发工具
零基础程序员想要学好.Net,跟着这7个步骤学习就可以了
零基础程序员想要学好.Net,跟着这7个步骤学习就可以了
26 0