基于java技术实现word、pdf文件添加水印

前言:

       在当今数字化时代,随着电子文档的广泛应用,保护文档的安全性和完整性变得尤为重要。为了防止文档被盗用、篡改或未经授权的复制,添加水印成为一种常见的解决方案。

        水印作为一种可见或隐形的标记,可以为文档增加额外的信息或标识,从而提高文档的可信度和可辨识性。无论是在商业领域中保护机密文件,还是在学术界中保护研究成果的版权,添加水印都发挥着重要的作用。

        在本文中,我们将探讨如何使用Java技术实现在Word和PDF文件中添加水印。

pdf文件添加水印:

package test;

import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;

import javax.swing.*;
import java.awt.*;
import java.io.FileOutputStream;


/**
 * @author weixinxin 2024-01-24
 **/
public class textWaterMark {
    public static void main(String[] args) {
        String waterMark = "内部使用";
        String filePath = "C:\\Users\\weixinxin_ext\\Desktop\\FLEET10463_湖北小柚智行汽车科技有限公司_20240123_GUA 保证协议-甜猫_replace_加密.pdf";
        String outFilePath = "C:\\Users\\weixinxin_ext\\Desktop\\添加多行文字水印_replace_加密_水印.pdf";

        addPdfWatermark(filePath, outFilePath, waterMark);
    }


    public static void addPdfWatermark(String inputFile, String outputFile, String waterMarkName) {
        try {
            // 水印的高和宽(参数可调节)
            int textH = 75;
            int textW = 170;
            // 间隔距离(参数可调节)
            int interval = 30;
            String owerPwd = "111";
            String userWpd = "222";
            PdfReader reader = new PdfReader(inputFile);
            PdfReader.unethicalreading = true;
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));
            //userWpd 文件加密(打开文件密码);owerPwd 操作加密(文件操作密码); permissions = 4 能打印不能复制
            //stamper.setEncryption(userWpd.getBytes(),owerPwd.getBytes(),4,false);
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
            PdfGState gs = new PdfGState();
            gs.setFillOpacity(0.2f);
            gs.setStrokeOpacity(0.4f);
            int total = reader.getNumberOfPages() + 1;
            JLabel label = new JLabel();
            label.setText(waterMarkName);
            PdfContentByte under;

            Rectangle pageRect = null;
            FontMetrics metrics;
            label.setText(waterMarkName);
            metrics = label.getFontMetrics(label.getFont());
            for (int i = 1; i < total; i++) {
                pageRect = reader.getPageSizeWithRotation(i);
                // 在内容上方加水印
                under = stamper.getOverContent(i);
                // 在内容下方加水印
                //under = stamper.getUnderContent(i);
                under.saveState();
                under.setGState(gs);
                under.beginText();
                under.setFontAndSize(base, 20);
                // 水印文字成30度角倾斜
                for (int height = interval + textH; height < pageRect.getHeight();
                     height = height + textH * 3) {
                    for (int width = interval + textW; width < pageRect.getWidth() + textW;
                         width = width + textW * 2) {
                        under.showTextAligned(Element.ALIGN_LEFT
                                , waterMarkName, width - textW,
                                height - textH, 30);
                    }
                }
                under.endText();
            }
            stamper.close();
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 结果:

 word文件添加水印:

package com.atxinxin;

import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.HeaderFooter;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ShapeLineStyle;
import com.spire.doc.documents.ShapeType;
import com.spire.doc.fields.ShapeObject;

import javax.swing.*;
import java.awt.*;
import java.io.FileOutputStream;

/**
 * @author weixinxin 2024-01-26
 **/
public class WaterMark {
    public static void main(String[] args) {
        String waterMark = "内部使用";
        String filePath = "C:\\Users\\weixinxin_ext\\Desktop\\测试word转pdf.docx";
        String outFilePath = "C:\\Users\\weixinxin_ext\\Desktop\\测试word转pdf-水印.docx";
        addWordWaterMark(filePath,outFilePath,waterMark);
    }
    
    private static void addWordWaterMark(String filePath, String outFilePath,String waterMark) {
        Document doc = new Document();
        doc.loadFromFile(filePath);
        ShapeObject shape = new ShapeObject(doc, ShapeType.Text_Plain_Text);
        shape.setWidth(60);
        shape.setHeight(20);
        //设置艺术字文本内容、位置及样式
        shape.setVerticalPosition(30);
        shape.setHorizontalPosition(20);
        shape.setRotation(315);
        shape.getWordArt().setFontFamily("宋体");
        shape.getWordArt().setText(waterMark);
        shape.setFillColor(new Color(255, 255, 255));
        shape.setLineStyle(ShapeLineStyle.Single);
        shape.setStrokeColor(new Color(185, 181, 181, 204));
        shape.setStrokeWeight(1);

        Section section;
        HeaderFooter header;
        for (int n = 0; n < doc.getSections().getCount(); n++) {
            section = doc.getSections().get(n);
            header = section.getHeadersFooters().getHeader();
            Paragraph paragraph;

            if (header.getParagraphs().getCount() > 0) {
                paragraph = header.getParagraphs().get(0);
            } else {
                paragraph = header.addParagraph();
            }
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 4; j++) {
                    shape = (ShapeObject) shape.deepClone();
                    shape.setVerticalPosition(60 + 200 * i);
                    shape.setHorizontalPosition(50 + 200 * j);
                    paragraph.getChildObjects().add(shape);
                }
            }
        }
        //保存文档
        doc.saveToFile(outFilePath, FileFormat.Docx_2013);
    }
}

 结果:

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Java中的WordPDF水印功能,你可以使用freemaker对模板解析填充数据导出Word文档的功能,然后使用itext将Word转换为PDF,最后使用itext对PDF添加水印。 以下是一个示例代码实现这个功能: ``` import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.aspose.words.Shape; import com.aspose.words.ShapeType; import java.io.FileOutputStream; public static void docToPdfWithWatermark(String inPath, String outPath, String imgUrl) { if (getLicense()) { try { FileOutputStream os = new FileOutputStream(new File(outPath)); Document doc = new Document(inPath); Shape shapeRectangle = new Shape(doc, ShapeType.RECTANGLE); doc.setBackgroundShape(shapeRectangle); // 添加水印图片 shapeRectangle.getImageData().setImage(imgUrl); // 降低对比度,增加亮度 shapeRectangle.getImageData().setContrast(0.2); shapeRectangle.getImageData().setBrightness(0.7); doc.save(os, SaveFormat.PDF); os.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 你可以参考上面的代码将输入路径(inPath)、输出路径(outPath)和水印图片路径(imgUrl)替换为你自己的路径。确保你已经获得了适用于Aspose Words的许可证。 为了实现这个功能,你需要引入Aspose Words的jar文件。你可以在这个链接中找到所需的jar文件:https://blog.csdn.net/qq_34315636/article/details/95358305 请注意,在实际使用中,你可能需要根据你的具体需求进行必要的调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值