前言
?继上次研究java实现pdf的分割 和pdf合并功能后,这次给大家贡献java实现pdf添加水印和去除水印的代码。
?
教程
引入第三方jar依赖
<!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>3.0.0-RC1</version>
</dependency>
<dependency>
<groupId>com.github.jai-imageio</groupId>
<artifactId>jai-imageio-jpeg2000</artifactId>
<version>1.3.0</version>
</dependency>
核心代码实现
该代码实现将pdf添加自定义文字水印,pdf水印去除的功能。
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.state.PDExtendedGraphicsState;
import org.apache.pdfbox.util.Matrix;
import java.awt.*;
import java.io.*;
import java.util.Iterator;
public class PDFHelper {
public static void main(String[] args) throws Exception {
addWatermark(new File("C:\\Users\\liuya\\Desktop\\pdf\\test.pdf"), "tarzan");
removeWatermark(new File("C:\\Users\\liuya\\Desktop\\pdf\\test_waterMark.pdf"));
}
/**
* pdf添加文字水印
*/
private static void addWatermark(File file, String text) {
try {
PDDocument document = Loader.loadPDF(file);
document.setAllSecurityToBeRemoved(true);
for (PDPage page : document.getPages()) {
PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
//pdf扩展图形对象
PDExtendedGraphicsState graphicsState = new PDExtendedGraphicsState();
// 透明度
graphicsState.setNonStrokingAlphaConstant(0.2f);
graphicsState.setAlphaSourceFlag(true);
cs.setGraphicsStateParameters(graphicsState);
cs.setNonStrokingColor(Color.red);//Red
cs.beginText();
cs.setFont(PDType1Font.HELVETICA_OBLIQUE, 50.0f);
// 获取旋转实例
cs.setTextMatrix(Matrix.getRotateInstance(20, 350f, 490f));
cs.showText(text);
cs.endText();
cs.close();
}
String folderPath= file.getParent();
String fileName=file.getName().substring(0,file.getName().lastIndexOf("."));
document.save(folderPath+File.separator+fileName+"_waterMark"+".pdf");
document.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* pdf去除水印
*/
public static boolean removeWatermark(File file) {
try{
//通过文件名加载文档
PDDocument document = Loader.loadPDF(file);
PDPageTree pages = document.getPages();
Iterator<PDPage> iter = pages.iterator();
while(iter.hasNext()){
PDPage page = iter.next();
PDResources resources =page.getResources();
resources.getExtGStateNames().forEach(ext->{
PDExtendedGraphicsState gState=resources.getExtGState(ext);
gState.setNonStrokingAlphaConstant(0.0f);
});
}
String folderPath= file.getParent();
String fileName=file.getName().substring(0,file.getName().lastIndexOf("."));
document.save(folderPath+File.separator+fileName+"_noMark"+".pdf");
document.close();
return true;
} catch(IOException ex){
ex.printStackTrace();
return false;
}
}
}
添加水印效果图
去除水印对比图
?
?相关文章推荐
《JAVA实现PDF合并、拆分代码工具类》
温馨提示,如有技术问题,请在评论区留言。
|