How to Apply Custom Watermarks to PDFs

This article was translated from English: Does it need improvement?
Translated
View the article in English

Watermarking is a popular technique for protecting PDF documents and asserting ownership or status, such as marking them as "Confidential" or branding them with a logo. IronPDF offers a highly flexible solution by allowing you to create custom watermarks using HTML strings with full CSS support. This enables complete customization, allowing you to style watermarks with all the possibilities that HTML and CSS offer.

This guide will demonstrate different types of watermarks—text, image, watermark location, opacity, and rotation adjustments, as well as advanced methods using TextStamper and ImageStamper.

Apply Text Watermark Example

To apply a simple text watermark to a PDF document, use the applyWatermark method. This method allows you to input text using HTML and CSS for advanced styling. For example, let's use this method to add the text 'Confidential' in red color to the PDF.

import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:red;'>Confidential</h1>";

        // Apply the watermark using HTML string
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("text_watermark.pdf");
    }
}
import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:red;'>Confidential</h1>";

        // Apply the watermark using HTML string
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("text_watermark.pdf");
    }
}
JAVA

Output

The resulting PDF file, 'text_watermark.pdf,' will have the specified watermark applied to all its pages, with the text 'Confidential' displayed in red at the center of the document.


Image Watermark Example

Using the same method, you can apply images as watermarks, supporting various formats such as PNG, JPEG, SVG, and more. The image can be styled and positioned using CSS within the HTML string.

import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // Image HTML watermark
        String watermarkHtml = "<img src='logo.png' style='width:100px;'/>";

        // Apply the image watermark
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("image_watermark.pdf");
    }
}
import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // Image HTML watermark
        String watermarkHtml = "<img src='logo.png' style='width:100px;'/>";

        // Apply the image watermark
        pdf.applyWatermark(watermarkHtml);

        // Save the PDF to a new file
        pdf.saveAs("image_watermark.pdf");
    }
}
JAVA

Output

The resulting PDF file, 'image_watermark.pdf,' will have the specified image 'logo.png' applied as a watermark on all pages. The image will be displayed with a width of 100 pixels.


Watermark Opacity and Rotation Example

You can customize the watermark's appearance by adjusting its opacity and applying rotation. The applyWatermark method allows you to specify both properties as parameters.

import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:blue;'>Confidential</h1>";

        // Apply the HTML watermark with 30% opacity, positioned at the top-left corner of each page
        pdf.applyWatermark(watermarkHtml, 30, VerticalAlignment.TOP, HorizontalAlignment.LEFT);

        // Save the PDF to a new file
        pdf.saveAs("watermark_opacity_rotation.pdf");
    }
}
import java.io.IOException;
import java.nio.file.Paths;

import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.VerticalAlignment;

public class Main {
    public static void main(String[] args) throws IOException {
        // Set the license key for IronPDF
        License.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01");

        // Load an existing PDF document from file
        PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));

        // HTML string for watermark
        String watermarkHtml = "<h1 style='color:blue;'>Confidential</h1>";

        // Apply the HTML watermark with 30% opacity, positioned at the top-left corner of each page
        pdf.applyWatermark(watermarkHtml, 30, VerticalAlignment.TOP, HorizontalAlignment.LEFT);

        // Save the PDF to a new file
        pdf.saveAs("watermark_opacity_rotation.pdf");
    }
}
JAVA

Output

The resulting PDF file, 'watermark_opacity_rotation.pdf,' will have the specified watermark applied to all its pages with 30% opacity. The watermark text, 'Confidential,' in blue will be aligned to the top-left corner of each page.

Applying the Watermark: The applyWatermark method applies the HTML-based watermark to all pages of the PDF with additional options:

  • Opacity: The watermark is applied with 30% opacity, making it partially transparent.
  • Vertical Alignment: The watermark is aligned to the TOP of the page. Other vertical alignment options include:
    • TOP: Watermark appears at the top of the page.
    • MIDDLE: Watermark appears in the middle of the page.
    • BOTTOM: Watermark appears at the bottom of the page.
  • Horizontal Alignment: The watermark is aligned to the LEFT of the page. Other horizontal alignment options include:
    • LEFT: Watermark appears on the left side of the page.
    • CENTER: Watermark appears at the center of the page.
    • RIGHT: Watermark appears on the right side of the page.

Preguntas Frecuentes

¿Cómo puedo agregar una marca de agua personalizada a un PDF en Java?

Para agregar una marca de agua personalizada a un PDF en Java, puede usar IronPDF para utilizar cadenas HTML y CSS para crear y diseñar su marca de agua. IronPDF permite insertar marcas de agua de texto o imagen con opciones personalizables.

¿Cuáles son los tipos de marcas de agua que se pueden aplicar usando IronPDF?

Con IronPDF, puede aplicar marcas de agua tanto de texto como de imagen a PDFs. Las marcas de agua de texto se pueden diseñar con HTML y CSS, mientras que las marcas de agua de imagen admiten formatos como PNG y JPEG.

¿Cómo ajusto la posición de una marca de agua en una página PDF?

Puede ajustar la posición de una marca de agua usando IronPDF configurando parámetros para la alineación vertical y horizontal. Las opciones incluyen SUPERIOR, MEDIO, INFERIOR para vertical, e IZQUIERDA, CENTRO, DERECHA para la alineación horizontal.

¿Puedo cambiar la opacidad y rotación de una marca de agua en un PDF?

Sí, IronPDF le permite personalizar la opacidad y la rotación de las marcas de agua. Esto le permite ajustar la transparencia y la orientación para satisfacer los requisitos de diseño de su documento.

¿Es posible aplicar marcas de agua a páginas específicas de un PDF?

Sí, IronPDF proporciona la funcionalidad para aplicar marcas de agua a páginas específicas. Puede especificar los números de las páginas al usar el método de aplicación de marcas de agua.

¿Cómo agrego una marca de agua de texto con fuente y color específicos a un PDF?

Para agregar una marca de agua de texto con una fuente y color específicos, puede utilizar estilos HTML y CSS dentro de IronPDF. Defina la fuente y el color deseados en su cadena HTML, luego aplíquela como una marca de agua.

¿Puedo usar CSS para diseñar mis marcas de agua en PDFs?

Sí, IronPDF admite CSS para diseñar marcas de agua, lo que le permite personalizar ampliamente la apariencia de las marcas de agua de texto e imagen.

¿Cuál es el método para guardar un PDF después de aplicar marcas de agua usando IronPDF?

Después de aplicar marcas de agua a un PDF usando IronPDF, puede guardar el documento modificado usando el método saveAs, especificando la ruta y el nombre del archivo.

¿Cómo aplico una marca de agua de imagen a un documento PDF?

Para aplicar una marca de agua de imagen, cargue su imagen en un formato compatible como PNG o JPEG, y use IronPDF para posicionarla y diseñarla en las páginas del PDF usando CSS.

¿Cuáles son los pasos para comenzar a agregar marcas de agua usando IronPDF en Java?

Para comenzar, descargue la biblioteca de IronPDF, configure su entorno Java y use los métodos de la API de la biblioteca para renderizar o cargar PDFs y aplicar marcas de agua personalizadas con sus configuraciones elegidas.

Darrius Serrant
Ingeniero de Software Full Stack (WebOps)

Darrius Serrant tiene una licenciatura en Ciencias de la Computación de la Universidad de Miami y trabaja como Ingeniero de Marketing WebOps Full Stack en Iron Software. Atraído por la programación desde joven, vio la computación como algo misterioso y accesible, convirtiéndolo en el ...

Leer más
¿Listo para empezar?
Versión: 2025.11 recién lanzado