项目描述:最近有一个需求是在文件上传的时候,需要添加水印来防伪,防止篡改。网上找了下,发现一个aspose可以实现
jar包在最下面!!!
package com.wlkj.watermark.word;
import com.aspose.words.*;
import com.aspose.words.Shape;
import java.awt.*;
public class WordWatermarkUtil {
public static void main(String[] args) {
try {
Document document = new Document("C:\\Users\\Administrator\\Desktop\\背影.docx");
WordWatermarkUtil.WaterMarkMore(document,"我的水印");
//文件输出路径
document.save("C:\\Users\\Administrator\\Desktop\\背影2.docx");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 为word文档添加水印
* @param doc word文档模型
* @param watermarkText 需要添加的水印字段
* @throws Exception
*/
public static void insertWatermarkText(Document doc, String watermarkText) throws Exception {
Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
//水印内容
watermark.getTextPath().setText(watermarkText);
//水印字体
watermark.getTextPath().setFontFamily("宋体");
//水印宽度
watermark.setWidth(100);
//水印高度
watermark.setHeight(20);
//旋转水印
watermark.setRotation(-40);
//水印颜色 浅灰色
watermark.getFill().setColor(Color.lightGray);
watermark.setStrokeColor(Color.lightGray);
//设置相对水平位置
watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
//设置相对垂直位置
watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
//设置包装类型
watermark.setWrapType(WrapType.NONE);
//设置垂直对齐
watermark.setVerticalAlignment(VerticalAlignment.CENTER);
//设置文本水平对齐方式
watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.appendChild(watermark);
for (Section sect : doc.getSections())
{
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
System.out.println("Watermark Set");
}
/**
* 在页眉中插入水印
* @param watermarkPara
* @param sect
* @param headerType
* @throws Exception
*/
private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception{
HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
if (header == null)
{
header = new HeaderFooter(sect.getDocument(), headerType);
sect.getHeadersFooters().add(header);
}
header.appendChild(watermarkPara.deepClone(true));
}
/**
* 设置水印属性
* @param doc
* @param wmText
* @param left
* @param top
* @return
* @throws Exception
*/
public static Shape ShapeMore(Document doc, String wmText, double left, double top)throws Exception{
Shape waterShape = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
//水印内容
waterShape.getTextPath().setText(wmText);
//水印字体
waterShape.getTextPath().setFontFamily("宋体");
//水印宽度
waterShape.setWidth(40);
//水印高度
waterShape.setHeight(13);
//旋转水印
waterShape.setRotation(-40);
//水印颜色 浅灰色
/*waterShape.getFill().setColor(Color.lightGray);
waterShape.setStrokeColor(Color.lightGray);*/
waterShape.setStrokeColor(new Color(210,210,210));
//将水印放置在页面中心
waterShape.setLeft(left);
waterShape.setTop(top);
//设置包装类型
waterShape.setWrapType(WrapType.NONE);
return waterShape;
}
/**
* 插入多个水印
* @param mdoc
* @param wmText
* @throws Exception
*/
public static void WaterMarkMore(Document mdoc, String wmText)throws Exception{
Paragraph watermarkPara = new Paragraph(mdoc);
for (int j = 0; j < 500; j = j + 100)
{
for (int i = 0; i < 700; i = i + 85)
{
Shape waterShape = ShapeMore(mdoc, wmText, j, i);
watermarkPara.appendChild(waterShape);
}
}
for (Section sect : mdoc.getSections())
{
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
}
}
}
com.itextpdf
itextpdf
5.5.13
com.itextpdf
itext-asian
5.2.0
package com.wlkj.watermark.pdf;
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;
import java.io.InputStream;
public class PdfwaterMarkUtil {
private static int interval = -5;
public static void wateMark(InputStream inputStream, String outputFile, String waterMarkName){
try{
PdfReader reader = new PdfReader(inputStream);
PdfStamper stamper = new PdfStamper(reader,new FileOutputStream(outputFile));
BaseFont base = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.EMBEDDED);
Rectangle pageRect = null;
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.3f);
gs.setStrokeOpacity(0.4f);
int total = reader.getNumberOfPages() + 1;
JLabel label = new JLabel();
FontMetrics metrics;
int textH = 0;
int textW = 0;
label.setText(waterMarkName);
metrics = label.getFontMetrics(label.getFont());
textH = metrics.getHeight();
textW = metrics.stringWidth(label.getText());
PdfContentByte under;
for (int i = 1; i < total; i++) {
pageRect = reader.getPageSizeWithRotation(i);
under = stamper.getOverContent(i);
under.saveState();
under.setGState(gs);
under.beginText();
under.setFontAndSize(base, 20);
under.setRGBColorFill(145,145,145);
// 水印文字成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) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) {
//wateMark("C:\\Users\\Administrator\\Desktop\\背影.pdf","C:\\Users\\Administrator\\Desktop\\背影2.pdf","我的水印");
}
}
效果图:
demo地址
链接:https://pan.baidu.com/s/1Q0kyl_J9Hfy_Rx4aK2w_Lw
提取码:gqh2