Backend Development 15 min read

How to Add Watermarks to PDF Files Using Spring Boot and Various Java Libraries

This article demonstrates multiple ways to add text or image watermarks to PDF documents within a Spring Boot application, covering Apache PDFBox, iText, Ghostscript, Free Spire.PDF, and Aspose.PDF, and provides complete Maven dependencies and code examples for each method.

Top Architect
Top Architect
Top Architect
How to Add Watermarks to PDF Files Using Spring Boot and Various Java Libraries

PDF (Portable Document Format) is a popular file format that can be viewed and printed across many operating systems. Adding watermarks to PDFs helps with identification and copyright protection. This article explains how to implement PDF watermarking in a Spring Boot project using several Java libraries and tools.

Method 1: Using Apache PDFBox

PDFBox is a free, open‑source Java library for creating, modifying, and extracting PDF content. Add the dependency to pom.xml and use the following code to load a PDF, iterate over its pages, and draw a text watermark.

<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.24</version>
</dependency>
PDDocument document = PDDocument.load(new File("original.pdf"));
for (int i = 0; i < document.getNumberOfPages(); i++) {
    PDPage page = document.getPage(i);
    PDPageContentStream cs = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
    cs.setFont(PDType1Font.HELVETICA_BOLD, 36);
    cs.setNonStrokingColor(200, 200, 200);
    cs.beginText();
    cs.newLineAtOffset(100, 100);
    cs.showText("Watermark");
    cs.endText();
    cs.close();
}
document.save(new File("output.pdf"));
document.close();

Method 2: Using iText

iText is another popular Java PDF library. Add the Maven dependency and use the following snippet to read a PDF, add a text watermark on each page, and save the result.

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13</version>
</dependency>
PdfReader reader = new PdfReader("original.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf"));
int pageCount = reader.getNumberOfPages();
for (int i = 1; i <= pageCount; i++) {
    PdfContentByte cb = stamper.getUnderContent(i);
    cb.beginText();
    cb.setFontAndSize(BaseFont.createFont(), 36f);
    cb.setColorFill(BaseColor.LIGHT_GRAY);
    cb.showTextAligned(Element.ALIGN_CENTER, "Watermark", 300, 400, 45);
    cb.endText();
}
stamper.close();
reader.close();

Method 3: Using Ghostscript Command Line

Ghostscript is a free, open‑source PDF processor. After installing Ghostscript, run the following command to add a watermark directly from the terminal.

gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=output.pdf -c "newpath /Helvetica-Bold findfont 36 scalefont setfont 0.5 setgray 200 200 moveto (Watermark) show showpage" original.pdf

Method 4: Using Free Spire.PDF for Java

Free Spire.PDF provides a simple API for PDF manipulation. Add its Maven dependency and use the code below to load a PDF, add a text watermark (or an image watermark), and save the file.

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId>free-spire-pdf-for-java</artifactId>
    <version>1.9.6</version>
</dependency>
PdfDocument pdf = new PdfDocument();
pdf.loadFromFile("original.pdf");
for (int i = 0; i < pdf.getPages().getCount(); i++) {
    PdfPageBase page = pdf.getPages().get(i);
    PdfWatermark wm = new PdfWatermark("Watermark");
    wm.setFont(new PdfFont(PdfFontFamily.Helvetica, 36));
    wm.setOpacity(0.5f);
    page.getWatermarks().add(wm);
}
pdf.saveToFile("output.pdf");
pdf.close();

Method 5: Using Aspose.PDF for Java

Aspose.PDF is a powerful commercial library. After adding the dependency, you can expose RESTful endpoints in Spring Boot to add text or image watermarks to uploaded PDFs.

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>21.4</version>
</dependency>
@PostMapping("/addTextWatermark")
public ResponseEntity
addTextWatermark(@RequestParam("file") MultipartFile file) throws IOException {
    Document pdf = new Document(file.getInputStream());
    TextStamp ts = new TextStamp("Watermark");
    ts.setVerticalAlignment(VerticalAlignment.Center);
    ts.setHorizontalAlignment(HorizontalAlignment.Center);
    pdf.getPages().get_Item(1).addStamp(ts);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    pdf.save(out);
    return ResponseEntity.ok()
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"watermarked.pdf\"")
        .contentType(MediaType.APPLICATION_PDF)
        .body(out.toByteArray());
}

@PostMapping("/addImageWatermark")
public ResponseEntity
addImageWatermark(@RequestParam("file") MultipartFile file) throws IOException {
    Document pdf = new Document(file.getInputStream());
    ImageStamp is = new ImageStamp("watermark.png");
    is.setWidth(100);
    is.setHeight(100);
    is.setVerticalAlignment(VerticalAlignment.Center);
    is.setHorizontalAlignment(HorizontalAlignment.Center);
    pdf.getPages().get_Item(1).addStamp(is);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    pdf.save(out);
    return ResponseEntity.ok()
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"watermarked.pdf\"")
        .contentType(MediaType.APPLICATION_PDF)
        .body(out.toByteArray());
}

Conclusion

The article presented five practical approaches to add watermarks to PDFs in a Spring Boot environment. Choose the library or tool that best fits your project requirements, and always keep a backup of the original PDF before applying modifications.

JavaSpring BootiTextPDFBoxAsposeGhostscriptPDF watermark
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.