Edit in GitHubLog an issue

PDF Watermark

A watermark in a document is usually semi-transparent or faded text, a logo, or a pattern placed in the background or foreground of the page for security, authenticity, and branding. PDF Watermark API is a cloud based solution to apply watermark on specified pages of PDF document using a source watermark PDF. The first page of the source watermark PDF will be added as a watermark in the input PDF document. If a page range is not specified, the watermark will be applied on all pages of the source document.

PDF Watermark

Input Documents : Required

Supported format is PDF (application/pdf). Input PDF with version 1.6 and above is supported.

Input Document

A PDF document to which a watermark will be applied.

Watermark Document

A PDF document whose first page will be used as a watermark for the input document. The output generated will retain the content along with the watermark from the first page.

Note: If the watermark document is a scanned PDF, placing the watermark in the foreground may make the content of the input PDF unreadable. Place watermarks from scanned PDF files in the background by setting appearOnForeground to false.

Watermark Parameters

Page ranges (pageRanges)

Specifies the number of pages on which the watermark will be applied. Page numbers are indexed from 1 to N. If a page range is not specified, the watermark will be applied on all pages of the source document. The page ranges are specified as an array of objects whose length cannot exceed beyond 20. Each object has the following properties:

  • Start Page (start) : The first page number of the range. Default value is 1.
  • End Page (end) : The last page number of the range. Default value is the last page of the document.

Appearance (appearance)

  • Foreground (appearOnForeground) : Specifies the placement of the watermark on the page. It can appear in the foreground or background. The default value is true, placing the watermark in the foreground.
  • Opacity (opacity) : Specifies the opacity of the watermark, represented as an integer percentage value ranging from 0 to 100. The default value is 100.

REST API

See our public API Reference for PDF Watermark API.

Apply Watermark on Input PDF

The sample below performs watermark operation applying watermark in foreground on all pages of a given PDF.

Please refer to the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://github.com/adobe/pdfservices-java-sdk-samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.pdfwatermark.PDFWatermark
package com.adobe.pdfservices.operation.samples.pdfwatermark;
public class PDFWatermark {
// Initialize the logger
private static final Logger LOGGER = LoggerFactory.getLogger(PDFWatermark.class);
public static void main(String[] args) {
try (
InputStream sourceFileInputStream = Files.newInputStream(new File("src/main/resources/pdfWatermarkInput.pdf").toPath());
InputStream watermarkFileInputStream = Files.newInputStream(new File("src/main/resources/watermark.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(System.getenv("PDF_SERVICES_CLIENT_ID"), System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset inputDocumentAsset = pdfServices.upload(sourceFileInputStream, PDFServicesMediaType.PDF.getMediaType());
Asset watermarkDocumentAsset = pdfServices.upload(watermarkFileInputStream, PDFServicesMediaType.PDF.getMediaType());
// Creates a new job instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates a new job instance
PDFWatermarkJob pdfWatermarkJob = new PDFWatermarkJob(inputDocumentAsset, watermarkDocumentAsset);
// Submit the job and gets the job result
String location = pdfServices.submit(pdfWatermarkJob);
PDFServicesResponse<PDFWatermarkResult> pdfServicesResponse = pdfServices.getJobResult(location, PDFWatermarkResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/pdfWatermarkWithOptionsOutput.pdf").toPath());
LOGGER.info("Saving asset at output/pdfWatermarkWithOptionsOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
}

Apply Watermark on specified pages

The sample below performs watermark operation applying watermark in foreground on specified pages of a given PDF.

Please refer to the API usage guide to understand how to use our APIs.

Copied to your clipboard
// Get the samples from https://github.com/adobe/pdfservices-java-sdk-samples
// Run the sample:
// mvn -f pom.xml exec:java -Dexec.mainClass=com.adobe.pdfservices.operation.samples.pdfwatermark.PDFWatermarkWithOptions
package com.adobe.pdfservices.operation.samples.pdfwatermark;
public class PDFWatermarkWithOptions {
// Initialize the logger
private static final Logger LOGGER = LoggerFactory.getLogger(PDFWatermarkWithOptions.class);
public static void main(String[] args) {
try (
InputStream sourceFileInputStream = Files.newInputStream(new File("src/main/resources/pdfWatermarkInput.pdf").toPath());
InputStream watermarkFileInputStream = Files.newInputStream(new File("src/main/resources/watermark.pdf").toPath())) {
// Initial setup, create credentials instance
Credentials credentials = new ServicePrincipalCredentials(System.getenv("PDF_SERVICES_CLIENT_ID"), System.getenv("PDF_SERVICES_CLIENT_SECRET"));
// Creates a PDF Services instance
PDFServices pdfServices = new PDFServices(credentials);
// Creates an asset(s) from source file(s) and upload
Asset inputDocumentAsset = pdfServices.upload(sourceFileInputStream, PDFServicesMediaType.PDF.getMediaType());
Asset watermarkDocumentAsset = pdfServices.upload(watermarkFileInputStream, PDFServicesMediaType.PDF.getMediaType());
// Creates a new job instance
PDFServices pdfServices = new PDFServices(credentials);
// Watermark pages of the document (as specified by PageRanges).
PageRanges pageRangeForPDFWatermark = new PageRanges();
pageRangeForPDFWatermark.addRange(2, 5);
pageRangeForPDFWatermark.addRange(8, 10);
// Creates PDF Watermark appearance option
WatermarkAppearance watermarkAppearance = new WatermarkAppearance();
watermarkAppearance.setOpacity(50);
// Create parameters for the job
PDFWatermarkParams pdfWatermarkParams = PDFWatermarkParams.pdfWatermarkParamsBuilder()
.withPageRanges(pageRangeForPDFWatermark)
.withWatermarkAppearance(watermarkAppearance)
.build();
// Creates a new job instance
PDFWatermarkJob pdfWatermarkJob = new PDFWatermarkJob(inputDocumentAsset, watermarkDocumentAsset)
.setParams(pdfWatermarkParams);
// Submit the job and gets the job result
String location = pdfServices.submit(pdfWatermarkJob);
PDFServicesResponse<PDFWatermarkResult> pdfServicesResponse = pdfServices.getJobResult(location, PDFWatermarkResult.class);
// Get content from the resulting asset(s)
Asset resultAsset = pdfServicesResponse.getResult().getAsset();
StreamAsset streamAsset = pdfServices.getContent(resultAsset);
// Creates an output stream and copy stream asset's content to it
Files.createDirectories(Paths.get("output/"));
OutputStream outputStream = Files.newOutputStream(new File("output/pdfWatermarkWithOptionsOutput.pdf").toPath());
LOGGER.info("Saving asset at output/pdfWatermarkWithOptionsOutput.pdf");
IOUtils.copy(streamAsset.getInputStream(), outputStream);
outputStream.close();
} catch (ServiceApiException | IOException | SDKException | ServiceUsageException ex) {
LOGGER.error("Exception encountered while executing operation", ex);
}
}
}
  • Privacy
  • Terms of Use
  • Do not sell or share my personal information
  • AdChoices
Copyright © 2024 Adobe. All rights reserved.