//: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");
using applyWatermark 方法將文字蓋章到 PDF 文件的每個頁面。 該方法接受 HTML 字串,因此您可以使用任何 CSS 屬性樣式化水印:字體系列、大小、顏色、字母間距或文字陰影。 下面的範例將文件標記為紅色的"機密",這涵蓋了最常見的審核追蹤和存取控制方案。
//: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");
}
}
Java
applyWatermark 調用一次新增水印到所有頁面。 預設情況下,水印以 50% 的不透明度渲染,並居中於每個頁面。 要激活程式庫的所有功能,請在進行任何 PDF 操作之前配置您的IronPDF 授權金鑰。 傳遞給 applyWatermark 的 HTML 接受任何有效的 HTML 元素,因此您可以包含 <span> 或樣式化的 <p> 標籤以製作多行水印。
圖片水印通過包裝在 HTML 字串中的 applyWatermark 方法工作。 具有透明背景的 PNG 文件是徽標水印的理想選擇,因為在將圖片合成到 PDF 頁面時會保留透明性。 JPEG、GIF、SVG 和 BMP 格式也受支持。
//: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);'/>";
Java
圖片路徑必須在運行時從 JVM 的工作目錄可存取。對於伺服器部署,請使用絕對路徑或將圖片作為 Base64 資料 URI 嵌入在 src 屬性中,以避免路徑解析問題。
//: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");
}
}
重要: TextStamper 和 ImageStamper 類共享一個通用的 Stamper 基底類。 當您的應用程式動態生成水印時,請始終使用 stamper 方法,例如為每個生成的 PDF 嵌入唯一的交易 ID 或使用者名。
對於批量處理,例如為數百個 PDF 蓋上特定客戶的水印,在迴圈中載入和蓋章,每次迭代之前調用 PdfDocument。 IronPDF 獨立地處理每個文件,因此記憶體使用保持有限。 Java PDF 蓋章指南詳細介紹了 ImageStamper 和額外的蓋章選項,以及 IronPDF Java 文件提供完整的 API 參考。
TextStamper 與 applyWatermark 有何不同?
applyWatermark 方法為 HTML 字串提供快速、一致的多頁蓋章進行了優化。 TextStamper 和 ImageStamper 處理 applyWatermark 的情境:目標頁面子集,應用多個不同配置的獨特蓋章,或程式化地從資料庫或使用者輸入生成水印參數。 這兩種方法直接嵌入於 PDF 文件流中生產矢量品質的輸出,因此水印在再次保存、列印以及不支持可選內容層的 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.