首页 > 其他分享 >word转pdf,并加水印

word转pdf,并加水印

时间:2023-08-22 16:47:18浏览次数:39  
标签:pdfPath word String reader content 加水 poi pdf path

Word转PDF

引入jar包

 <!-- poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>${poi.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>${poi.version}</version>
        </dependency>

        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.2.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

 

代码

public static boolean word2pdf(String wordPath,String pdfPath){
        boolean success = true;
        try {
            wordPath = StringUtil.handlePath(wordPath);
            pdfPath = StringUtil.handlePath(pdfPath);

            //读取word文档
            XWPFDocument document = null;
            try (InputStream in = Files.newInputStream(Paths.get(wordPath))) {
                document = new XWPFDocument(in);
            } catch (IOException e) {
                log.error("",e);
                success = false;
            }

            //将word转成pdf
            PdfOptions options = PdfOptions.create();
            try (OutputStream outPDF = Files.newOutputStream(Paths.get(pdfPath))) {
                PdfConverter.getInstance().convert(document, outPDF, options);
            } catch (IOException e) {
                log.error("",e);
                success = false;
            }
        } catch (Exception e) {
            log.error("",e);
            success = false;
        }
        return success;
    }

PDF加水印

引入jar包

<!-- PDF水印-->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>
<!-- 中文字体 -->
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

 

代码

public static void pdfWatermark(String pdfPath,String waterImgPath,String outPath){
        PdfStamper stamper = null;
        PdfReader reader = null;

        try {
            pdfPath = StringUtil.handlePath(pdfPath);
            outPath = StringUtil.handlePath(outPath);

            InputStream is = Files.newInputStream(Paths.get(pdfPath));
            OutputStream outputStream = Files.newOutputStream(Paths.get(outPath));

            reader = new PdfReader(is);
            stamper = new PdfStamper(reader, outputStream);

            //添加图片
            PdfGState gState = new PdfGState();
            //设置透明度
            gState.setFillOpacity(0.5f);
            //image
            Image image = Image.getInstance(imageBytes(waterImgPath));
            image.scalePercent(30);//图片缩放度
            image.setAbsolutePosition(100, 350); //图片位置,这个位置测试是在中央

            int total = reader.getNumberOfPages() + 1;
            for (int i = 1; i < total; i++) {
                PdfContentByte content = stamper.getOverContent(i);
//                content.beginText();

                content.setGState(gState);
                content.addImage(image);
                content.stroke();

//                content.endText();
            }
        } catch (DocumentException| IOException e) {
            log.error("",e);
        }finally {
            if (null != stamper) {
                try {
                    stamper.close();
                } catch (Exception e) {
                    log.info("关闭失败:",  e);
                }
            }
            if (null != reader) {
                reader.close();
            }
        }
    }

 

工具类

/**
	 * 处理这种前面多了一个斜杠的路径  /E:/xyhj/project/esg/esg-ats-serve/portal_server/target/classes/echarts/barOption.js
	 * linux下是  /home/XXX/XXX路径,前面这/ 不能去掉
	 * @param path
	 * @return
	 */
	public static String handlePath(String path){
		String osName = System.getProperties().getProperty("os.name");
		if (osName.toLowerCase().startsWith("win")) {
			if(path.startsWith("/")){
				path = path.substring(1);
			}
		}
		return path;
	}

 

仅记录简单代码,供自己后续方便拷贝。

标签:pdfPath,word,String,reader,content,加水,poi,pdf,path
From: https://www.cnblogs.com/weiyanei/p/17648932.html

相关文章

  • 重磅文章:VictoriaMetrics存储引擎分析.pdf
    作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!cnblogs博客zhihuGithub公众号:一本正经的瞎扯万字长文,详细介绍VictoriaMetrics存储引擎的内部细节。首次公开。具体文章请见:https://github.com/ahfuzhang/victoria-metrics-1.72.0/blob/master/VictoriaMetric......
  • 如何复制word的图文到xhEditor中自动上传
    ​ 1.编辑器修改(可选)1.1在 ueditor/config.json 中添加代码块    /* 上传word配置 */    "wordActionName":"wordupload",/* 执行上传视频的action名称 */    "wordFieldName":"upfile",/* 提交的视频表单名称 */    "wordPathFormat":"/p......
  • canvas实现图片加水印
    前言:有时候需要在一张图片上添加文字水印,形成新的图片,便于1vs多的分享。html中:<imgsrc=""alt=""id="newImg"/><canvasid="myCanvas"class="hidden"></canvas>js中:引入jquery.qrcode.min.js后functionImg(){//图片加......
  • 搭建自己的私人密码库!Bitwarden 部署安装教程 (免费 1Password 替代品)
    常生活中我们几乎每个人都会拥有大量网站帐号,时间久远了密码很容易忘记。因此,像1Password等密码管理+同步+一键登录的工具成为了很多人的首选。然而1Password毕竟要付费,也有人会担心这类工具有隐私泄露的风险。其实,如果你有一台自己的VPS服务器,那么就能用开源的Bitwar......
  • 如何复制word的图文到wangEditor中自动上传
    ​ 这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下)<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@     page contentType="text/html;cha......
  • 如何复制word的图文到Web编辑器中自动上传
    ​ 由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直接解决这个问题考虑到自己除了工作其他时间基本上不使用windows,因此打算使用nodejs来......
  • 如何复制word的图文到在线编器中自动上传
    ​ 在之前在工作中遇到在富文本编辑器中粘贴图片不能展示的问题,于是各种网上扒拉,终于找到解决方案,在这里感谢一下知乎中众大神以及TheViper。通过知乎提供的思路找到粘贴的原理,通过TheViper找到粘贴图片的方法。其原理为一下步骤:监听粘贴事件;【用于插入图片】获取光标位置;【......
  • 如何复制word的图文到富文本框编辑器中自动上传
    ​  自动导入Word图片,或者粘贴Word内容时自动上传所有的图片,并且最终保留Word样式,这应该是Web编辑器里面最基本的一个需求功能了。一般情况下我们将Word内容粘贴到Web编辑器(富文本编辑器)中时,编辑器都无法自动上传图片。需要用户手动一张张上传Word图片。如果只有一张图片还能......
  • 在 Amazon Linux 2023 上托管 WordPress 博客
    以下步骤将帮助您在AmazonLinux2023实例上安装、配置和保护WordPress博客。本教程是很好的AmazonEC2入门教程,因为您可以完全控制托管您WordPress博客的Web服务器,这对传统的托管服务来说并不是一个典型的方案。您负责更新软件包并为您的服务器维护安全补丁。对于不需......
  • Docker 搭建 LNMP 架构的 Wordpress网站
    目录一、项目环境二、服务器环境三、任务需求四、获取Linux系统基础镜像五、Nginx1.建立工作目录2.编写Dockerfile脚本3.编辑nginx的主配置文件4.生成镜像5.创建自定义网络6.启动镜像容器7.验证nginx六、MySQL1.建立工作目录2.编写Dockerfile3.编辑mysql主配......