使用FlexPaper实现在线阅读swf(使用pdf2swf.exe将pdf在线转换为swf文件)

第一步:将pdf文件转为swf文件,具体步骤如下:

1,首先下载并安装SWFTools工具,下载路径:点击打开链接

2,然后使用命令 pdf2swf 123.pdf -o 123.swf

    2.1  这里注意SWFTools 工具安装路径的问题,如果安装在C盘 则直接使用命令C:pdf2swf 123.pdf -o 123.swf,但是如果安装在D盘目录下 则需要将命令切换到D盘在使用D:pdf2swf 123.pdf -o 123.swf该指令。

    切换指令cd 回车,在输入D: 回车即可     

     如果SWFTools工具安装在C盘的SWFTools文件夹下 则输入命令行 C:SWFToolspdf2swf 123.pdf -o 123.swf

    2.2 要转换的pdf文件必须和SWFTools安装路径保持一致,否则无法找到该pdf文件。

这样即可实现pdf文件转换为swf文件。

第二步:通过FlexPaperViewer将转换好的swf文件显示在页面上

1,页面添加jquery.js,flexpaper_flash.js和flexpaper_flash_debug.js的引用

2,将已经转换好的swf和FlexPaperViewer.swf放在web应用的某个文件夹下,本实例中放在ReportTemplate文件夹下

3,如下在页面中这样编码即可实现PDF文件的在线预览功能

要实现pdf在线转换swf,需要下载pdf2swf.exe,FlexPaper以及swftools,并将swftools解压到c盘,将pdf2swf.exe放到项目的bin目录下

后台代码:

/// PDF格式转为SWF
///
/// 原视频文件地址,如/a/b/c.pdf
/// 生成后的FLV文件地址,如/a/b/c.swf
/// 转换的前N页
///
public static bool PDF2SWF(string pdfPath, string swfPath, int page)
{
string exe = HttpContext.Current.Server.MapPath("~/Bin/pdf2swf.exe");
pdfPath = HttpContext.Current.Server.MapPath("../../" + pdfPath);
swfPath = HttpContext.Current.Server.MapPath("../../" + swfPath);

StringBuilder sb = new StringBuilder();
sb.Append(" "" + pdfPath + """);//input
sb.Append(" -o "" + swfPath + """);//output
//sb.Append(" -z");
sb.Append(" -s flashversion=9");//flash version
//sb.Append(" -s disablelinks");//禁止PDF里面的链接
sb.Append(" -p " + ""1" + "-" + page + """);//page range
sb.Append(" -j 100");//SWF中的图片质量
string Command = sb.ToString();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = exe;
p.StartInfo.Arguments = Command;
p.StartInfo.WorkingDirectory = HttpContext.Current.Server.MapPath("~/Bin/");
p.StartInfo.UseShellExecute = false;//不使用操作系统外壳程序 启动 线程
//p.StartInfo.RedirectStandardInput = true;
//p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;//把外部程序错误输出写到StandardError流中(这个一定要注意,pdf2swf.exe的所有输出信息,都为错误输出流,用 StandardOutput是捕获不到任何消息的...
p.StartInfo.CreateNoWindow = true;//不创建进程窗口
p.Start();//启动线程
p.BeginErrorReadLine();//开始异步读取
p.WaitForExit();//等待完成
//p.StandardError.ReadToEnd();//开始同步读取
p.Close();//关闭进程
p.Dispose();//释放资源
return true;
}
public static bool PDF2SWF(string pdfPath, string swfPath)
{
return PDF2SWF(pdfPath, swfPath, GetPageCount(HttpContext.Current.Server.MapPath("../../" + pdfPath)));
}
public static int GetPageCount(string pdfPath)
{
//try
//{
byte[] buffer = System.IO.File.ReadAllBytes(pdfPath);
int length = buffer.Length;
if (buffer == null)
return -1;
if (buffer.Length <= 0)
return -1;
string pdfText = Encoding.Default.GetString(buffer);
System.Text.RegularExpressions.Regex rx1 = new System.Text.RegularExpressions.Regex(@"/Types*/Page[^s]");
System.Text.RegularExpressions.MatchCollection matches = rx1.Matches(pdfText);
return matches.Count;
//}
//catch (Exception ex)
//{
// throw ex;
//}
}

展示页面:

<div style="position: absolute; left: 20px; top: 20px;">
<a id="viewerPlaceHolder" style=" 800px; height: 500px; display: block;"></a>
<!--设置一个隐蔽控件来得到要显示的文件的名字-->
<input type="hidden" id="file" value='<%=path %>' />
<script type="text/javascript">
var fileURL = $("#file").val();
var fp = new FlexPaperViewer(
'../../FlexPaper/FlexPaperViewer',
'viewerPlaceHolder',
{ config: {
SwfFile: escape("../../" + fileURL), //需要使用Flexpaper打开的文档
Scale: 1, //初始化缩放比例,
ZoomTransition: 'easeOut', //Flexpaper中缩放样式,默认参数值为easeOut.其他可选值包括: easenone, easeout, linear, easeoutquad
ZoomTime: 0.5, //从一个缩放比例变为另外一个缩放比例需要花费的时间,该参数值应该为0或更大
ZoomInterval: 0.2, //缩放比例之间间隔,默认值为0.1,该值为正数
FitPageOnLoad: true, //初始化得时候自适应页面,与使用工具栏上的适应页面按钮同样的效果
FitWidthOnLoad: true, //初始化的时候自适应页面宽度,与工具栏上的适应宽度按钮同样的效果
PrintEnabled: true,
FullScreenAsMaxWindow: true, //当设置为true的时候,单击全屏按钮会打开一个flexpaper最大化的新窗口而不是全屏,当由于flash播放器因为安全而禁止全屏,而使用flexpaper作为独立的flash播放器的时候设置为true是个优先选择。
ProgressiveLoading: false, //当设置为true的时候,展示文档时不会加载完整个文档,而是逐步加载,但是需要将文档转化为9以上的flash版本(使用pdf2swf的时候使用-T 9 标签)。
MinZoomSize: 0.2, //页面最小可缩小成20%
MaxZoomSize: 5, //页面最大可放大成500%
SearchMatchAll: true, //设置为true的时候,单击搜索所有符合条件的地方高亮显示
InitViewMode: 'Portrait', //设置启动模式如"Portrait" or "TwoPage".
ViewModeToolsVisible: true, //工具栏上是否显示样式选择框。
ZoomToolsVisible: true, //工具栏上是否显示缩放工具
NavToolsVisible: true, //工具栏上是否显示导航工具
CursorToolsVisible: true, //工具栏上是否显示光标工具
SearchToolsVisible: true, //工具栏上是否显示搜索
localeChain: 'zh_CN'//设置地区(语言)
}
}
);
</script>
</div>

原文地址:https://www.cnblogs.com/hcf-0320/p/4218629.html