首页 > 基础资料 博客日记

Java pdfbox 给 PDF 添加文字和图片水印 并旋转45度

2024-07-07 04:00:07基础资料围观698

本篇文章分享Java pdfbox 给 PDF 添加文字和图片水印 并旋转45度,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识

POM

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.27</version>
</dependency>

代码:

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.util.Matrix;

import java.awt.*;
import java.io.File;

public class Test1 {


    public static void main(String[] args) {

        // 原始文件路径
        String sourcePath = "D:\\poi\\11.pdf";
        // 去c://windows/font下赋值去
        String fontFilePath = "D:\\poi\\simhei.ttf";
        String waterMarkText = "水印文字";
        // log图片的位置
        String logoFilePath = "D:\\poi\\logo.jpeg";
        // 输出的PDF文件路径
        String pdfFile = "D:\\poi\\11-new" + System.currentTimeMillis() + ".pdf";

        addWaterRemark(sourcePath, pdfFile, fontFilePath, waterMarkText, logoFilePath);
    }

    public static void addWaterRemark(String sourcePath, String pdfFile, String fontFilePath, String waterMarkText, String logoFilePath) {
        try {
            // 读取原始 PDF 文件
            PDDocument doc = PDDocument.load(new File(sourcePath));
            doc.setAllSecurityToBeRemoved(true);

            //必须是ttf字体
            PDFont font = PDType0Font.load(doc, new File(fontFilePath));

            PDImageXObject logoImage = PDImageXObject.createFromFile(logoFilePath, doc);
            float logoImageWidth = logoImage.getWidth();
            float logoImageHeight = logoImage.getHeight();

            float maxX;
            float maxY;
            float tempX;
            float tempY;
            float xStepLength = 300f;
            float yStepLength = 200f;
            float startX = 100f;
            float startY = 100f;
            PDPageContentStream cs;

            // 透明度
            PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
            r0.setNonStrokingAlphaConstant(0.2f);
            r0.setAlphaSourceFlag(true);
            for (PDPage page : doc.getPages()) {
                maxX = page.getBBox().getUpperRightX();
                maxY = page.getBBox().getUpperRightX();

                cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true, true);
                cs.setGraphicsStateParameters(r0);
                cs.setNonStrokingColor(new Color(200, 200, 200));
                cs.setFont(font, 36f);

                // 添加图片,不能在beginText和endText之间加
                cs.drawImage(logoImage, (maxX - logoImageWidth) / 2, maxY / 2, logoImageWidth * 1.5f, logoImageHeight * 1.5f);

                cs.beginText();
                tempX = startX;
                while (tempX < maxX) {
                    tempY = startY;
                    while (tempY < maxY) {
                        cs.setTextMatrix(Matrix.getRotateInstance(45, tempX, tempY));
                        cs.showText(waterMarkText);
                        tempY += yStepLength;
                    }
                    tempX += xStepLength;
                }
                cs.endText();

                // 关闭流
                cs.close();
            }

            // 保存修改后的 PDF 文件
            doc.save(new File(pdfFile));
            doc.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


文章来源:https://blog.csdn.net/weixin_42754896/article/details/137263165
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云