今天在网上看到一个关于pdf在线阅读系统实现思路,突发的对这个特别有兴趣,所以就在网上东搜搜,西找找的,查找了一些资料。发现实现这个功能需要这些第三方插件。
下面是实现转换成pdf的代码
try {
File inputFile = new File("D:\\1.doc");
File outputFile = new File("D:\\11.pdf");
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
} catch (ConnectException e) {
// TODO Auto-generated catch block
e.printStackTrace(); }
二。下载 SWFTools 工具包,利用Process pro = Runtime.getRuntime().exec("pdf2swf -11.pdf -o 111.swf");这样就可以转换pdf文件为swf文件了。
三。下载FlexPaper,用户显示swf文件显示代码如下
<</SPAN>script type="text/javascript" src="js/swfobject/swfobject.js"></</SPAN>script> <</SPAN>script type="text/javascript">
var swfVersionStr = "10.0.0";
var xiSwfUrlStr = "playerProductInstall.swf";
var flashvars = {
SwfFile : escape("111.swf"),
Scale : 0.6,
ZoomTransition : "easeOut",
ZoomTime : 0.5,
ZoomInterval : 0.1,
FitPageOnLoad : false,
FitWidthOnLoad : true,
PrintEnabled : true,
FullScreenAsMaxWindow : false,
ProgressiveLoading : true,
PrintToolsVisible : true,
ViewModeToolsVisible : true,
ZoomToolsVisible : true,
FullScreenVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true,
localeChain: "zh_CN"
}; var params = { }
params.quality = "high";
params.bgcolor = "#ffffff";
params.allowscriptaccess = "sameDomain";
params.allowfullscreen = "true";
var attributes = {};
attributes.id = "FlexPaperViewer";
attributes.name = "FlexPaperViewer";
swfobject.embedSWF(
"FlexPaperViewer.swf", "flashContent",
"780", "650",
swfVersionStr, xiSwfUrlStr,
flashvars, params, attributes);
swfobject.createCSS("#flashContent", "display:block;text-align:left;");
</</SPAN>script>
<</SPAN>div id="flashContent"> </</SPAN>div>
至此就可以顺利显示用户上传的doc文件或ppt文件等等。 ------------------完-------------------
在先文档浏览器,像豆丁那样的,每篇文章都会有个缩略图,我就很好奇它那个缩略图是怎么弄出来的,难道是人工直接剪切出来(这肯定是荒谬的)。所以就借助百度和谷歌。搜索到了截取图片的插件,就是PDFBOX,目前版本是1.6
1 try {
2 PDDocument doc = PDDocument.load("D:\\1.pdf");// 待获取图片的pdf文件 3
int pageCount = doc.getNumberOfPages();
4 System.out.println(pageCount); 5 6 System.out.println("开始获取图片。。。。");
7 List pages = doc.getDocumentCatalog().getAllPages();
8 for (int i = 0; i < pages.size(); i++) {
9 PDPage page = pages.get(i);
10 BufferedImage image = page.convertToImage();
11 Iterator iter = ImageIO.getImageWritersBySuffix("JPG");
12 ImageWriter writer = iter.next();
13 File outFile = new File("D:\\pdfImage\" + i + ".jpg");// 保存图片的
14 FileOutputStream out = new FileOutputStream(outFile);
15 ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
16 writer.setOutput(outImage);
17 writer.write(new IIOImage(image, null, null));
18 }
19 doc.close();
20 System.out.println("获取图片结束!");
21 } catch (IOException e) {
22 e.printStackTrace();
23 }
上传Servlet的实现代码
1 public void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException, IOException { 3
4 String id = null;
5 Affiche affiche = new Affiche();//自定义的一个简单javabean
6 boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// 判断提交的表单是不是普通表单,不是的话就进行上传
7 if (isMultipart) {// 不是普通表单的话就进行上传
8 FileItemFactory factory = new DiskFileItemFactory();
// 固定用法,创建一个FileItemFactory对象
9 ServletFileUpload upload = new ServletFileUpload(factory);
// 固定用法,创建一个ServletFileUpload对象
10 Iterator items;// 定义一个保存所有表单数据的集合
11 try {
12 items = upload.parseRequest(request).iterator();// 将表单数据全部赋值给items
13 while (items.hasNext()) {// while循环迭代items,得到所有的表单数据
14 FileItem item = items.next();
15 if (!item.isFormField()) {// 判断此表单数据是否是file类型没事的话 就进行上传
16 String name = item.getName();// 得到file文件的名字
17 String fileName = name.substring(name.lastIndexOf('\\') + 1, name.length());
18 fileName = System.currentTimeMillis() + "_" + fileName;
// 生成保存file的文件名,利用当前时间的毫秒值加上源文件的后缀名
19 String path = request.getSession().getServletContext().getRealPath("upload")
20 + File.separatorChar + fileName;// 生成保存该文件的路径名
21 System.out.println(path);
22 File uploadedFile = new File(path);
23 item.write(uploadedFile);// 保存文件
24 affiche.setAfFilepath(path);
25 } else {
// 如果不是file类型的表单,那么就进行简单的赋值操作,注意这里不能再简单的用reqeuest.getParameter("")得到页面传递的表单数据,因为全部封装到items这个集合了
26 if ("id".equals(item.getFieldName())) { //获取非上传参数
27 id = item.getString("gb2312");
28 }
29 if ("time".equals(item.getFieldName())) {//获取非上传参数
30
31 affiche.setAfEnabletime(parseDate(item.getString("gb2312")));
32 }
33 if ("title".equals(item.getFieldName())) {//获取非上传参数
34
35 affiche.setAfTitle(item.getString("gb2312"));
36 }
37 if ("content".equals(item.getFieldName())) {//获取非上传参数
38
39 affiche.setAfContent(item.getString("gb2312"));
40 }
41 }
42 }
43 } catch (Exception e) {
44 e.printStackTrace();
45 }
46 }
48 }