zoukankan      html  css  js  c++  java
  • java word/doc/docx文档转PDF 加水印

    本文实例讲述了java实现word文档转pdf并添加水印的方法。分享给大家供大家参考,具体如下:

    前段时间,项目需要将上传的Word文档在浏览器浏览,思来想去,把word文档转成pdf就好了,于是乎研究了一下。

    将word文档转化为PDF是项目中常见的需求之一,目前主流的方法可以分为两大类,一类是利用各种Office应用进行转换,譬如Microsoft Office、WPS以及LiberOffice,另一种是利用各种语言提供的对于Office文档读取的接口(譬如Apache POI,jacob,docx4j,openoffice),这些要么收费,要么要装插件,要么转换之后样式走形,乱码等等。

    我使用Aspose.Words for Java 可以导出复杂WORD PDF HTML 多种数据格式

    官方下载地址:http://www.aspose.com/java/word-component.aspx

    我所用的版本是aspose-words-14.9.0-jdk16。分享给大家:

    链接:https://pan.baidu.com/s/1xt7P9YFjoHmPCzQHJV65Xg&shfl=sharepset
    提取码:1zdt 

    废话不多说,直接上代码:

    /**
         * 
         * @param originalFile doc文件
         * @param toFilePath 文件夹路径
         * @param fileName 文件名
         * @param type 文件类型
         * @return
         * @throws Exception
         */
        public String file2pdf(File originalFile, String toFilePath,String fileName,String type,String contractBillcode) throws Exception {
            String htmFileName;
            //获取转换成PDF之后文件名
            if(".doc".equals(type)){
                htmFileName = fileName+".pdf";
            }else if(".docx".equals(type)){
                htmFileName = fileName+".pdf";
            }else{
                return null;
            }
            //通过转换之后的PDF文件名,创建PDF文件
            File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
            //获取文件输出流
            FileOutputStream os = new FileOutputStream(htmlOutputFile);
            //获取Doc文档对象模型
            Document doc = new Document(toFilePath+ File.separatorChar + fileName+type);
            //为doc文档添加水印
            insertWatermarkText(doc, "Smile、斌");
            //将doc文旦转换成PDF文件并输出到之前创建好的pdf文件中
            doc.save(os, SaveFormat.PDF);
            //关闭输出流
            if(os!=null){
                os.close();
            }
            return htmFileName;
        }

    以上就将一个word文档转换成了PDF文件,接下来就是添加水印部分了

    废话不多说,上代码

    /**
         * 为word文档添加水印
         * @param doc word文档模型
         * @param watermarkText 需要添加的水印字段
         * @throws Exception
         */
        private static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
            Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
            //水印内容
            watermark.getTextPath().setText(watermarkText);
            //水印字体
            watermark.getTextPath().setFontFamily("宋体");
            //水印宽度
            watermark.setWidth(500);
            //水印高度
            watermark.setHeight(100);
            //旋转水印
            watermark.setRotation(-40);
            //水印颜色
            watermark.getFill().setColor(Color.lightGray); 
            watermark.setStrokeColor(Color.lightGray); 
            watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
            watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
            watermark.setWrapType(WrapType.NONE);
            watermark.setVerticalAlignment(VerticalAlignment.CENTER);
            watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
            Paragraph watermarkPara = new Paragraph(doc);
            watermarkPara.appendChild(watermark);
            for (Section sect : doc.getSections())
            {
              insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
              insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
              insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
            }
            System.out.println("Watermark Set");
        }
    /**
         * 在页眉中插入水印
         * @param watermarkPara
         * @param sect
         * @param headerType
         * @throws Exception
         */
        private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception{
            HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
            if (header == null)
            {
              header = new HeaderFooter(sect.getDocument(), headerType);
              sect.getHeadersFooters().add(header);
            }
            header.appendChild(watermarkPara.deepClone(true));
        }

    效果图如下,我添加的水印为我的博客昵称:Smile、斌

    希望本文所述对大家java程序设计有所帮助。