print2flash文档在线预览应用(java,.net)
一、背景
前段时间,LZ的boss突然给了出了这样一个需求:将原项目中的所有文章关联的附件TXT、PDF、office相关文件全部以flash的形式在网页上进行展示,便于预览。看似简单的需求,整个研发小组都懵逼的。LZ也没办法只好Google一把,找了好几套解决方案,基本上是调第三方软件的API,像什么flashpaper、print2flash等等。LZ先试了flashpaper,反正是各种问题,不支持64位系统什么的;print2flash在版本4以前也是很多问题,dll调用失败,dll缺失。最终,LZ在官网下载了最新的版本(商业软件,收费的),用起来还是相对来说比较稳定,至今没出现问题。提供的API还是比较丰富的,本篇LZ主要介绍java和.NET的操作。
二、print2flash安装
这里LZ为了简单实验主要介绍破解版的安装(费了好大的力才找到的),如果商用,还是给钱找别人买吧。
1)下载print2flash4.如果想使用服务器模式,大概需要600刀,也不是很贵。官网地址http://print2flash.com/
2)运行print2flashsetup.exe一直下一步就OK。
3)下载补丁文件。共3个文件p2fServer.exe,print2f4.exe,Print2FlashPrinter4.dll;如果之前已设置了windows服务方式启动,则先需要停止Print2Flash4服务;拷贝p2fServer.exe、print2f4.exe到安装目标文件夹替换原文件,拷贝Print2FlashPrinter4.dll到以下文件夹替换原始文件:C:\Windows\System32\spool\drivers\x64\以及C:\Windows\System32\spool\drivers\x64\3\
4)注册print2flash4服务。运行print2flash,options->print2flash service configuration,输入Windows密码,没有的话就不用输,勾选Grant access and launch pemmissions to Everyone给EveryOne用户授予权限。点击OK,如果提示successful,Current status显示Installed表示安装成功。
5)开启print2flash4服务。安装成功后,在windows服务中可以找到print2flash服务。启动该服务。
三、java操作API
开始前我们需要将print2flash提供的SDK中的P2FConst.java引入:
1 public class P2FConst 2 { 3 // 枚举常量 4 public static final int MSEXCEL = 0x00000001; 5 public static final int MSWORD = 0x00000002; 6 public static final int MSPOWERPOINT = 0x00000004; 7 public static final int ACROBAT = 0x00000008; 8 9 10 public static final int JPEG = 1; 11 public static final int PNG = 2; 12 13 14 public static final int STRETCH = 1; 15 public static final int TILE = 2; 16 17 18 public static final int INTLOGO = 0x00000001; 19 public static final int INTDRAG = 0x00000002; 20 public static final int INTSELTEXT = 0x00000004; 21 public static final int INTZOOMSLIDER = 0x00000008; 22 public static final int INTZOOMBOX = 0x00000010; 23 public static final int INTFITWIDTH = 0x00000020; 24 public static final int INTFITPAGE = 0x00000040; 25 public static final int INTPREVPAGE = 0x00000080; 26 public static final int INTGOTOPAGE = 0x00000100; 27 public static final int INTNEXTPAGE = 0x00000200; 28 public static final int INTSEARCHBOX = 0x00000400; 29 public static final int INTSEARCHBUT = 0x00000800; 30 public static final int INTROTATE = 0x00001000; 31 public static final int INTPRINT = 0x00002000; 32 public static final int INTNEWWIND = 0x00004000; 33 public static final int INTHELP = 0x00008000; 34 public static final int INTBACKBUTTON = 0x00030000; 35 public static final int INTBACKBUTTONAUTO = 0x00010000; 36 public static final int INTFORWARDBUTTON = 0x000C0000; 37 public static final int INTFORWARDBUTTONAUTO = 0x00040000; 38 public static final int INTFULLSCREEN = 0x00300000; 39 public static final int INTFULLSCREENAUTO = 0x00100000; 40 41 42 // METADATAPORMAT enumeration constants 43 public static final int XML = 1; 44 public static final int TEXT = 2; 45 46 // OUTPUTFORMAT enumeration constants 47 public static final int SINGLEFILE = 1; 48 public static final int SINGLEFILEPERPAGE = 2; 49 public static final int EXTVIEWER = 4; 50 51 // PAPER_ORIENTATION enumeration constants 52 public static final int ORIENT_PORTRAIT = 1; 53 public static final int ORIENT_LANDSCAPE = 2; 54 55 // PROTECTION_OPTION enumeration constants 56 public static final int PROTDISPRINT = 0x00000001; 57 public static final int PROTDISTEXTCOPY = 0x00000002; 58 public static final int PROTENAPI = 0x00000004; 59 60 // ThreeStateFlag enumeration constants 61 public static final int TSF_NO = 0; 62 public static final int TSF_YES = 1; 63 public static final int TSF_AUTO = 2; 64 65 // TOOLBARIMAGE enumeration constants 66 public static final int IMGLOGO = 1; 67 public static final int IMGDRAG = 2; 68 public static final int IMGSELTEXT = 3; 69 public static final int IMGZOOMRULER = 4; 70 public static final int IMGZOOMFOCUSNADLE = 5; 71 public static final int IMGZOOMNADLE = 6; 72 public static final int IMGFITWIDTH = 7; 73 public static final int IMGFITPAGE = 8; 74 public static final int IMGPREVPAGE = 9; 75 public static final int IMGNEXTPAGE = 10; 76 public static final int IMGSEARCHBUT = 11; 77 public static final int IMGROTATE = 12; 78 public static final int IMGPRINT = 13; 79 public static final int IMGNEWWIND = 14; 80 public static final int IMGHELP = 15; 81 public static final int IMGMORE = 16; 82 public static final int IMGTOOLBARBGR = 17; 83 public static final int IMGBACK = 18; 84 public static final int IMGFORWARD = 19; 85 public static final int IMGFULLSCREEN = 20; 86 public static final int IMGEXITFULLSCREEN = 21; 87 88 // TEMPLATETYPE enumeration constants 89 public static final int TEMPLATE_CUSTOM = 1; 90 public static final int TEMPLATE_ACTIONSCRIPT2 = 2; 91 public static final int TEMPLATE_ACTIONSCRIPT3 = 3; 92 93 // WATERMARKANCHOR enumeration constants 94 public static final int CENTER = 0; 95 public static final int LEFTCENTER = 1; 96 public static final int RIGHTCENTER = 2; 97 public static final int TOPCENTER = 16; 98 public static final int BOTTOMCENTER = 32; 99 public static final int LEFTTOP = 17; 100 public static final int RIGHTTOP = 18; 101 public static final int LEFTBOTTOM = 33; 102 public static final int RIGHTBOTTOM = 34; 103 104 // COMPRESSION_METHOD enumeration constants 105 public static final int COMPRESSION_METHOD_ZLIB = 0; 106 public static final int COMPRESSION_METHOD_LZMA = 1; 107 108 // DOCUMENT_TYPE Enumeration constants 109 public static final int FLASH = 1; 110 public static final int HTML5 = 2; 111 112 // BROWSER_TYPE Enumeration public static final intants 113 public static final int INTERNET_EXPLORER = 1; 114 public static final int FIREFOX = 2; 115 public static final int CHROME = 4; 116 public static final int OPERA = 8; 117 public static final int SAFARI = 16; 118 119 // POWERPOINT_PRINTOUTPUT Enumeration constants 120 public static final int POWERPOINT_PRINTOUTPUT_AUTO = 0; 121 public static final int POWERPOINT_PRINTOUTPUT_Slides = 1; 122 public static final int POWERPOINT_PRINTOUTPUT_TwoSlideHandouts = 2; 123 public static final int POWERPOINT_PRINTOUTPUT_ThreeSlideHandouts = 3; 124 public static final int POWERPOINT_PRINTOUTPUT_SixSlideHandouts = 4; 125 public static final int POWERPOINT_PRINTOUTPUT_NotesPages = 5; 126 public static final int POWERPOINT_PRINTOUTPUT_Outline = 6; 127 public static final int POWERPOINT_PRINTOUTPUT_BuildSlides = 7; 128 public static final int POWERPOINT_PRINTOUTPUT_FourSlideHandouts = 8; 129 public static final int POWERPOINT_PRINTOUTPUT_NineSlideHandouts = 9; 130 public static final int POWERPOINT_PRINTOUTPUT_OneSlideHandouts = 10; 131 }
开始转换Test.java类:
1 package print2flash; 2 3 import java.io.IOException; 4 5 import com.jacob.activeX.*; 6 import com.jacob.com.*; 7 8 public class Test { 9 10 public static void main(String[] args) throws IOException { 11 try { 12 ComThread.InitSTA();//com组件管理,用来初始化com线程,释放线程 13 ActiveXComponent p2f = new ActiveXComponent("Print2Flash4.Server");//创建print2flash的一个应用,调用print2flash服务 14 //设置属性值,可根据自己需求修改 15 ActiveXComponent defProfile = new ActiveXComponent(p2f.getProperty("DefaultProfile").toDispatch()); 16 defProfile.setProperty("InterfaceOptions", P2FConst.INTLOGO | P2FConst.INTZOOMSLIDER | P2FConst.INTPREVPAGE 17 | P2FConst.INTGOTOPAGE | P2FConst.INTNEXTPAGE); 18 defProfile.setProperty("ProtectionOptions", P2FConst.PROTDISPRINT | P2FConst.PROTENAPI); 19 defProfile.setProperty("DocumentType", P2FConst.FLASH | P2FConst.HTML5); 20 p2f.invoke("ConvertFile", "E:/print2flashTest/test.doc");//方法调用 21 System.out.println("转换成功!"); 22 } catch (Exception e) { 23 System.out.println("转换异常: " + e.toString()); 24 } finally { 25 ComThread.Release(); 26 } 27 } 28 }
注意:这里涉及到java应用调用windows的COM组件,需要用到jacob,没有的同学,可以用LZ提供的链接地址下载(文章最后会一起附上)。
1)将jacob.jar build path。
2)将jacob-1.16-M1-x64.dll(根据自己系统情况选择)复制到jre/bin目录(如果没有该dll,会抛出找不到ComThread类异常)
接下来LZ准备一个test.doc,来进行测试:
test.docx.swf即为生成的flash文件,可嵌入到你需要的网页中。
四、.NET操作API
.NET的操作相对来说就简单得多了,LZ这里也是只做一个简单的win32控制台演示:
1)首先新建win32控制台应用程序,增加程序集Interop.Print2Flash4.dll。
2)在Proogram.cs中写具体业务逻辑:
1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Linq; 5 using System.Text; 6 7 namespace Test 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 string fs_filename = System.AppDomain.CurrentDomain.BaseDirectory()+"/print2flashTest/test.docx"; 14 string fs_convertedfilename = System.AppDomain.CurrentDomain.BaseDirectory()+"/print2flashTest/swf/test.swf"; 15 Print2Flash4.Server2 p2fServer = new Print2Flash4.Server2();//创建print2flash对象 16 //设置参数值,可以参考官网SDK文档 17 p2fServer.ConvertFile(fs_filename, fs_convertedfilename, null, null, null); 18 } 19 } 20 }
运行结果与java一样。官网SDK还提供了ASP、PHP等脚本语言的操作API,有情趣的同学可以自己尝试一下。
五、总结
print2flash这款软件还是不错的,转换后的效果比较清晰。运用范围差不多就是内容管理系统、文章管理等附件的预览,当然也可以用来控制游客用户不允许文字复制等等。最后附上本文LZ所用到的源代码,补丁,附件的下载地址:
源代码:https://github.com/LJunChina/JavaResource/tree/master/print2flash
print2flash4补丁:http://download.csdn.net/detail/qq503665965/9860438
jacob:http://download.csdn.net/detail/qq503665965/9860441
SDK:http://download.csdn.net/detail/qq503665965/9860435