//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/quickstart.javaPdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));pdf.applyWatermark("<h1 style='color:red;'>Confidential</h1>");pdf.saveAs("watermarked.pdf");
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/quickstart.java
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
pdf.applyWatermark("<h1 style='color:red;'>Confidential</h1>");
pdf.saveAs("watermarked.pdf");
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/text-watermark.javaimport 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 IronPDFLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load an existing PDF document from file PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf")); // HTML string defines the watermark appearance via CSS String watermarkHtml = "<h1 style='color:red;'>Confidential</h1>"; // Apply the watermark to every page pdf.applyWatermark(watermarkHtml); // Save the watermarked PDF to a new file pdf.saveAs("text_watermark.pdf"); }}
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/text-watermark.java
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 defines the watermark appearance via CSS
String watermarkHtml = "<h1 style='color:red;'>Confidential</h1>";
// Apply the watermark to every page
pdf.applyWatermark(watermarkHtml);
// Save the watermarked PDF to a new file
pdf.saveAs("text_watermark.pdf");
}
}
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/image-watermark.javaimport 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 IronPDFLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load an existing PDF document from file PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf")); // Reference the image file path relative to the runtime working directory String watermarkHtml = "<img src='logo.png' style='width:100px;'/>"; // Apply the image watermark to all pages pdf.applyWatermark(watermarkHtml); // Save the result pdf.saveAs("image_watermark.pdf"); }}
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/image-watermark.java
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"));
// Reference the image file path relative to the runtime working directory
String watermarkHtml = "<img src='logo.png' style='width:100px;'/>";
// Apply the image watermark to all pages
pdf.applyWatermark(watermarkHtml);
// Save the result
pdf.saveAs("image_watermark.pdf");
}
}
Java
在<img>标签中的CSS属性控制大小、位置和透明度:
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/image-watermark-advanced.java// Apply a 150px logo at 50% opacity, rotated 45 degrees counterclockwiseString advancedWatermarkHtml = "<img src='logo.png' style='width:150px; opacity:0.5; transform:rotate(-45deg);'/>";
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/image-watermark-advanced.java
// Apply a 150px logo at 50% opacity, rotated 45 degrees counterclockwise
String advancedWatermarkHtml =
"<img src='logo.png' style='width:150px; opacity:0.5; transform:rotate(-45deg);'/>";
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/watermark-opacity-alignment.javaimport 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 IronPDFLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load an existing PDF document from file PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf")); // Define the watermark HTML String watermarkHtml = "<h1 style='color:blue;'>Confidential</h1>"; // Apply at 30% opacity, anchored to the top-left corner of each page pdf.applyWatermark(watermarkHtml, 30, VerticalAlignment.TOP, HorizontalAlignment.LEFT); // Save the result pdf.saveAs("watermark_opacity_alignment.pdf"); }}
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/watermark-opacity-alignment.java
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"));
// Define the watermark HTML
String watermarkHtml = "<h1 style='color:blue;'>Confidential</h1>";
// Apply at 30% opacity, anchored to the top-left corner of each page
pdf.applyWatermark(watermarkHtml, 30, VerticalAlignment.TOP, HorizontalAlignment.LEFT);
// Save the result
pdf.saveAs("watermark_opacity_alignment.pdf");
}
}
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/text-stamper.javaimport java.io.IOException;import java.nio.file.Paths;import java.util.Arrays;import com.ironsoftware.ironpdf.License;import com.ironsoftware.ironpdf.PdfDocument;import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;import com.ironsoftware.ironpdf.stamp.TextStamper;import com.ironsoftware.ironpdf.stamp.VerticalAlignment;public class Main { public static void main(String[] args) throws IOException { // Set the license key for IronPDFLicense.setLicenseKey("IRONPDF-MYLICENSE-KEY-1EF01"); // Load the target PDF document PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf")); // Configure a TextStamper with custom font, rotation, and opacity TextStamper stamper = newTextStamper(); stamper.setText("DRAFT"); stamper.setFontSize(72); stamper.setFontColor("gray"); stamper.setOpacity(40); stamper.setRotation(45); stamper.setVerticalAlignment(VerticalAlignment.MIDDLE); stamper.setHorizontalAlignment(HorizontalAlignment.CENTER); // Apply the stamp only to pages 0 and 1 (zero-indexed) pdf.stamp(stamper, Arrays.asList(0, 1)); // Save the stamped PDF pdf.saveAs("text_stamped.pdf"); }}
//:path=/static-assets/pdf/content-code-examples/how-to/custom-watermark/text-stamper.java
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import com.ironsoftware.ironpdf.License;
import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.stamp.HorizontalAlignment;
import com.ironsoftware.ironpdf.stamp.TextStamper;
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 the target PDF document
PdfDocument pdf = PdfDocument.fromFile(Paths.get("sample.pdf"));
// Configure a TextStamper with custom font, rotation, and opacity
TextStamper stamper = new TextStamper();
stamper.setText("DRAFT");
stamper.setFontSize(72);
stamper.setFontColor("gray");
stamper.setOpacity(40);
stamper.setRotation(45);
stamper.setVerticalAlignment(VerticalAlignment.MIDDLE);
stamper.setHorizontalAlignment(HorizontalAlignment.CENTER);
// Apply the stamp only to pages 0 and 1 (zero-indexed)
pdf.stamp(stamper, Arrays.asList(0, 1));
// Save the stamped PDF
pdf.saveAs("text_stamped.pdf");
}
}
Is it possible to apply multiple watermarks on the same page using IronPDF?
Yes, IronPDF allows the application of multiple watermarks on the same page by using separate `applyWatermark` calls or utilizing the `TextStamper` and `ImageStamper`. This provides flexibility for adding distinct stamps or logos at different positions within a single document.
What are the steps for applying a watermark in bulk to multiple PDFs with IronPDF?
To apply watermarks in bulk, iterate over your list of PDF documents, applying the watermark using the desired method (e.g., `applyWatermark`, `TextStamper`) on each, and save the changes with `saveAs`. This process ensures efficient memory usage as IronPDF handles each document independently.