Java生成文本水印

本文介绍了如何使用Java生成文本水印,支持设置半透明背景和文字描边,并能自动适应字体大小。虽然目前不支持多行文字,但提供的代码示例可供开发者根据需求进行调整。
摘要由CSDN通过智能技术生成

Java生成文本水印,支持半透明背景、文字描边,能根据字体自适应宽高,有需要的小伙伴可根据自身情况调整。看效果~

暂不支持多行文字

微软雅黑
在这里插入图片描述
宋体
在这里插入图片描述

半透明背景
在这里插入图片描述
透明背景(字体有描边)在这里插入图片描述

参考代码

/**
     * 生成文本水印
     *
     * @param text      文本
     * @param size      字体大小
     * @param textColor 字体颜色
     * @param bgMask    是否使用背景遮罩
     * @param padding   上下左右填充
     */
    public BufferedImage genTextWatermark(String text, int size, Color textColor, boolean bgMask, int padding) {
   

        final Font font = new Font("微软雅黑", Font.BOLD, size);
        FontMetrics fm = FontDesignMetrics.getMetrics(font);
        int stringWidth = fm.stringWidth(text);
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Java 可以使用 Apache PDFBox 库来生成 PDF 文档的水印。以下是一个示例代码: ``` import java.io.File; import java.io.IOException; 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.PDType1Font; public class AddWatermark { public static void main(String[] args) throws IOException { // Load the PDF document File file = new File("input.pdf"); PDDocument document = PDDocument.load(file); // Create a font object PDFont font = PDType1Font.HELVETICA_BOLD; // Iterate over each page of the document for (PDPage page : document.getPages()) { // Start a new content stream which will "hold" the to be created content PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true); // Set the font and font size contentStream.setFont(font, 36); // Set the color of the text contentStream.setNonStrokingColor(255, 255, 255); // Define the text to be added as watermark String text = "CONFIDENTIAL"; // Get the width of the text float textWidth = font.getStringWidth(text) / 1000 * 36; // Get the height of the text float textHeight = font.getFontDescriptor().getFontBoundingBox().getHeight() / 1000 * 36; // Calculate the position of the text float x = (page.getMediaBox().getWidth() - textWidth) / 2; float y = (page.getMediaBox().getHeight() - textHeight) / 2; // Rotate the text 45 degrees counterclockwise contentStream.saveGraphicsState(); contentStream.transform(Matrix.getRotateInstance(-45, x, y)); // Draw the text on the page contentStream.beginText(); contentStream.newLineAtOffset(x, y); contentStream.showText(text); contentStream.endText(); // Restore the graphics state contentStream.restoreGraphicsState(); // Close the content stream contentStream.close(); } // Save the PDF document document.save("output.pdf"); // Close the document document.close(); } } ``` 这段代码会读取名为 `input.pdf` 的文档,在每一页上生成一个文本水印 “CONFIDENTIAL”,并将生成的文档保存为 `output
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值