博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FlexPaper+SWFTool+操作类=在线预览PDF
阅读量:6858 次
发布时间:2019-06-26

本文共 4595 字,大约阅读时间需要 15 分钟。

引言

由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swftool配合使用,在程序运行时将pdf文件转换为swf格式的文件。

如果flexpaper不满足你的要求,也可以对其进行二次开发,这里推荐两篇文章,希望对您有所帮助:

如何使用flexpaper

测试demo项目结构如图

使用的页面代码:

1  2 
3 4 5 6
7 22
23 24 25
26 68 69 70 71
72
73

74 To view this page ensure that Adobe Flash Player version75 10.0.0 or greater is installed.76

77
82
83
84 Can't see the document? Running FlexPaper from your local directory? Make sure you have added FlexPaper as trusted. You can do that at
Adobe's website.85
86
87 88

效果图

上面的工具栏:打印,全屏等功能可配置。

swftool工具

 操作类(本类来自常用类库,从网上下载的,一搜一大把)

1 using System.Web; 2 using System.Text; 3  4 public static class PSD2swfHelper 5 { 6     ///  7     /// 转换所有的页,图片质量80% 8     ///  9     /// PDF文件地址10     /// 生成后的SWF文件地址11     public static bool PDF2SWF(string pdfPath, string swfPath)12     {13         return PDF2SWF(pdfPath, swfPath, 1, GetPageCount(HttpContext.Current.Server.MapPath(pdfPath)), 80);14     }15 16     /// 17     /// 转换前N页,图片质量80%18     /// 19     /// PDF文件地址20     /// 生成后的SWF文件地址21     /// 页数22     public static bool PDF2SWF(string pdfPath, string swfPath, int page)23     {24         return PDF2SWF(pdfPath, swfPath, 1, page, 80);25     }26 27     /// 28     /// PDF格式转为SWF29     /// 30     /// PDF文件地址31     /// 生成后的SWF文件地址32     /// 转换开始页33     /// 转换结束页34     private static bool PDF2SWF(string pdfPath, string swfPath, int beginpage, int endpage, int photoQuality)35     {36         //swftool,首先先安装,然后将安装目录下的东西拷贝到tools目录下37         string exe = HttpContext.Current.Server.MapPath("~/Bin/tools/pdf2swf.exe");38         pdfPath = HttpContext.Current.Server.MapPath(pdfPath);39         swfPath = HttpContext.Current.Server.MapPath(swfPath);40         if (!System.IO.File.Exists(exe) || !System.IO.File.Exists(pdfPath) || System.IO.File.Exists(swfPath))41         {42             return false;43         }44         StringBuilder sb = new StringBuilder();45         sb.Append(" \"" + pdfPath + "\"");46         sb.Append(" -o \"" + swfPath + "\"");47         sb.Append(" -s flashversion=9");48         if (endpage > GetPageCount(pdfPath)) endpage = GetPageCount(pdfPath);49         sb.Append(" -p " + "\"" + beginpage + "" + "-" + endpage + "\"");50         sb.Append(" -j " + photoQuality);51         string Command = sb.ToString();52         System.Diagnostics.Process p = new System.Diagnostics.Process();53         p.StartInfo.FileName = exe;54         p.StartInfo.Arguments = Command;55         p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");56         p.StartInfo.UseShellExecute = false;57         p.StartInfo.RedirectStandardError = true;58         p.StartInfo.CreateNoWindow = false;59         p.Start();60         p.BeginErrorReadLine();61         p.WaitForExit();62         p.Close();63         p.Dispose();64         return true;65     }66 67     /// 68     /// 返回页数69     /// 70     /// PDF文件地址71     private static int GetPageCount(string pdfPath)72     {73         byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);74         int length = buffer.Length;75         if (buffer == null)76             return -1;77         if (buffer.Length <= 0)78             return -1;79         string pdfText = Encoding.Default.GetString(buffer);80         System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Type\s*/Page[^s]");81         System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);82         return matches.Count;83     }84 }

然后安装swftool工具,将安装后的目录中的文件拷贝到tools目录下,如图

 

test.asp.cs代码

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.UI; 6 using System.Web.UI.WebControls; 7  8 namespace Wolfy.FlexPaperDemo 9 {10     public partial class Test : System.Web.UI.Page11     {12         protected void Page_Load(object sender, EventArgs e)13         {14             //这里需要虚拟路径15             PSD2swfHelper.PDF2SWF("PDFFile/王牌2_C#_控件查询手册.pdf", "SWFFile/王牌2_C#_控件查询手册.swf");16         }17     }18 }

结果

如何禁用右键中的打印,复制功能

如果pdf保密性强,不让别人复制,打印等该如何?在上面推荐的两篇文章中,他们对其进行了二次开发,禁用了这个功能。非常感谢,那么之后只需将FlexPaperViewer.swf替换就可以了。

原图

替换后的

 

说保密只是相对的,在互联网上,只要能看,别人想盗取还是很容易的事,大不了,一张一张的截图。

总结

互联网,没有绝对安全的,想安全就别放在互联网上显摆,只要想要,总会有办法的。有时候客户的需求真他妈的让人蛋疼。

demo下载:链接:链接: 密码:gupg

swftools-2013-04-09-1007下载:链接:http://pan.baidu.com/s/1c0CvBDA 密码:v38r

转载地址:http://oityl.baihongyu.com/

你可能感兴趣的文章
常用排序算法
查看>>
程序员保持快乐活跃的6个好习惯(转)
查看>>
找工作的一些感悟——前端小菜的成长
查看>>
jSON Call can throw but it is not marked with try
查看>>
基于bootstrap的jQuery多级列表树插件 treeview
查看>>
node06
查看>>
笔试题[转]
查看>>
图片轮换
查看>>
PHP数据结构练习笔记--栈
查看>>
JSON对象配合jquery.tmpl.min.js插件,手动攒出一个table
查看>>
编译安装QEMU 及卸载
查看>>
关于php-fpm与nginx进程重载的坑
查看>>
P2S、P2P、P2SP之对比
查看>>
笔记01 登录、常用配置参数、Action访问Servlet API 和设置Action中对象的值、命名空间和乱码处理、Action中包含多个方法如何调用...
查看>>
替代变量
查看>>
73. Spring Boot注解(annotation)列表【从零开始学Spring Boot】
查看>>
UNIX环境高级编程——pthread_create的问题
查看>>
接口继承中一个常见问题的思考
查看>>
C#获取软件图标
查看>>
提高代码质量的三要素
查看>>