• 给pdf文件加水印(aspose)


    //要jar  加群找群主要。

    package com.tythin.tyboot.core.util;
    
    import com.aspose.pdf.Color;
    import com.aspose.pdf.Document;
    import com.aspose.pdf.FontRepository;
    import com.aspose.pdf.FontStyles;
    import com.aspose.pdf.PageCollection;
    import com.aspose.pdf.Rotation;
    import com.aspose.pdf.TextStamp;
    
    import java.io.InputStream;
    
    /**
     * @author kuangQingLin
     * @version 1.0
     * @date 2019/10/21/021 15:39
     **/
    public class WatermarkPdfUtil {
        public static void main(String[] args) {
    
            Document pdfDocument = new Document("C:\Users\Administrator\Desktop\采购合同.pdf");
    
            TextStamp textStamp = new TextStamp("合同水印");
            textStamp.setBackground(true);
            textStamp.setXIndent(200);
            textStamp.setYIndent(100);
            textStamp.setRotate(Rotation.None);
            textStamp.getTextState().setFont( FontRepository.findFont("Arial"));
            textStamp.getTextState().setFontSize(34.0F);
            textStamp.getTextState().setFontStyle(FontStyles.Bold);
            textStamp.getTextState().setFontStyle(FontStyles.Italic);
            textStamp.getTextState().setForegroundColor(Color.getPink());
    
            TextStamp textStamp1 = new TextStamp("合同水印");
            textStamp1.setBackground(true);
            textStamp1.setXIndent(200);
            textStamp1.setYIndent(400);
            textStamp1.setRotate(Rotation.None);
            textStamp1.getTextState().setFont( FontRepository.findFont("Arial"));
            textStamp1.getTextState().setFontSize(34.0F);
            textStamp1.getTextState().setFontStyle(FontStyles.Bold);
            textStamp1.getTextState().setFontStyle(FontStyles.Italic);
            textStamp1.getTextState().setForegroundColor(Color.getPink());
    
    
    
    
            TextStamp textStamp2 = new TextStamp("合同水印");
            textStamp2.setBackground(true);
            textStamp2.setXIndent(200);
            textStamp2.setYIndent(700);
            textStamp2.setRotate(Rotation.None);
            textStamp2.getTextState().setFont( FontRepository.findFont("Arial"));
            textStamp2.getTextState().setFontSize(34.0F);
            textStamp2.getTextState().setFontStyle(FontStyles.Bold);
            textStamp2.getTextState().setFontStyle(FontStyles.Italic);
            textStamp2.getTextState().setForegroundColor(Color.getPink());
    
    
            PageCollection pages = pdfDocument.getPages();
            int size = pages.size();
            for (int i = 0; i < size; i++) {
                pages.get_Item(i + 1).addStamp(textStamp);
                pages.get_Item(i + 1).addStamp(textStamp1);
                pages.get_Item(i + 1).addStamp(textStamp2);
            }
    
            pdfDocument.save("C:\Users\Administrator\Desktop\采购合同11.pdf");
    
        }
    
    
        /**
         * 给pdf文件加水印
         * 水印字体不支持宋体,不要乱改字体 !!!
         * @param inputStream 输入流
         * @param path 输出路径
         */
        public static void addWater(InputStream inputStream, String path) {
            Document pdfDocument = new Document(inputStream);
    
            TextStamp textStamp = new TextStamp("合同水印");
            textStamp.setBackground(true);
            textStamp.setXIndent(200);
            textStamp.setYIndent(100);
            textStamp.setRotate(Rotation.None);
            textStamp.getTextState().setFont( FontRepository.findFont("Arial"));
            textStamp.getTextState().setFontSize(34.0F);
            textStamp.getTextState().setFontStyle(FontStyles.Bold);
            textStamp.getTextState().setFontStyle(FontStyles.Italic);
            textStamp.getTextState().setForegroundColor(Color.getPink());
    
            TextStamp textStamp1 = new TextStamp("合同水印");
            textStamp1.setBackground(true);
            textStamp1.setXIndent(200);
            textStamp1.setYIndent(400);
            textStamp1.setRotate(Rotation.None);
            textStamp1.getTextState().setFont( FontRepository.findFont("Arial"));
            textStamp1.getTextState().setFontSize(34.0F);
            textStamp1.getTextState().setFontStyle(FontStyles.Bold);
            textStamp1.getTextState().setFontStyle(FontStyles.Italic);
            textStamp1.getTextState().setForegroundColor(Color.getPink());
    
    
    
    
            TextStamp textStamp2 = new TextStamp("合同水印");
            textStamp2.setBackground(true);
            textStamp2.setXIndent(200);
            textStamp2.setYIndent(700);
            textStamp2.setRotate(Rotation.None);
            textStamp2.getTextState().setFont( FontRepository.findFont("Arial"));
            textStamp2.getTextState().setFontSize(34.0F);
            textStamp2.getTextState().setFontStyle(FontStyles.Bold);
            textStamp2.getTextState().setFontStyle(FontStyles.Italic);
            textStamp2.getTextState().setForegroundColor(Color.getPink());
            PageCollection pages = pdfDocument.getPages();
            int size = pages.size();
            //这里下标从1 开始,切记
            for (int i = 0; i < size; i++) {
                pages.get_Item(i + 1).addStamp(textStamp);
                pages.get_Item(i + 1).addStamp(textStamp1);
                pages.get_Item(i + 1).addStamp(textStamp2);
            }
            pdfDocument.save(path);
        }
    
    }

     有疑问:加Q

  • 相关阅读:
    java简单计算器,只能鼠标点击数字
    可以掉落和滑动的星星
    servlet生成数字验证码
    web服务器底层-http请求与相应
    Codeforces 990D Graph And Its Complement 【构造】【性质】
    2018美团CodeM编程大赛 Round A Problem 2 下棋 【贪心】
    Codeforces 988F Rain and Umbrellas 【dp】
    Codeforces 988D Points and Powers of Two 【性质】【卡常】
    Codeforces 981D Bookshelves 【dp】【性质】
    POJ 2104 K-th Number 【主席树】【不带修改】
  • 原文地址:https://www.cnblogs.com/woshuaile/p/11865072.html
Copyright © 2020-2023  润新知