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。

print2flash文档在线预览应用(java,.net)

  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表示安装成功。

print2flash文档在线预览应用(java,.net)

  5)开启print2flash4服务。安装成功后,在windows服务中可以找到print2flash服务。启动该服务。

print2flash文档在线预览应用(java,.net)

三、java操作API


  开始前我们需要将print2flash提供的SDK中的P2FConst.java引入:

 public class P2FConst
{
// 枚举常量
public static final int MSEXCEL = 0x00000001;
public static final int MSWORD = 0x00000002;
public static final int MSPOWERPOINT = 0x00000004;
public static final int ACROBAT = 0x00000008; public static final int JPEG = 1;
public static final int PNG = 2; public static final int STRETCH = 1;
public static final int TILE = 2; public static final int INTLOGO = 0x00000001;
public static final int INTDRAG = 0x00000002;
public static final int INTSELTEXT = 0x00000004;
public static final int INTZOOMSLIDER = 0x00000008;
public static final int INTZOOMBOX = 0x00000010;
public static final int INTFITWIDTH = 0x00000020;
public static final int INTFITPAGE = 0x00000040;
public static final int INTPREVPAGE = 0x00000080;
public static final int INTGOTOPAGE = 0x00000100;
public static final int INTNEXTPAGE = 0x00000200;
public static final int INTSEARCHBOX = 0x00000400;
public static final int INTSEARCHBUT = 0x00000800;
public static final int INTROTATE = 0x00001000;
public static final int INTPRINT = 0x00002000;
public static final int INTNEWWIND = 0x00004000;
public static final int INTHELP = 0x00008000;
public static final int INTBACKBUTTON = 0x00030000;
public static final int INTBACKBUTTONAUTO = 0x00010000;
public static final int INTFORWARDBUTTON = 0x000C0000;
public static final int INTFORWARDBUTTONAUTO = 0x00040000;
public static final int INTFULLSCREEN = 0x00300000;
public static final int INTFULLSCREENAUTO = 0x00100000; // METADATAPORMAT enumeration constants
public static final int XML = 1;
public static final int TEXT = 2; // OUTPUTFORMAT enumeration constants
public static final int SINGLEFILE = 1;
public static final int SINGLEFILEPERPAGE = 2;
public static final int EXTVIEWER = 4; // PAPER_ORIENTATION enumeration constants
public static final int ORIENT_PORTRAIT = 1;
public static final int ORIENT_LANDSCAPE = 2; // PROTECTION_OPTION enumeration constants
public static final int PROTDISPRINT = 0x00000001;
public static final int PROTDISTEXTCOPY = 0x00000002;
public static final int PROTENAPI = 0x00000004; // ThreeStateFlag enumeration constants
public static final int TSF_NO = 0;
public static final int TSF_YES = 1;
public static final int TSF_AUTO = 2; // TOOLBARIMAGE enumeration constants
public static final int IMGLOGO = 1;
public static final int IMGDRAG = 2;
public static final int IMGSELTEXT = 3;
public static final int IMGZOOMRULER = 4;
public static final int IMGZOOMFOCUSNADLE = 5;
public static final int IMGZOOMNADLE = 6;
public static final int IMGFITWIDTH = 7;
public static final int IMGFITPAGE = 8;
public static final int IMGPREVPAGE = 9;
public static final int IMGNEXTPAGE = 10;
public static final int IMGSEARCHBUT = 11;
public static final int IMGROTATE = 12;
public static final int IMGPRINT = 13;
public static final int IMGNEWWIND = 14;
public static final int IMGHELP = 15;
public static final int IMGMORE = 16;
public static final int IMGTOOLBARBGR = 17;
public static final int IMGBACK = 18;
public static final int IMGFORWARD = 19;
public static final int IMGFULLSCREEN = 20;
public static final int IMGEXITFULLSCREEN = 21; // TEMPLATETYPE enumeration constants
public static final int TEMPLATE_CUSTOM = 1;
public static final int TEMPLATE_ACTIONSCRIPT2 = 2;
public static final int TEMPLATE_ACTIONSCRIPT3 = 3; // WATERMARKANCHOR enumeration constants
public static final int CENTER = 0;
public static final int LEFTCENTER = 1;
public static final int RIGHTCENTER = 2;
public static final int TOPCENTER = 16;
public static final int BOTTOMCENTER = 32;
public static final int LEFTTOP = 17;
public static final int RIGHTTOP = 18;
public static final int LEFTBOTTOM = 33;
public static final int RIGHTBOTTOM = 34; // COMPRESSION_METHOD enumeration constants
public static final int COMPRESSION_METHOD_ZLIB = 0;
public static final int COMPRESSION_METHOD_LZMA = 1; // DOCUMENT_TYPE Enumeration constants
public static final int FLASH = 1;
public static final int HTML5 = 2; // BROWSER_TYPE Enumeration public static final intants
public static final int INTERNET_EXPLORER = 1;
public static final int FIREFOX = 2;
public static final int CHROME = 4;
public static final int OPERA = 8;
public static final int SAFARI = 16; // POWERPOINT_PRINTOUTPUT Enumeration constants
public static final int POWERPOINT_PRINTOUTPUT_AUTO = 0;
public static final int POWERPOINT_PRINTOUTPUT_Slides = 1;
public static final int POWERPOINT_PRINTOUTPUT_TwoSlideHandouts = 2;
public static final int POWERPOINT_PRINTOUTPUT_ThreeSlideHandouts = 3;
public static final int POWERPOINT_PRINTOUTPUT_SixSlideHandouts = 4;
public static final int POWERPOINT_PRINTOUTPUT_NotesPages = 5;
public static final int POWERPOINT_PRINTOUTPUT_Outline = 6;
public static final int POWERPOINT_PRINTOUTPUT_BuildSlides = 7;
public static final int POWERPOINT_PRINTOUTPUT_FourSlideHandouts = 8;
public static final int POWERPOINT_PRINTOUTPUT_NineSlideHandouts = 9;
public static final int POWERPOINT_PRINTOUTPUT_OneSlideHandouts = 10;
}

  开始转换Test.java类:

 package print2flash;

 import java.io.IOException;

 import com.jacob.activeX.*;
import com.jacob.com.*; public class Test { public static void main(String[] args) throws IOException {
try {
ComThread.InitSTA();//com组件管理,用来初始化com线程,释放线程
ActiveXComponent p2f = new ActiveXComponent("Print2Flash4.Server");//创建print2flash的一个应用,调用print2flash服务
//设置属性值,可根据自己需求修改
ActiveXComponent defProfile = new ActiveXComponent(p2f.getProperty("DefaultProfile").toDispatch());
defProfile.setProperty("InterfaceOptions", P2FConst.INTLOGO | P2FConst.INTZOOMSLIDER | P2FConst.INTPREVPAGE
| P2FConst.INTGOTOPAGE | P2FConst.INTNEXTPAGE);
defProfile.setProperty("ProtectionOptions", P2FConst.PROTDISPRINT | P2FConst.PROTENAPI);
defProfile.setProperty("DocumentType", P2FConst.FLASH | P2FConst.HTML5);
p2f.invoke("ConvertFile", "E:/print2flashTest/test.doc");//方法调用
System.out.println("转换成功!");
} catch (Exception e) {
System.out.println("转换异常: " + e.toString());
} finally {
ComThread.Release();
}
}
}

  注意:这里涉及到java应用调用windows的COM组件,需要用到jacob,没有的同学,可以用LZ提供的链接地址下载(文章最后会一起附上)。

  1)将jacob.jar build path。

  2)将jacob-1.16-M1-x64.dll(根据自己系统情况选择)复制到jre/bin目录(如果没有该dll,会抛出找不到ComThread类异常)

  接下来LZ准备一个test.doc,来进行测试:

  print2flash文档在线预览应用(java,.net)

  test.docx.swf即为生成的flash文件,可嵌入到你需要的网页中。

四、.NET操作API


  .NET的操作相对来说就简单得多了,LZ这里也是只做一个简单的win32控制台演示:

  1)首先新建win32控制台应用程序,增加程序集Interop.Print2Flash4.dll。

  2)在Proogram.cs中写具体业务逻辑:  

 using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text; namespace Test
{
class Program
{
static void Main(string[] args)
{
string fs_filename = System.AppDomain.CurrentDomain.BaseDirectory()+"/print2flashTest/test.docx";
string fs_convertedfilename = System.AppDomain.CurrentDomain.BaseDirectory()+"/print2flashTest/swf/test.swf";
Print2Flash4.Server2 p2fServer = new Print2Flash4.Server2();//创建print2flash对象
//设置参数值,可以参考官网SDK文档
p2fServer.ConvertFile(fs_filename, fs_convertedfilename, null, null, null);
}
}
}

  运行结果与java一样。官网SDK还提供了ASP、PHP等脚本语言的操作API,有情趣的同学可以自己尝试一下。

  print2flash文档在线预览应用(java,.net)

五、总结


  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

  

上一篇:jquery单击事件的写法


下一篇:磁盘是随机存储设备,但不是随机存储器(RAM)。为什么?