Create Watermarks on PDFs in UWP

An app using PSPDFKit for Windows can draw content on top of a PDF using the OnWatermarkRequested event found in PSPDFKit.UI.PdfView. This event exposes a callback for internal watermark requests, allowing your code to supply a Watermark object containing the desired image.

Using OnWatermarkRequested

Whenever a page is rendered, you can use the OnWatermarkRequested event to render watermarks on the page.

To do this, add an event handler in which a Watermark is created, accepting a SoftwareBitmap as the image to render, and pass it back. At the end of your handler, the event’s deferral must also be completed:

PDFView.OnWatermarkRequested += async (controller, deferral) =>
{
    using (var image = await AssetLoader.GetWatermarkAsync())
    {
        controller.ViewWatermark = new Watermark(image)
        {
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center,
            SizeOnPage = new Size(200, 200)
        };
    }

    deferral.Complete();
};

PDF page with a watermark

Make sure the handler is as efficient as possible, as it might be invoked multiple times per page (depending on tile rendering).

Moreover, watermarks created through OnWatermarkRequested are displayed over the document, and they’re only visible to the client. When exporting this document, the watermark won’t be included.

Printing

Watermarks created via OnWatermarkRequested aren’t present when printing a document. To print with a watermark included, it must be set in similar fashion, but to the Watermark property of our PrintHelper:

var printHelper = await PrintHelper.CreatePrintHelperFromSourceAsync(documentSource, this, "PrintCanvas", "PrintWithoutUI");

using (var image = await AssetLoader.GetWatermarkAsync())
{
    printHelper.Watermark = new Watermark(image)
    {
        HorizontalAlignment = HorizontalAlignment.Center,
        VerticalAlignment = VerticalAlignment.Center,
        SizeOnPage = new Size(200, 200)
    };
}

await printHelper.ShowPrintUIAsync();

Our Printing guide has more information about the PrintHelper.