PDF文件JAVA去水印源码,java pdf增加水印示例源码

【实例简介】

【实例截图】

acdd326c5c5c04d4258eaa8ccd06fe11.png

【核心代码】

package com.test.main;

import java.io.File;

import java.io.FileOutputStream;

import java.util.ArrayList;

import java.util.List;

import com.itextpdf.text.BaseColor;

import com.itextpdf.text.Document;

import com.itextpdf.text.Element;

import com.itextpdf.text.Font;

import com.itextpdf.text.Image;

import com.itextpdf.text.PageSize;

import com.itextpdf.text.Paragraph;

import com.itextpdf.text.Phrase;

import com.itextpdf.text.Rectangle;

import com.itextpdf.text.pdf.BaseFont;

import com.itextpdf.text.pdf.PdfContentByte;

import com.itextpdf.text.pdf.PdfPCell;

import com.itextpdf.text.pdf.PdfPTable;

import com.itextpdf.text.pdf.PdfReader;

import com.itextpdf.text.pdf.PdfStamper;

import com.itextpdf.text.pdf.PdfWriter;

public class test4 {

public static void buidPDF(String pdfFile, String imageFile, String waterMarkName, int permission) {

try {

File file = File.createTempFile("tempFile", ".pdf"); // 创建临时文件

// 生成PDF

if (createPDFFile(file)) {

waterMark(file.getPath(), imageFile, pdfFile, waterMarkName,

permission); // 添加水印

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 创建PDF文件

*

* @param file

* 临时文件

* @return 成功/失败

*/

public static boolean createPDFFile(File file) {

Rectangle rect = new Rectangle(PageSize.A5);

Document document = new Document();

try {

PdfWriter.getInstance(document,

new FileOutputStream("TableCellBorder.pdf"));

document.open();

PdfPTable table = new PdfPTable(3);

PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));

cell1.setUseBorderPadding(true);

//

// Setting cell's border width and color

//

cell1.setBorderWidth(5f);

cell1.setBorderColor(BaseColor.BLUE);

table.addCell(cell1);

PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));

cell2.setUseBorderPadding(true);

//

// Setting cell's background color

//

cell2.setBackgroundColor(BaseColor.GRAY);

//

// Setting cell's individual border color

//

cell2.setBorderWidthTop(1f);

cell2.setBorderColorTop(BaseColor.RED);

cell2.setBorderColorRight(BaseColor.GREEN);

cell2.setBorderColorBottom(BaseColor.BLUE);

cell2.setBorderColorLeft(BaseColor.BLACK);

table.addCell(cell2);

PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));

cell3.setUseBorderPadding(true);

//

// Setting cell's individual border width

//

cell3.setBorderWidthTop(2f);

cell3.setBorderWidthRight(1f);

cell3.setBorderWidthBottom(2f);

cell3.setBorderWidthLeft(1f);

table.addCell(cell3);

table.completeRow();

document.add(table);

} catch (Exception e) {

e.printStackTrace();

} finally {

document.close();

}

return true;

}

public static void waterMark(String inputFile, String imageFile,

String outputFile, String waterMarkName, int permission) {

try {

PdfReader reader = new PdfReader(inputFile);

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(

outputFile));

BaseFont base = BaseFont.createFont(

"C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体

int total = reader.getNumberOfPages() 1;

Image image = Image.getInstance(imageFile);

// 图片位置

image.setAbsolutePosition(0,88);

image.setRotationDegrees(45);

PdfContentByte under;

int j = waterMarkName.length();

char c = 0;

int rise = 0;

for (int i = 1; i < total; i ) {

rise = 400;

under = stamper.getUnderContent(i);

under.beginText();

under.setFontAndSize(base, 30);

if (j >= 15) {

under.setTextMatrix(200, 120);

for (int k = 0; k < j; k ) {

under.setTextRise(rise);

c = waterMarkName.charAt(k);

under.showText(c "");

}

} else {

under.setTextMatrix(240, 100);

for (int k = 0; k < j; k ) {

under.setTextRise(rise);

c = waterMarkName.charAt(k);

under.showText(c "");

rise -= 18;

}

}

// 添加水印文字

under.endText();

// 添加水印图片

under.addImage(image);

/* // 画个圈

under.ellipse(250, 450, 350, 550);

under.setLineWidth(1f);

under.stroke();*/

}

stamper.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

String imageFilePath = "C:\\Users\\admin\\Desktop\\ruituo.png"; // 水印图片路径

String pdfFilePath = "F:\\Download\\itext.PDF"; // 文件生成路径

buidPDF(pdfFilePath, imageFilePath, "", 16);

}

public static String leftPad(String str, int i) {

int addSpaceNo = i-str.length();

String space = "";

for (int k=0; k

space= " " space;

};

String result =space str ;

return result;

}

public static void add(List list,int num){

for(int i=0;i

list.add("A" i);

}

}

public static void add2(List list,int num){

for(int i=0;i

list.add("D" i);

}

}

public static void add1(List list,int num){

for(int i=0;i

list.add("C" i);

}

}

public static String printBlank(int tmp){

String space="";

for(int m=0;m

space=space " ";

}

return space;

}

/**

* 创建一个跨多列的单元格

* @param colspan 所占列数

* @param paragraph 单元格内容文字

* @param align 对齐方式

*/

public static PdfPCell newPdfPCellByColspan(int colspan,Paragraph paragraph,int align){

PdfPTable iTable=new PdfPTable(1);

PdfPCell iCell=new PdfPCell();

iCell.setColspan(colspan);

iCell.setBorder(0);

iCell.addElement(paragraph);

iCell.setHorizontalAlignment(align);

iTable.addCell(iCell);

PdfPCell cell=new PdfPCell(iTable);

return cell;

}

/**

* 创建一个跨多行的单元格

* @param rows 所占行数

* @param paragraph 单元格内容文字

* @param align 对齐方式

*/

public static PdfPCell newPdfPCellByRows(int rows,Paragraph paragraph,int align){

PdfPTable iTable=new PdfPTable(1);

PdfPCell iCell=new PdfPCell();

iCell.setFixedHeight(iCell.getFixedHeight()*rows);

iTable.addCell(iCell);

iCell.addElement(paragraph);

iCell.setHorizontalAlignment(align);

PdfPCell cell=new PdfPCell(iTable);

return cell;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,要去除PDF文件中的水印通常需要使用专门处理PDF内容的库,如iText、Apache PDFBox等。这些库允许开发者读取、修改PDF页面的内容,包括文本、图像和元数据。以下是使用iText库的一个简单示例: ```java import com.itextpdf.text.Document; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.PdfStamper; public class RemoveWatermark { public static void main(String[] args) throws Exception { // 载原始PDF PdfReader reader = new PdfReader("input.pdf"); // 创建一个新的PDF用于保存处理后的结果 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf")); // 获取每一页并检查是否存在透明层(水印常见于这种位置) for (int page = 1; page <= reader.getNumberOfPages(); page++) { try (Document document = new Document()) { // 使用PdfCopy实例复制页面到新的文档,从而删除透明层 PdfCopy copy = new PdfCopy(document, stamper.getWriter()); copy.addPage(reader.getPage(page)); // 如果发现有透明层(例如一个半透明的文字或图片),可以尝试用纯色替换它 int opacity = reader.getOperatorList(page).getOpacity(); if (opacity > 0) { // 这里只是一个简化示例,实际操作可能需要更复杂的图像处理技术 // 您可能需要提取页面元素,应用算法去除水印,然后重新合成页面 // 提供的库应该会有相应的功能支持 } } catch (Exception e) { System.err.println("Failed to process page " + page); } } // 清理资源 stamper.close(); reader.close(); } } ``` 请注意,这只是一个基础的示例,实际的水印去除可能会更复杂,涉及到图像处理和分析技术。另外,这种方法可能并不适用于所有类型的水印,特别是那些嵌入PDF结构或密保护的水印
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值