pdf生成水印

使用pdfbox可以生成水印,但是对中文支持不友好,要生成英文水印还是可以用的。


      src
      
         
            maven-compiler-plugin
            3.3
            
               1.8
               1.8
             
         
       
    

     
       
         org.apache.pdfbox 
         pdfbox 
         2.0.1 
         

       
         org.apache.pdfbox 
         fontbox 
         2.0.0 
      

        
         org.apache.pdfbox 
         jempbox 
         1.8.11 
       

      
         org.apache.pdfbox 
         xmpbox 
         2.0.0 
       

       
         org.apache.pdfbox 
         preflight 
         2.0.0 
       

       
         org.apache.pdfbox 
         pdfbox-tools 
         2.0.0 
      

   

测试类:

package com.hbk.maven_test;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.util.Matrix;


/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception
    {
        markTxt("C:\\Users\\yangfeng\\Downloads\\hbk_test.pdf", "C:\\Users\\yangfeng\\Downloads\\hbk_shuiyin.pdf");
        System.out.println("生成水印完成!");
    }
    
    /**
     * 读取文本
     */
    public static void readString(String source) throws IOException{
        PDDocument doc = PDDocument.load(new File(source));
        PDFTextStripper stripper = new PDFTextStripper();
        // 设置按顺序输出
        stripper.setSortByPosition(true);
        stripper.setStartPage(1);
        stripper.setEndPage(doc.getNumberOfPages());
        String text = stripper.getText(doc);
        System.out.println(text);
    }
    
    /**
     * 水印
     */
    public static void markTxt(String source, String target) throws InvalidPasswordException, IOException {
        File tmpPDF = new File(target);
        PDDocument doc = PDDocument.load(new File(source));
        doc.setAllSecurityToBeRemoved(true);
        for (PDPage page : doc.getPages()) {
            PDPageContentStream cs = new PDPageContentStream(doc, page, PDPageContentStream.AppendMode.APPEND, true,
                    true);
            
            String ts = "huangbaokang";
            PDFont font = PDType1Font.HELVETICA_OBLIQUE;
            PDExtendedGraphicsState r0 = new PDExtendedGraphicsState();
            // 透明度
            r0.setNonStrokingAlphaConstant(0.2f);
            r0.setAlphaSourceFlag(true);
            cs.setGraphicsStateParameters(r0);
            cs.setNonStrokingColor(200, 0, 0);// Red
            cs.beginText();
            float fontSize = 50.0f;
            cs.setFont(font, fontSize);
            // 获取旋转实例
            cs.setTextMatrix(Matrix.getRotateInstance(20, 350f, 490f));
            cs.showText(ts);
            cs.endText();

            cs.close();
        }
        doc.save(tmpPDF);
    }
}

你可能感兴趣的:(工具类)