栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java word转pdf (添加水印)通过poi等组件实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java word转pdf (添加水印)通过poi等组件实现

 所需jar包地址

        
        
             e-iceblue 
            spire.doc.free
            3.9.0
        

  
        
            org.apache.poi
            poi
            4.1.2
        
        
            org.apache.poi
            poi-ooxml
            4.1.2
        
        
            org.apache.poi
            poi-ooxml-schemas
            4.1.2
        
        
            org.apache.poi
            poi-scratchpad
            4.1.2
        

        
        
            fr.opensagres.xdocreport
            org.apache.poi.xwpf.converter.pdf
            1.0.6
        

        
        
            fr.opensagres.xdocreport
            xdocreport
            2.0.2
        

        
        
            com.itextpdf
            itextpdf
            5.5.13.1
        
        
        
            com.itextpdf
            itext-asian
            5.2.0
        

        
        
            com.deepoove
            poi-tl
            1.9.1
        

        






    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                
                    true
                
            
        
    

执行代码

import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.*;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import java.io.*;

public class TestWordToPDF {

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        String docxPath =  "C:\Users\admin-xu\Desktop\11\test.docx";
        String pdfPath =  "C:\Users\admin-xu\Desktop\11\test2.pdf";
        File file = new File( docxPath);
        InputStream inputStream = new FileInputStream(file);
        ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
        XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
        fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(xwpfDocument, pdfBaos, PdfOptions.create());
        inputStream.close();
        xwpfDocument.close();
        byte[] pdfArray = null;
        pdfArray = pdfBaos.toByteArray();
        pdfBaos.close();
        InputStream pdfInputStream = new ByteArrayInputStream( pdfArray ) ;
        FileOutputStream fileOutputStream = new FileOutputStream(pdfPath);
        addWaterMark(pdfInputStream , fileOutputStream);
        long end = System.currentTimeMillis();
        System.out.println("执行时间: " + ((end - start) / 1000) + "秒");
    }

    //给PDF文件添加水印
    public static void addWaterMark(InputStream pdfInputStream ,   FileOutputStream fileOutputStream) {
        try {
            // 原PDF文件
            PdfReader reader = new PdfReader(pdfInputStream);
            // 输出的PDF文件内容
            PdfStamper pdfStamper = new PdfStamper(reader,   fileOutputStream );
            // 字体 来源于 itext-asian JAR包
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", true);
            PdfGState pdfGState = new PdfGState();
            // 设置透明度
            pdfGState.setFillOpacity(0.2f);
            pdfGState.setStrokeOpacity(0.4f);
            int totalPage = reader.getNumberOfPages() + 1;
            for (int i = 1; i < totalPage; i++) {
                // 内容上层
//			PdfContentByte content = stamper.getOverContent(i);
                // 内容下层
                PdfContentByte pdfContentByte = pdfStamper.getUnderContent(i);
                pdfContentByte.beginText();
                // 字体添加透明度
                pdfContentByte.setGState(pdfGState);
                // 添加字体大小等
                pdfContentByte.setFontAndSize(baseFont, 20);
                // 添加范围
                pdfContentByte.setTextMatrix(70, 200);
                //  具体位置 内容   多少行  多少列  旋转多少度 共360度
                for (int a = 0; a < 3; a++) { // 一页几排
                    for (int j = 0; j < 3; j++) { // 一排几个
                        int x =  70 + 170 * j ;   // 横向  宽
                        int y =  170 + 200 *  a ;  // 纵向  高
                        pdfContentByte.showTextAligned(Element.ALIGN_BOTTOM, "机密文件"  , x, y, 45); // 45 是水印旋转的角度
                    }
                }
                pdfContentByte.endText();
            }
            // 关闭
            pdfStamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

转换前的word

转换后的 pdf

 

执行结果和时间,项目中实际测试大概是5-6秒左右(和服务器性能相关)

另外一种方法

java word转换pdf(先自定义添加水印 后转换pdf)_My--Style的博客-CSDN博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/949278.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号