我想使用itext7作为透明水印将图像添加到pdf中。我正在使用pdfcanvas和PdfExtGState。但图片显示在页面文字的顶部。虽然文本没有丢失,但是变得越来越模糊。我想解决这个问题。预期的结果是将图像推到背景,并且文本应显示在其顶部。
PdfDocument bodyPDFDoc = new PdfDocument(new PdfReader(in_filePath+pdfDest.getName()), new PdfWriter(in_filePath+fileName1+"_WATERMARK.pdf"));
Document bodyDoc = new Document(bodyPDFDoc);
int numPages = bodyPDFDoc.getNumberOfPages();
ImageData img = ImageDataFactory.create(in_watermarkImgFileName);// NEED TO GIVE WATERMARK FILE PATH
float w = img.getWidth();
float h = img.getHeight();
// transparency
PdfExtGState gs1 = new PdfExtGState();
gs1.setFillOpacity(0.2f);
// properties
PdfCanvas canvasBodyPDFDoc = null;
Rectangle pagesize;
float x, y;
// loop over every page
for (int pageNo = 1; pageNo <= numPages; pageNo++) {
if(pageNo>1){
pagesize = bodyPDFDoc.getPage(pageNo).getPageSize();
x = (pagesize.getLeft() + pagesize.getRight()) / 2;
y = (pagesize.getTop() + pagesize.getBottom()) / 2;
canvasBodyPDFDoc = new PdfCanvas(bodyPDFDoc.getPage(pageNo));
canvasBodyPDFDoc.saveState();
canvasBodyPDFDoc.setExtGState(gs1);
canvasBodyPDFDoc.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2), false);
canvasBodyPDFDoc.restoreState();
}
}
请帮助。