.net mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法

时间:2023-03-09 15:40:50
.net  mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法

FlexPaper插件可以实现在浏览器中在线预览pdf,word,excel等。

在网上看到很多关于这个插件实现预览的技术,但是很难做到word和excel在线预览。

pdf很好实现。

首先下载相关的插件信息,这里不多说了。

其中这个插件主要需要配合Aspose来实现将上传的excel和word来转换为pdf。再通过pdf2swf来将pdf转换为swf格式。才能在前段在线预览。

1.所以这里还需要下载Aspose.dll  和Aspose.Cells.dll(处理Excel)还有Aspose.Words.dll(处理word)

有了dll之后,需要写一个cs类,专门处理word和excel转换为pdf,以及pdf转换为swf的方法。

直接推荐这篇文章里面已经实现的方法。

http://www.bkjia.com/Asp_Netjc/890896.html

通过这个类实现的方法

.net  mvc使用FlexPaper插件实现在线预览PDF,EXCEL,WORD的方法

2.结合我们上传的文件路径,来实现将上传的文件格式转换以及在线预览。

首先配置前段页面信息

  <input type="hidden" id="_filename" value="/./../../@Model" />
<div style="margin:0 auto;width:100%;">
<div id="flashContent" style="display:none;">
<p>
To view this page ensure that Adobe Flash Player version
10.0.0 or greater is installed.
</p> </div> <script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/swfupload/flexpaper_flash.js"></script>
<script src="~/Scripts/swfupload/swfobject.js"></script> <script type="text/javascript">
var _filename = $("#_filename").val(); // alert(_filename)
//alert(_filename);
var swfVersionStr = "9.0.0";
var xiSwfUrlStr = "playerProductInstall.swf";
var flashvars = {
SwfFile: escape(_filename),
SwfFile: _filename,
// SwfFile: "/./../../UpDir/expressInstall.swf",
Scale: 0.6,
ZoomTransition: "easeOut",
ZoomTime: 0.5,
ZoomInterval: 0.1,
FitPageOnLoad: false,
FitWidthOnLoad: true,
PrintEnabled: true,
FullScreenAsMaxWindow: false,
ProgressiveLoading: true,
PrintToolsVisible: true,
ViewModeToolsVisible: true,
ZoomToolsVisible: true,
FullScreenVisible: true,
NavToolsVisible: true,
CursorToolsVisible: true,
SearchToolsVisible: true,
SearchMatchAll: true,
localeChain: "zh_CN"
};
var params = {
quality: "high",
bgcolor: "#ffffff",
allowscriptaccess: "sameDomain",
allowfullscreen: "true",
wmode: "direct" }
var attributes = { id: "FlexPaperViewer", name: "FlexPaperViewer" };
54 swfobject.embedSWF("../../../../Scripts/FlexPaper/FlexPaperViewer.swf", "flashContent", "100%", "1000", swfVersionStr, xiSwfUrlStr, flashvars, params, attributes);
55
56 swfobject.createCSS("#flashContent", "display:block;text-align:left;");
</script>
</div>

一些主要的配置信息我已经标红,至于插件配置信息的属性功能,自行参考网上很多文档介绍,我用的就是一般上传。

3.接下来就是controller中实现的转换方法,我直接将路径传给了model返回给页面显示了。

                        //swf文件路径
string path = Server.MapPath("~\\UpDir") + node.FilePath.Substring().Replace('\\', '/') + ".swf";
//SIO是system.io 我重命名了
SIO.FileInfo fileinfo = new SIO.FileInfo(path);
if (fileinfo.Exists)
{
return View((object)(node.FilePath + ".swf"));
}
else
{
try
{
//上传的源文件地址
string sourcefile = Server.MapPath("~\\UpDir") + node.FilePath.Substring();
string FileName = node.name;
string ext = node.FilePath.Substring(node.FilePath.LastIndexOf(".") + ); if (ext.ToLower() == "doc" || ext.ToLower() == "docx")
{
string pdffile = sourcefile;
string swffile = Server.MapPath("~\\UpDir") + node.FilePath.Substring().Replace('\\', '/');
string path1 = Server.MapPath("~\\UpDir") + node.FilePath.Substring().Replace('\\', '/') + ".swf";
Aspose.Words.Document doc = new Aspose.Words.Document(pdffile);
25 doc.Save(swffile, Aspose.Words.SaveFormat.Pdf); AsposeUtils.ConvertDocToPdF(swffile, path1);
AsposeUtils.PDFConvertToSwf(swffile, path1, AsposeUtils.GetPageCountByPDF(swffile));
}
else if (ext.ToLower() == "xls" || ext.ToLower() == "xlsx")
{
string pdffile = sourcefile;
string swffile = Server.MapPath("~\\UpDir") + node.FilePath.Substring().Replace('\\', '/') + ".pdf";
string path1 = Server.MapPath("~\\UpDir") + node.FilePath.Substring().Replace('\\', '/') + ".swf"; Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(pdffile); workbook.Save(swffile, Aspose.Cells.SaveFormat.Pdf); //AsposeUtils.ConvertExcelToHtml(swffile, swffile);
42 AsposeUtils.PDFConvertToSwf(swffile, path1, AsposeUtils.GetPageCountByPDF(swffile));
}
else if (ext.ToLower() == "pdf")
{ try
{
string pdffile = sourcefile;
string swffile = path; SIO.File.Copy(pdffile, swffile, true);
AsposeUtils.PDFConvertToSwf(pdffile, swffile, AsposeUtils.GetPageCountByPDF(pdffile)); }
catch (Exception ex)
{
string str = ex.Message;
}
}
else if (ext.ToLower() == "ppt" || ext.ToLower() == "pptx")
{
string pdffile = sourcefile;
string swffile = path; AsposeUtils.ConvertPowerPointToPdf(sourcefile, swffile);
AsposeUtils.PDFConvertToSwf(pdffile, swffile, AsposeUtils.GetPageCountByPDF(pdffile));
}
else if (ext.ToLower() == "txt")
{
string pdffile = sourcefile;
string swffile = path; Txt2Pdf.zscsc(sourcefile, swffile);
AsposeUtils.PDFConvertToSwf(pdffile, swffile, AsposeUtils.GetPageCountByPDF(pdffile));
}
else if (ext.ToLower() == "jpg" || ext.ToLower() == "jpeg" || ext.ToLower() == "png")
{
return View((object)node.FilePath);
}
else
{
// ViewBag.FileName = @"/./../../UpDir/"+ project.ProjectCode + "/" + contract.ContractType + "/" + contract.Id + ".swf"; ;
}

4.我用的上传插件师swfupload,个人觉得不怎么好用,后面用uploadfiy插件了。上传不说了,controller中对word和excel的转换方法和我连接中文章使用的不一样。因为用文章中的总不能在前段使用flexpapaer预览成功。所以标黄的代码是我自己加上的。绿色代码是原先的代码。可以自己参考,两种方法哪个可行用哪个。