使用pdfbox可以生成水印,但是对中文支持不友好,要生成英文水印还是可以用的。
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>jempbox</artifactId>
<version>1.8.11</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>xmpbox</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>preflight</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox-tools</artifactId>
<version>2.0.0</version>
</dependency>
</dependencies>
测试类:
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);
}
}