java 对word添加水印 (aspose .jar)

package com.tythin.tyboot.core.util;

import com.aspose.words.Document;
import com.aspose.words.HeaderFooter;
import com.aspose.words.HeaderFooterType;
import com.aspose.words.HorizontalAlignment;
import com.aspose.words.Paragraph;
import com.aspose.words.RelativeHorizontalPosition;
import com.aspose.words.RelativeVerticalPosition;
import com.aspose.words.Section;
import com.aspose.words.Shape;
import com.aspose.words.ShapeType;
import com.aspose.words.VerticalAlignment;
import com.aspose.words.WrapType;
import org.springframework.cglib.core.internal.Function;

/**
 * @author kuangQingLin
 * @version 1.0
 * @date 2019/10/21/021 15:03
 **/
public class WatermarkWordUtil {
    public static void main(String[] args) {

        //这个加水印文字方法可用
        try {
            Document document = new Document("C:\\Users\\Administrator\\Desktop\\人事合同.docx");
            insertWatermarkText(document, "我的水印");
            //文件输出路径
            document.save("C:\\Users\\Administrator\\Desktop\\人事合同1111.docx");

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

    /**
     * 加水印方法
     *
     * @param doc           word文件流
     * @param watermarkText 水印内容
     */
    public static void insertWatermarkText(Document doc, String watermarkText) {
        // 居中
        insertWatermarkText(doc, watermarkText, new Function<Shape, Object>() {
            @Override
            public Object apply(Shape watermark) {
                // Place the watermark in the page center.
                watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
                watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
                watermark.setWrapType(WrapType.NONE);
                watermark.setVerticalAlignment(VerticalAlignment.CENTER);
                watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
                return null;
            }
        });
        // 顶部
        insertWatermarkText(doc, watermarkText, new Function<Shape, Object>() {
            @Override
            public Object apply(Shape watermark) {
                watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
                watermark.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN);
                watermark.setWrapType(WrapType.NONE);
                //  我们需要自定义距离顶部的高度
                watermark.setVerticalAlignment(VerticalAlignment.TOP);
                watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
                //  watermark.setTop(120);
                return null;
            }
        });

        //尾部
        insertWatermarkText(doc, watermarkText, new Function<Shape, Object>() {
            @Override
            public Object apply(Shape watermark) {
                watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.MARGIN);
                watermark.setRelativeVerticalPosition(RelativeVerticalPosition.MARGIN);
                watermark.setWrapType(WrapType.NONE);
                // 我们需要自定义距离顶部的高度
                watermark.setVerticalAlignment(VerticalAlignment.BOTTOM);
                watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
                // 设置距离顶部的高度
                //   watermark.setTop(480);

                return null;
            }
        });
    }

    private static void insertWatermarkText(Document doc, String watermarkText,
                                            Function<Shape, Object> watermaskPositionConfigFunc) {

        Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
        watermark.getTextPath().setText(watermarkText);
        // 这里设置为宋体可以保证在转换为PDF时中文不是乱码.
        watermark.getTextPath().setFontFamily("WeiRuanYaHei");
        //WeiRuanYaHei 宋体

        try {
            // 水印大小
            watermark.setWidth(150);
            watermark.setHeight(30);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 左下到右上
        watermark.setRotation(-20);
//字体RGB颜色
final String colorStr = "EE8262"; watermark.getFill().setColor(new java.awt.Color(Integer.parseInt(colorStr, 16))); watermark.setStrokeColor(new java.awt.Color(Integer.parseInt(colorStr, 16))); watermaskPositionConfigFunc.apply(watermark); Paragraph watermarkPara = new Paragraph(doc); watermarkPara.appendChild(watermark); for (Section sect : doc.getSections()) { insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY); } } private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) { HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType); if (header == null) { header = new HeaderFooter(sect.getDocument(), headerType); sect.getHeadersFooters().add(header); } try { header.appendChild(watermarkPara.deepClone(true)); } catch (Exception e) { throw new RuntimeException(e); } } }

 

posted @ 2019-10-21 17:22  夏风中的Young_Uncle  阅读(3377)  评论(2编辑  收藏  举报