java实现word和pdf添加文字水印

相关jar包

链接:https://pan.baidu.com/s/1VkcCsKIqSHlg5vwIOHoHeg
提取码:ju96

实现代码

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.presentation.IAutoShape;
import com.spire.presentation.PortionEx;
import com.spire.presentation.Presentation;
import com.spire.presentation.ShapeType;
import com.spire.presentation.drawing.FillFormatType;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.OutputStream;

import javax.swing.JLabel;

import org.junit.Test;

public class textWaterMark {
	public static void main(String[] args) throws Exception{
		insertTextWatermark("d:/test/水印测试文件.doc","d:/test/textWaterMark.docx");
		pdfInsertWatermark("d:/test/水印测试文件.pdf","d:/test/textWaterMark.pdf");
	}
	/**
	 * 添加word文字水印
	 * @param srcFilePath 源文件路径(需要添加水印的文件路径)
	 * @param descFilePath 目标文件路径(文件输出的路径)
	 * @throws Exception
	 */
    public static void insertTextWatermark(String srcFilePath,String descFilePath) {
    	 //以模板形式打开word文档
        Document document = new Document(srcFilePath); //源文件路径
        TextWatermark txtWatermark = new TextWatermark();
        txtWatermark.setText("E-iceblue");//设置水印内容
        txtWatermark.setFontSize(25);//设置水印字体大小
        txtWatermark.setColor(Color.blue); //设置水印颜色
        txtWatermark.setLayout(WatermarkLayout.Diagonal);//水印版式
        
        Section section = document.getSections().get(0);
        section.getDocument().setWatermark(txtWatermark);
        //另存为docx文件
        document.saveToFile(descFilePath, FileFormat.Docx);
        //javaWeb下载方式,以文件流方式输出到浏览器
//        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
//        document.saveToFile(toClient, FileFormat.Docx);

    }
	/**
	 * 添加pdf文字水印
	 * @param srcFilePath 源文件路径
	 * @param descFilePath 目标文件路径
	 * @throws Exception
	 */
	public static void pdfInsertWatermark(String srcFilePath,String descFilePath)throws Exception {
    	PdfReader reader = new PdfReader(srcFilePath);
    	PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(descFilePath));
        PdfGState gs = new PdfGState();
        BaseFont font =  BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        gs.setFillOpacity(0.4f);// 设置透明度

        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        for (int i = 1; i < total; i++) {
            content = stamper.getOverContent(i);
            content.beginText();
            content.setGState(gs);
            content.setColorFill(BaseColor.RED); //水印颜色
            content.setFontAndSize(font, 30); //水印字体样式和大小
            content.showTextAligned(Element.ALIGN_CENTER,"水印测试", 400, 400, -15); //水印内容和水印位置
            content.endText();
          }
          stamper.close();
    }
}

你可能感兴趣的:(java)