Add a watermark to a PDF document
Add a watermark to a PDF document using a source watermark PDF. Specify the pages to which the watermark is to be applied. This is interoperable with the Acrobat Watermark tool. Watermarks are typically added to indicate the status, classification, or branding of a document.
See our public API Reference and quickly try our APIs using the Postman collections
REST API
Node js
.NET
Java
Python
Copied to your clipboardcurl --location --request POST 'https://pdf-services.adobe.io/operation/addwatermark' \--header 'x-api-key: {{Placeholder for client_id}}' \--header 'Content-Type: application/json' \--header 'Authorization: Bearer {{Placeholder for token}}' \--data-raw '{"inputDocumentAssetID": "urn:aaid:AS:UE1:54cbf87f-d7f5-4918-8e4b-9f68","watermarkDocumentAssetID": "urn:aaid:AS:UE1:54cbf87f-d7f5-4918-8e4b-9f1878678e68"}'
Copied to your clipboard// Get the samples from https://github.com/adobe/pdfservices-node-sdk-samples// Run the sample:// node src/pdfwatermark/pdf-watermark.jsconst {ServicePrincipalCredentials,PDFServices,MimeType,PDFWatermarkJob,PDFWatermarkResult,SDKError,ServiceUsageError,ServiceApiError,} = require("@adobe/pdfservices-node-sdk");const fs = require("fs");(async () => {let sourceFileReadStream;let watermarkFileReadStream;try {// Initial setup, create credentials instanceconst credentials = new ServicePrincipalCredentials({clientId: process.env.PDF_SERVICES_CLIENT_ID,clientSecret: process.env.PDF_SERVICES_CLIENT_SECRET});// Creates a PDF Services instanceconst pdfServices = new PDFServices({credentials});// Creates an asset(s) from source file(s) and uploadsourceFileReadStream = fs.createReadStream("resources/watermarkPDFInput.pdf");watermarkFileReadStream = fs.createReadStream("resources/watermark.pdf");const [inputAsset, watermarkAsset] = await pdfServices.uploadAssets({streamAssets: [{readStream: sourceFileReadStream,mimeType: MimeType.PDF}, {readStream: waterMarkReadStream,mimeType: MimeType.PDF}]});// Creates a new job instanceconst job = new PDFWatermarkJob({inputAsset: inputAsset,watermarkAsset: watermarkAsset});// Submit the job and get the job resultconst pollingURL = await pdfServices.submit({job});const pdfServicesResponse = await pdfServices.getJobResult({pollingURL,resultType: PDFWatermarkResult});// Get content from the resulting asset(s)const resultAsset = pdfServicesResponse.result.asset;const streamAsset = await pdfServices.getContent({asset: resultAsset});// Creates a write stream and copy stream asset's content to itconst outputFilePath = "./pdfWatermarkOutput.pdf";console.log(`Saving asset at ${outputFilePath}`);const writeStream = fs.createWriteStream(outputFilePath);streamAsset.readStream.pipe(writeStream);} catch (err) {if (err instanceof SDKError || err instanceof ServiceUsageError || err instanceof ServiceApiError) {console.log("Exception encountered while executing operation", err);} else {console.log("Exception encountered while executing operation", err);}} finally {sourceFileReadStream?.destroy();watermarkFileReadStream?.destroy();}})();
Copied to your clipboard// Get the samples from https://github.com/adobe/PDFServices.NET.SDK.Samples// Run the sample:// cd PDFWatermark/// dotnet run PDFWatermark.csprojnamespace PDFWatermark{class Program{// Initialize the logger.private static readonly ILog log = LogManager.GetLogger(typeof(Program));static void Main(){//Configure the loggingConfigureLogging();try{// Initial setup, create credentials instanceICredentials credentials = new ServicePrincipalCredentials(Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_ID"),Environment.GetEnvironmentVariable("PDF_SERVICES_CLIENT_SECRET"));// Creates a PDF Services instancePDFServices pdfServices = new PDFServices(credentials);// Creates an asset(s) from source file(s) and uploadStream sourceFileInputStream = File.OpenRead(@"pdfWatermarkInput.pdf");IAsset inputDocumentAsset = pdfServices.Upload(sourceFileInputStream, PDFServicesMediaType.PDF.GetMIMETypeValue());// Creates a watermark asset from source file(s) and uploadStream watermarkFileInputStream = File.OpenRead(@"watermark.pdf");IAsset watermarkDocumentAsset = pdfServices.Upload(watermarkFileInputStream, PDFServicesMediaType.PDF.GetMIMETypeValue());// Submits the job and gets the job resultPDFWatermarkJob pdfWatermarkJob = new PDFWatermarkJob(inputDocumentAsset, watermarkDocumentAsset);String location = pdfServices.Submit(pdfWatermarkJob);// Get content from the resulting asset(s)PDFServicesResponse<PDFWatermarkResult> pdfServicesResponse =pdfServices.GetJobResult<PDFWatermarkResult>(location, typeof(PDFWatermarkResult));// Creating output streams and copying stream asset's content to itIAsset resultAsset = pdfServicesResponse.Result.Asset;StreamAsset streamAsset = pdfServices.GetContent(resultAsset);// Creating output streams and copying stream asset's content to itString outputFilePath = "/output/pdfWatermarkWithOptionsOutput.pdf";new FileInfo(Directory.GetCurrentDirectory() + outputFilePath).Directory.Create();Stream outputStream = File.OpenWrite(Directory.GetCurrentDirectory() + outputFilePath);streamAsset.Stream.CopyTo(outputStream);outputStream.Close();}catch (ServiceUsageException ex){log.Error("Exception encountered while executing operation", ex);}catch (ServiceApiException ex){log.Error("Exception encountered while executing operation", ex);}catch (SDKException ex){log.Error("Exception encountered while executing operation", ex);}catch (IOException ex){log.Error("Exception encountered while executing operation", ex);}catch (Exception ex){log.Error("Exception encountered while executing operation", ex);}}static void ConfigureLogging(){ILoggerRepository logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));}}}
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.PDFWatermarkpackage com.adobe.pdfservices.operation.samples.pdfwatermark;public class PDFWatermark {// Initialize the loggerprivate 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 instanceCredentials credentials = new ServicePrincipalCredentials(System.getenv("PDF_SERVICES_CLIENT_ID"), System.getenv("PDF_SERVICES_CLIENT_SECRET"));// Creates a PDF Services instancePDFServices pdfServices = new PDFServices(credentials);// Creates an asset(s) from source file(s) and uploadAsset inputDocumentAsset = pdfServices.upload(sourceFileInputStream, PDFServicesMediaType.PDF.getMediaType());Asset watermarkDocumentAsset = pdfServices.upload(watermarkFileInputStream, PDFServicesMediaType.PDF.getMediaType());// Creates a new job instancePDFServices pdfServices = new PDFServices(credentials);// Creates a new job instancePDFWatermarkJob pdfWatermarkJob = new PDFWatermarkJob(inputDocumentAsset, watermarkDocumentAsset);// Submit the job and gets the job resultString 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 itFiles.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);}}}
Copied to your clipboard# Get the samples from https://github.com/adobe/pdfservices-python-sdk-samples# Run the sample:# python src/pdfwatermark/watermark_pdf.py# Initialize the loggerlogging.basicConfig(level=logging.INFO)class PDFWatermark:def __init__(self):try:pdf_file = open("src/resources/watermarkPDFInput.pdf", 'rb')source_file_input_stream = pdf_file.read()pdf_file.close()pdf_file = open("src/resources/watermark.pdf", 'rb')watermark_file_input_stream = pdf_file.read()pdf_file.close()# Initial setup, create credentials instancecredentials = ServicePrincipalCredentials(client_id=os.getenv('PDF_SERVICES_CLIENT_ID'),client_secret=os.getenv('PDF_SERVICES_CLIENT_SECRET'))# Creates a PDF Services instancepdf_services = PDFServices(credentials=credentials)# Creates an asset(s) from source file(s) and uploadinput_asset = pdf_services.upload(input_stream=source_file_input_stream, mime_type=PDFServicesMediaType.PDF)watermark_asset = pdf_services.upload(input_stream=watermark_file_input_stream, mime_type=PDFServicesMediaType.PDF)# Creates a new job instancepdf_watermark_job = PDFWatermarkJob(input_asset=input_asset, watermark_asset=watermark_asset)# Submit the job and gets the job resultlocation = pdf_services.submit(pdf_watermark_job)pdf_services_response = pdf_services.get_job_result(location, PDFWatermarkResult)# Get content from the resulting asset(s)pdf_watermark_result: CloudAsset = pdf_services_response.get_result().get_asset()stream_asset: StreamAsset = pdf_services.get_content(pdf_watermark_result)# Creates an output stream and copy stream asset's content to itoutput_file_path = 'output/pdfWatermark.pdf'with open(output_file_path, "wb") as file:file.write(stream_asset.get_input_stream())except (ServiceApiException, ServiceUsageException, SdkException) as e:logging.exception(f'Exception encountered while executing operation: {e}')if __name__ == "__main__":PDFWatermark()
