[Asp.net]使用flexpaper+swftools大文件分页转换实现在线预览

引言

之前总结了在线预览几种常见解决方案,可以戳这里:

http://www.cnblogs.com/wolf-sun/p/3569960.html

http://www.cnblogs.com/wolf-sun/p/3525437.html

http://www.cnblogs.com/wolf-sun/p/3574278.html

客户突然给了比较大的文档,赫然崩溃,项目中采用的是flexpaper+swftools方式实现的,发现在pdf-》swf的时候,转了100页之后,就会出现问题,很无奈,可能客户上传的word文档有问题,客户给的文档,页面方向有横向的,也有纵向的。没办法只能想办法解决了。

最后想到了将他们一页一页的转,说实话我都疯了,几百页的文档,抽支烟回来才转完,你不疯不行啊。

之后想了用其他几种解决方案,由于客户要求文档不能被下载,被复制,要有保密性,这需求,你想保密,想安全,就别放在网上啊,别人只要想要,一张一张的截图,也能给你的文档扣下来,想当年,考研那会儿,我都干过这事,考题都是从网上一张一张截图搞下来的。现在想想,当时真sb。

单页pdf转swf

 这里还是使用这篇文章中的demo:http://www.cnblogs.com/wolf-sun/p/3525437.html

然后修改PSD2SwfHelper类下的方法PDF2SWF和GetPageCount,将私有改为公有:

 1   /// <summary>
 2     /// PDF格式转为SWF
 3     /// </summary>
 4     /// <param name="pdfPath">PDF文件地址</param>
 5     /// <param name="swfPath">生成后的SWF文件地址</param>
 6     /// <param name="beginpage">转换开始页</param>
 7     /// <param name="endpage">转换结束页</param>
 8     public static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)
 9     {
10         //swftool,首先先安装,然后将安装目录下的东西拷贝到tools目录下
11         string exe = HttpContext.Current.Server.MapPath("~/Bin/tools/pdf2swf.exe");
12         pdfPath = HttpContext.Current.Server.MapPath(pdfPath);
13         swfPath = HttpContext.Current.Server.MapPath(swfPath);
14         if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))
15         {
16             return false;
17         }
18         StringBuilder sb = new StringBuilder();
19         sb.Append(" "" + pdfPath + """);
20         sb.Append(" -o "" + swfPath + """);
21         sb.Append(" -s flashversion=9");
22         if (endpage > GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);
23         sb.Append(" -p " + """ + beginpage + "" + "-" + endpage + """);
24         sb.Append(" -j " + photoQuality);
25         string Command = sb.ToString();
26         System.Diagnostics.Process p = new System.Diagnostics.Process();
27         p.StartInfo.FileName = exe;
28         p.StartInfo.Arguments = Command;
29         p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");
30         p.StartInfo.UseShellExecute = false;
31         p.StartInfo.RedirectStandardError = true;
32         p.StartInfo.CreateNoWindow = false;
33         p.Start();
34         p.BeginErrorReadLine();
35         p.WaitForExit();
36         p.Close();
37         p.Dispose();
38         return true;
39     }

使用:

 1  public partial class Test : System.Web.UI.Page
 2     {
 3         protected void Page_Load(object sender, EventArgs e)
 4         {
 5             string pdfPath="PDFFile/王牌2_C#_控件查询手册.pdf";
 6             
 7             int pageCount = PSD2swfHelper.GetPageCount(Server.MapPath(pdfPath));
 8             for (int i = 1; i <=pageCount; i++)
 9             {
10                 //i to i 当前页
11                 PSD2swfHelper.PDF2SWF(pdfPath, "SWFFile/" + i.ToString() + ".swf", i, i, 80);
12             }
13             //这里需要虚拟路径
14            // PSD2swfHelper.PDF2SWF("PDFFile/王牌2_C#_控件查询手册.pdf", "SWFFile/王牌2_C#_控件查询手册.swf");
15         }
16     }

结果:

修改预览页面,flexpaper配置信息:

参考文章:http://www.blogjava.net/jforeverg/archive/2011/07/06/353813.html

http://blog.csdn.net/mishidezhu/article/details/7094490

 1   var flashvars = {
 2             SwfFile: "{/SWFFile/[*,0].swf,52}",//这里需要修改
 3             Scale: 0.6,
 4             ZoomTransition: "easeOut",
 5             ZoomTime: 0.5,
 6             ZoomInterval: 0.1,
 7             FitPageOnLoad: false,
 8             FitWidthOnLoad: true,
 9             PrintEnabled: true,
10             FullScreenAsMaxWindow: false,
11             ProgressiveLoading: true,
12             PrintToolsVisible: true,
13             ViewModeToolsVisible: true,
14             ZoomToolsVisible: true,
15             FullScreenVisible: true,
16             NavToolsVisible: true,
17             CursorToolsVisible: true,
18             SearchToolsVisible: true,
19             localeChain: "en_US"
20         };

测试结果:

总结

这里只是将工作中遇到的问题,记录一下,这种方式,转换速度太慢了,也许你有更好的解决方案,不妨分享一下你的解决方案,不胜感激。

原文地址:https://www.cnblogs.com/wolf-sun/p/3631381.html