How to watermark PDFs using text or images?

前端 未结 2 1318
孤独总比滥情好
孤独总比滥情好 2020-12-03 15:42

I have a bunch of PDF documents in a folder and I want to augment them with a watermark. What are my options from a Java serverside context?

Preferably the watermark

2条回答
  •  不知归路
    2020-12-03 16:42

    Best option is iText. Check a watermark demo here

    Important part of the code (where the watermar is inserted) is this:

    public class Watermark extends PdfPageEventHelper {
    
            @Override
            public void onEndPage(PdfWriter writer, Document document) {
                 // insert here your watermark
            }
    

    Read carefully the example.

    onEndPage() method will be something like (in my logo-watermarks I use com.itextpdf.text.Image;):

    Image image = Image.getInstance(this.getClass().getResource("/path/to/image.png"));
    
    // set transparency
    image.setTransparency(transparency);     
    
    // set position
    image.setAbsolutePosition(absoluteX, absoluteY);
    
    // put into document
    document.add(image);
    

提交回复
热议问题