zoukankan      html  css  js  c++  java
  • PDF解决方案(2)--文件转PDF

    相关专题链接:

    PDF解决方案(1)--文件上传

    PDF解决方案(2)--文件转PDF

    PDF解决方案(3)--PDF转SWF

    PDF解决方案(4)--在线浏览

    前言:上一篇中讲到的文件上传,文件上传在网络上有大量的范例,因为想提供一个完整的解决方案就放上了,仅供参考;这一篇主要介绍一些常用文件转换为PDF的实现。

    1、word、excel转pdf

    通过百度了解到现在office转换为pdf主要有三种形式:Jacob、JCom、openoffice,前两种方式均依赖windows平台和office软件(其中JCom还需要Acrobat_Pro且已注册),成本很高且局限性很大,第三种方式使用开源的openoffice支持跨平台使用,目前绝大部分的web应用都会部署在linux或UNIX平台,所以第三种方式是一种最好的实现方案,下面介绍第三种方案的实现(基于windows平台,linux平台在openoffice的安装和启动略有差异,但Java调用的代码是相同的,后面在提供专门篇幅来介绍linux平台的openoffice安装和启动),前两种方式会在下面提供demo供大家参考。

    转换前需要先安装并以服务方式启动openoffice软件:

    openoffice下载地址:http://www.openoffice.org/download/index.html

    安装成功后在cmd中定位到program目录,执行命令:soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" –nofirststartwizard,然后执行:netstat -ano|findstr "8100" ,当出现如图红线所示则表示启动成功了。

    openoffice

    调用openoffice代码:

    调用openoffice进行转换需要用到openoffice提供的jar包:jodconverter、juh、jurt、ridl、slf4j-api、slf4j-jdk14、unoil、xstream,Apache的io包,在项目中引用就可以了,其中在jodconverter包的com.artofsolving.jodconverter路径下有一个document-formats.xml的文件,它定义了openoffice支持的文件转换类型,这里我们只选中其中的doc、docx、xls、xlsx四种类型,先获取openoffice连接,然后定义输入输出文件格式信息,执行转换,关闭连接即可,代码中有详细注释这里不再赘述了。

    /**
         * 调用openoffice把office转成pdf
         * @param inStream 输入流
         * @param fos 输出流
         * @param extensionname 扩展名
         */
        public static void converterOffiec2PDF(InputStream inStream,
                FileOutputStream fos, String extensionname)
        {
            OpenOfficeConnection connection = null;
            try
            {
                connection = new SocketOpenOfficeConnection(8100);//获取openoffice连接
                DocumentConverter converter = new OpenOfficeDocumentConverter(
                        connection);//创建openoffice文件转换类
                DocumentFormat inputFormat = null;
                DocumentFormat pdf = new DocumentFormat("Portable Document Format",
                        "application/pdf", "pdf");//指定目标文件格式信息
                
                //指定输出过滤器参数
                pdf.setExportFilter(DocumentFamily.DRAWING, "draw_pdf_Export");
                pdf.setExportFilter(DocumentFamily.PRESENTATION,
                        "impress_pdf_Export");
                pdf.setExportFilter(DocumentFamily.SPREADSHEET, "calc_pdf_Export");
                pdf.setExportFilter(DocumentFamily.TEXT, "writer_pdf_Export");
                
                //指定输入文件格式信息  定义信息在docment_formats.xml中
                if (Constans.FileExtName.DOC.equalsIgnoreCase(extensionname))
                {
                    inputFormat = new DocumentFormat("Microsoft Word",
                            DocumentFamily.TEXT, "application/msword", "doc");
                    inputFormat.setExportFilter(DocumentFamily.TEXT, "MS Word 97");
                }
                else if (Constans.FileExtName.DOCX.equalsIgnoreCase(extensionname))
                {
                    inputFormat = new DocumentFormat(
                            "Microsoft Word 2007 XML",
                            DocumentFamily.TEXT,
                            "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                            "docx");
                }
                else if (Constans.FileExtName.XLS.equalsIgnoreCase(extensionname))
                {
                    inputFormat = new DocumentFormat("Microsoft Excel",
                            DocumentFamily.SPREADSHEET, "application/vnd.ms-excel",
                            "xls");
                    inputFormat.setExportFilter(DocumentFamily.SPREADSHEET,
                            "MS Excel 97");
                }
                else if (Constans.FileExtName.XLSX.equalsIgnoreCase(extensionname))
                {
                    inputFormat = new DocumentFormat(
                            "Microsoft Excel 2007 XML",
                            DocumentFamily.SPREADSHEET,
                            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                            "xlsx");
                }
                
                //执行文件转换
                converter.convert(inStream, inputFormat, fos, pdf);
            } finally
            {
                if (connection != null)
                {
                    connection.disconnect();
                    connection = null;
                }
    
                closeStream(inStream, fos);
            }
        }

    2、图片转pdf

    图片转pdf采用itext的开源jar包itextpdf,itextpdf可以创建、修改pdf,这里我们利用itextpdf创建一个新的pdf然后把图片添加进去,实现图片转pdf的效果;代码步骤为:先创建文档,获取pdf写入器,打开文档,设置内容格式,写入内容到文档,关闭文档。

    /**
         * 图片转pdf
         * @param inStream
         * @param fos
         * @throws MalformedURLException
         * @throws IOException
         * @throws
         */
        public static void converterImg2Pdf(InputStream inStream,
                FileOutputStream fos) throws MalformedURLException, IOException
        {
            //创建新文档
            Document doc = new Document();
            try
            {
                //pdf写入器装载文档、输出流
                PdfWriter.getInstance(doc, fos);
                doc.open();//打开文档准备写入,文档必须打开才能写入
                
                BufferedImage bufferedImage = ImageIO.read(inStream);
                float h = bufferedImage.getHeight();
                float w = bufferedImage.getWidth();
                Image image = Image.getInstance(bufferedImage, null);
                image.setAlignment(Image.MIDDLE);//图片对齐方式,居中即可
                image.scalePercent(getPercent2(h, w));//图片压缩比
                doc.add(image);//添加图片
                doc.close();//关闭文档,文档必须正确关闭
    
            } catch (DocumentException e)
            {
                e.printStackTrace();
            }
        }

    相关文件下载

    jcom、jacob demo下载地址:http://pan.baidu.com/s/1dD297rz

  • 相关阅读:
    游千佛塔有感
    时刻坚持高标准:成大事者的十条“箴言”
    谁愿意嫁给我这样的人
    成功的秘诀之一,就是敢于提出大设想、大思考
    寒冬里的暖阳
    世界最伟大的管理原则
    把你藏在心里
    登天门有感
    办公室保持最佳状态的诀窍
    “领悟”的价值是什么?思维能力训练问答
  • 原文地址:https://www.cnblogs.com/barros/p/3870704.html
Copyright © 2011-2022 走看看