Add Watermarks to PDFs and Images in C#

To add a watermark to a background, you need an image of the watermark and your source file. Watermarks can be added to both PDFs and images.

Adding a Watermark to a PDF

It’s possible to add a watermark to a PDF file using PDF layers. This functionality is called Optical Content Groups (OCG), and it was introduced in PDF version 1.5. OCGs enable grouping images or annotations together and controlling their visibility based on viewing conditions (whether a document is viewed onscreen or printed). For example, you can make a layer invisible when viewing a PDF onscreen, but make it visible in the printed output.

Information

If the background of your watermark image isn’t transparent, use the SetTransparencyColor method. For more information on how to make your background transparent, go to the Making a Watermark Image Background Transparent section.

To add a watermark image to a specified PDF page, follow the steps outlined below.

  1. Create a GdPicturePDF object and a GdPictureImaging object.

  2. Load the PDF file with the LoadFromFile method.

  3. Load the watermark image with the CreateGdPictureImageFromFile method.

  4. Add the watermark image as an image resource with the AddImageFromGdPictureImage method without any encoding or decoding process.

  5. Create a new OCG with the NewOCG method.

  6. Set the measurement unit used to specify dimensions. The SetMeasurementUnit method takes the PdfMeasurementUnit enumeration as an argument. It allows the following values:

    • PdfMeasurementUnitCentimeter

    • PdfMeasurementUnitMillimeter

    • PdfMeasurementUnitInch

    • PdfMeasurementUnitPoint

    • PdfMeasurementUnitUndefined

  7. Select the PDF page where you want to add the watermark with the SelectPage method.

  8. Set the opacity of the watermark image with the SetFillAlpha method. It requires only one parameter, which defines the opacity level (0 makes it fully visible, and 255 makes it fully transparent).

  9. Add the watermark image to the selected page with the DrawImage method. This method requires the following parameters:

    • ImageResName — The image resource name.

    • DstX — The horizontal distance between the left sides of the PDF and the drawn image.

    • DstY — The vertical distance between the left sides of the PDF and the drawn image.

    • Width — The width of the drawn image resource.

    • Height — The height of the drawn image resource.

  10. Add the drawn watermark image to the OCG with the SetImageOptional method.

  11. Optional: Set the OCG visibility for different states with any of the following methods:

  12. Save the PDF with the watermark to a file with the SaveToFile method.

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the PDF file.
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Load the watermark image.
int watermarkID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\watermark.png");
// Add the watermark image as an image resource.
string watermarkResource = gdpicturePDF.AddImageFromGdPictureImage(watermarkID, false, false);
// Create a new OCG.
var ocgID = gdpicturePDF.NewOCG("Watermark Layer");
// Set the measurement unit to centimeters.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
float pdfWidth = gdpicturePDF.GetPageWidth();
float pdfHeight = gdpicturePDF.GetPageHeight();
// Specify the watermark width and height and the horizontal and vertical margins.
float wWidth = pdfWidth * 5 / 10;
float wHeight = pdfHeight * 3 / 10;
float hMargin = (pdfWidth - wWidth) / 2;
float vMargin = (pdfHeight - wHeight) / 2;
// Select the PDF page where the watermark is added.
gdpicturePDF.SelectPage(2);
// Set the opacity of newly drawn elements.
gdpicturePDF.SetFillAlpha(100);
// Draw the watermark image onto the selected PDF page.
gdpicturePDF.DrawImage(watermarkResource, hMargin, vMargin, wWidth, wHeight);
// Add the watermark image to the OCG.
gdpicturePDF.SetImageOptional(watermarkResource, ocgID);
// Make the OCG visible onscreen.
gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn);
// Save the PDF file with the watermark to a file.
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
gdpictureImaging.ReleaseGdPictureImage(watermarkID);
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Load the PDF file.
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Load the watermark image.
    Dim watermarkID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\watermark.png")
    ' Add the watermark image as an image resource.
    Dim watermarkResource As String = gdpicturePDF.AddImageFromGdPictureImage(watermarkID, False, False)
    ' Create a new OCG.
    Dim ocgID = gdpicturePDF.NewOCG("Watermark Layer")
    ' Set the measurement unit to centimeters.
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    Dim pdfWidth As Single = gdpicturePDF.GetPageWidth()
    Dim pdfHeight As Single = gdpicturePDF.GetPageHeight()
    ' Specify the watermark width and height and the horizontal and vertical margins.
    Dim wWidth = pdfWidth * 5 / 10
    Dim wHeight = pdfHeight * 3 / 10
    Dim hMargin = (pdfWidth - wWidth) / 2
    Dim vMargin = (pdfHeight - wHeight) / 2
    ' Select the PDF page where the watermark is added.
    gdpicturePDF.SelectPage(2)
    ' Set the opacity of newly drawn elements.
    gdpicturePDF.SetFillAlpha(100)
    ' Draw the watermark image onto the selected PDF page.
    gdpicturePDF.DrawImage(watermarkResource, hMargin, vMargin, wWidth, wHeight)
    ' Add the watermark image to the OCG.
    gdpicturePDF.SetImageOptional(watermarkResource, ocgID)
    ' Make the OCG visible onscreen.
    gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn)
    ' Save the PDF file with the watermark to a file.
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
    gdpictureImaging.ReleaseGdPictureImage(watermarkID)
End Using
End Using
Used Methods

Adding a Watermark to All PDF Pages

To add a watermark image to all PDF pages, enclose the SelectPage, SetFillAlpha, and DrawImage methods in a loop to run for all PDF pages.

Use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the PDF file.
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Load the watermark image.
int watermarkID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\watermark.png");
// Add the watermark image as an image resource.
string watermarkResource = gdpicturePDF.AddImageFromGdPictureImage(watermarkID, false, false);
// Create a new OCG.
var ocgID = gdpicturePDF.NewOCG("Watermark Layer");
// Set the measurement unit to centimeters.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
float pdfWidth = gdpicturePDF.GetPageWidth();
float pdfHeight = gdpicturePDF.GetPageHeight();
// Specify the watermark width and height and the horizontal and vertical margins.
float wWidth = pdfWidth * 5 / 10;
float wHeight = pdfHeight * 3 / 10;
float hMargin = (pdfWidth - wWidth) / 2;
float vMargin = (pdfHeight - wHeight) / 2;
for (int i= 1; i <= gdpicturePDF.GetPageCount(); i++)
{
    // Select the PDF page where the watermark is added.
    gdpicturePDF.SelectPage(i);
    // Set the opacity of newly drawn elements.
    gdpicturePDF.SetFillAlpha(100);
    // Draw the watermark image onto the selected PDF page.
    gdpicturePDF.DrawImage(watermarkResource, hMargin, vMargin, wWidth, wHeight);
}
// Add the watermark image to the OCG.
gdpicturePDF.SetImageOptional(watermarkResource, ocgID);
// Make the OCG visible onscreen.
gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn);
// Save the PDF file with the watermark to a file.
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
gdpictureImaging.ReleaseGdPictureImage(watermarkID);
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Load the PDF file.
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Load the watermark image.
    Dim watermarkID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\watermark.png")
    ' Add the watermark image as an image resource.
    Dim watermarkResource As String = gdpicturePDF.AddImageFromGdPictureImage(watermarkID, False, False)
    ' Create a new OCG.
    Dim ocgID = gdpicturePDF.NewOCG("Watermark Layer")
    ' Set measurement units to centimeters.
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    Dim pdfWidth As Single = gdpicturePDF.GetPageWidth()
    Dim pdfHeight As Single = gdpicturePDF.GetPageHeight()
    ' Specify the watermark width and height and the horizontal and vertical margins.
    Dim wWidth = pdfWidth * 5 / 10
    Dim wHeight = pdfHeight * 3 / 10
    Dim hMargin = (pdfWidth - wWidth) / 2
    Dim vMargin = (pdfHeight - wHeight) / 2
    For i As Integer = 1 To gdpicturePDF.GetPageCount()
        ' Select the PDF page where the watermark is added.
        gdpicturePDF.SelectPage(i)
        ' Set the opacity of newly drawn elements.
        gdpicturePDF.SetFillAlpha(100)
        ' Draw the watermark image onto the selected PDF page.
        gdpicturePDF.DrawImage(watermarkResource, hMargin, vMargin, wWidth, wHeight)
    Next
    ' Add the watermark image to the OCG.
    gdpicturePDF.SetImageOptional(watermarkResource, ocgID)
    ' Make the OCG visible onscreen.
    gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn)
    ' Save the PDF file with the watermark to a file.
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
    gdpictureImaging.ReleaseGdPictureImage(watermarkID)
End Using
End Using
Used Methods

Making a Watermark Image Background Transparent

If the image you want to use as a watermark doesn’t have a transparent background, use the SetTransparencyColor method before adding the watermark image as an image resource. It requires specifying both the image ID and the color you want to make transparent. The latter is done by either declaring a Color object or using the ARGB method.

To make an image background transparent and later add it to the specified PDF page as a watermark, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the PDF file.
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Load the watermark image from a file.
int watermarkID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\watermark.png");
// Make the white color in the watermark image transparent.
gdpictureImaging.SetTransparencyColor(watermarkID, gdpictureImaging.ARGB(255, 255, 255, 255));
// Load the watermark image as an image resource.
var imageName = gdpicturePDF.AddImageFromGdPictureImage(watermarkID, false, false);
// Create a new OCG.
var ocgID = gdpicturePDF.NewOCG("Watermark Layer");
// Set the measurement unit to centimeters.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
float pdfWidth = gdpicturePDF.GetPageWidth();
float pdfHeight = gdpicturePDF.GetPageHeight();
// Specify the watermark width and height and the horizontal and vertical margins.
float wWidth = pdfWidth * 5 / 10;
float wHeight = pdfHeight * 3 / 10;
float hMargin = (pdfWidth - wWidth) / 2;
float vMargin = (pdfHeight - wHeight) / 2;
// Select the PDF page where the watermark is added.
gdpicturePDF.SelectPage(2);
// Set the opacity of newly drawn elements.
gdpicturePDF.SetFillAlpha(100);
// Draw the watermark image onto the selected PDF page.
gdpicturePDF.DrawImage(imageName, hMargin, vMargin, wWidth, wHeight);
// Add the watermark image to the OCG.
gdpicturePDF.SetImageOptional(imageName, ocgID);
// Make the OCG visible onscreen.
gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn);
// Save the PDF file with the watermark to a file.
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
gdpictureImaging.ReleaseGdPictureImage(watermarkID);
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    ' Load the PDF file.
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Load the watermark image from file.
    Dim watermarkID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\watermark.png")
    ' Make the white color in the watermark image transparent.
    gdpictureImaging.SetTransparencyColor(watermarkID, gdpictureImaging.ARGB(255, 255, 255, 255))
    ' Load the watermark image as an image resource.
    Dim imageName = gdpicturePDF.AddImageFromGdPictureImage(watermarkID, False, False)
    ' Create a new OCG.
    Dim ocgID = gdpicturePDF.NewOCG("Watermark Layer")
    ' Set measurement units to centimeters.
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    Dim pdfWidth As Single = gdpicturePDF.GetPageWidth()
    Dim pdfHeight As Single = gdpicturePDF.GetPageHeight()
    ' Specify the watermark width and height and the horizontal and vertical margins.
    Dim wWidth = pdfWidth * 5 / 10
    Dim wHeight = pdfHeight * 3 / 10
    Dim hMargin = (pdfWidth - wWidth) / 2
    Dim vMargin = (pdfHeight - wHeight) / 2
    ' Select the PDF page where the watermark is added.
    gdpicturePDF.SelectPage(2)
    ' Set the opacity of newly drawn elements.
    gdpicturePDF.SetFillAlpha(100)
    ' Draw the watermark image onto the selected PDF page.
    gdpicturePDF.DrawImage(imageName, hMargin, vMargin, wWidth, wHeight)
    ' Add the watermark image to the OCG.
    gdpicturePDF.SetImageOptional(imageName, ocgID)
    ' Make the OCG visible onscreen.
    gdpicturePDF.SetOCGViewState(ocgID, PdfOcgState.StateOn)
    ' Save the PDF file with the watermark to a file.
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
    gdpictureImaging.ReleaseGdPictureImage(watermarkID)
End Using
End Using
Used Methods

Adding Multiple Watermark Elements to a PDF

It’s possible to add different kinds of elements as a watermark to a PDF. To add multiple elements, use the BeginOCGMarkedContent and EndOCGMarkedContent methods. These methods only require the OCG ID, to which all elements drawn between the BeginOCGMarkedContent and EndOCGMarkedContent methods will be added. The methods below are used for drawing elements.

To add text enclosed in a rectangle as a watermark to all PDF pages, use the following code:

using GdPicturePDF gdpicturePDF = new GdPicturePDF();
// Load the PDF file.
gdpicturePDF.LoadFromFile(@"C:\temp\source.pdf");
// Create a new OCG.
var ocgID = gdpicturePDF.NewOCG("Watermark Layer");
// Set the origin of the coordinate system to the top-left corner.
gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft);
// Set the measurement unit to centimeters.
gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter);
// Specify the font type.
string fontName = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontCourierBold);
float pdfWidth = gdpicturePDF.GetPageWidth();
float pdfHeight = gdpicturePDF.GetPageHeight();
// Specify the watermark area width and height and the horizontal and vertical margins.
float wWidth = pdfWidth * 5 / 10;
float wHeight = pdfHeight * 3 / 10;
float hMargin = (pdfWidth - wWidth) / 2;
float vMargin = (pdfHeight - wHeight) / 2;
// Start marking drawn elements to be added to the OCG.
gdpicturePDF.BeginOCGMarkedContent(ocgID);
for (int i = 1; i <= gdpicturePDF.GetPageCount(); i++)
{
    // Select the PDF page where the watermark is added.
    gdpicturePDF.SelectPage(i);
    // Set the opacity of the rectangle element.
    gdpicturePDF.SetFillAlpha(50);
    // Set the fill color to dark blue.
    gdpicturePDF.SetFillColor(0, 0, 139);
    // Draw a rectangle.
    gdpicturePDF.DrawRectangle(hMargin, vMargin, wWidth, wHeight, true, false);
    // Set the opacity of the text box element.
    gdpicturePDF.SetFillAlpha(100);
    // Set the text size to 50 points.
    gdpicturePDF.SetTextSize(50);
    // Set the fill color to black.
    gdpicturePDF.SetFillColor(0, 0, 0);
    // Draw the watermark image onto the selected PDF page.
    gdpicturePDF.DrawTextBox(fontName, hMargin, vMargin,
        hMargin + wWidth, vMargin + wHeight, TextAlignment.TextAlignmentCenter,
        TextAlignment.TextAlignmentCenter, "DRAFT");
}
// Stop marking drawn elements to be added to the OCG.
gdpicturePDF.EndOCGMarkedContent();
// Save the PDF file with the watermark to a file.
gdpicturePDF.SaveToFile(@"C:\temp\output.pdf");
Using gdpicturePDF As GdPicturePDF = New GdPicturePDF()
    ' Load the PDF file.
    gdpicturePDF.LoadFromFile("C:\temp\source.pdf")
    ' Create a new OCG.
    Dim ocgID = gdpicturePDF.NewOCG("Watermark Layer")
    ' Set the origin of the coordinate system to the top-left corner.
    gdpicturePDF.SetOrigin(PdfOrigin.PdfOriginTopLeft)
    ' Set the measurement unit to centimeters.
    gdpicturePDF.SetMeasurementUnit(PdfMeasurementUnit.PdfMeasurementUnitCentimeter)
    ' Specify the font type.
    Dim fontName As String = gdpicturePDF.AddStandardFont(PdfStandardFont.PdfStandardFontCourierBold)
    Dim pdfWidth As Single = gdpicturePDF.GetPageWidth()
    Dim pdfHeight As Single = gdpicturePDF.GetPageHeight()
    ' Specify the watermark area width and height and the horizontal and vertical margins.
    Dim wWidth = pdfWidth * 5 / 10
    Dim wHeight = pdfHeight * 3 / 10
    Dim hMargin = (pdfWidth - wWidth) / 2
    Dim vMargin = (pdfHeight - wHeight) / 2
    ' Start marking drawn elements to be added to the OCG.
    gdpicturePDF.BeginOCGMarkedContent(ocgID)
    For i As Integer = 1 To gdpicturePDF.GetPageCount()
        ' Select the PDF page where the watermark is added.
        gdpicturePDF.SelectPage(i)
        ' Set the opacity of the rectangle element.
        gdpicturePDF.SetFillAlpha(50)
        ' Set the fill color to dark blue.
        gdpicturePDF.SetFillColor(0, 0, 139)
        ' Draw a rectangle.
        gdpicturePDF.DrawRectangle(hMargin, vMargin, wWidth, wHeight, True, False)
        ' Set the opacity for the text box element.
        gdpicturePDF.SetFillAlpha(100)
        ' Set the text size to 50 points.
        gdpicturePDF.SetTextSize(50)
        ' Set the fill color to black.
        gdpicturePDF.SetFillColor(0, 0, 0)
        ' Draw the watermark image onto the selected PDF page.
        gdpicturePDF.DrawTextBox(fontName, hMargin, vMargin,
            hMargin + wWidth, vMargin + wHeight, TextAlignment.TextAlignmentCenter,
            TextAlignment.TextAlignmentCenter, "DRAFT")
    Next
    ' Stop marking drawn elements to be added to the OCG.
    gdpicturePDF.EndOCGMarkedContent()
    ' Save the PDF file with the watermark to a file.
    gdpicturePDF.SaveToFile("C:\temp\output.pdf")
End Using
Used Methods

Related Topics

Adding a Watermark to an Image

To add a watermark to an image, follow the steps outlined below.

  1. Create a GdPictureImaging object.

  2. Load the source file.

  3. Load the watermark image.

  4. Optional: Make the background of the watermark image transparent by using the SetTransparencyColor method. It requires specifying the image ID and the color you want to make transparent by either declaring a Color object or using the ARGB method.

  5. Insert the watermark image into your file.

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\source.jpg");
int imageWatermarkID = gdpictureImaging.CreateGdPictureImageFromFile(@"C:\temp\watermark.jpg");
// Set the side margins of the watermark image to 500 px.
// Set the top and bottom margins to 100 px.
int vertMargin = 100;
int horMargin = 500;
// Set the width and height of the watermark image to the source
// image dimensions, subtracted by the margins.
int wWidth = gdpictureImaging.GetWidth(imageId) - 2 * vertMargin;
int wHeight = gdpictureImaging.GetHeight(imageId) - 2 * horMargin;
gdpictureImaging.SetTransparencyColor(imageWatermarkID,
    gdpictureImaging.ARGB(255, 255, 255, 255));
// Add the watermark image to the source image.
gdpictureImaging.DrawGdPictureImageTransparency(imageWatermarkID, imageId, 200,
    vertMargin, horMargin, wWidth, wHeight, InterpolationMode.Default);
gdpictureImaging.SaveAsJPEG(imageId, @"C:\temp\output.jpg");
gdpictureImaging.ReleaseGdPictureImage(imageWatermarkID);
gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging()
    Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\source.jpg")
    Dim imageWatermarkID As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:\temp\watermark.jpg")
    ' Set the side margins of the watermark image to 500 px.
    ' Set the top and bottom margins to 100 px.
    Dim vertMargin = 100
    Dim horMargin = 500
    ' Set the width and height of the watermark image to the source
    ' image dimensions subtracted by the margins.
    Dim wWidth As Integer = gdpictureImaging.GetWidth(imageId) - 2 * vertMargin
    Dim wHeight As Integer = gdpictureImaging.GetHeight(imageId) - 2 * horMargin
    gdpictureImaging.SetTransparencyColor(imageWatermarkID,
        gdpictureImaging.ARGB(255, 255, 255, 255))
    ' Add the watermark image to the source image.
    gdpictureImaging.DrawGdPictureImageTransparency(imageWatermarkID, imageId, 200,
        vertMargin, horMargin, wWidth, wHeight, InterpolationMode.[Default])
    gdpictureImaging.SaveAsJPEG(imageId, "C:\temp\output.jpg")
    gdpictureImaging.ReleaseGdPictureImage(imageWatermarkID)
    gdpictureImaging.ReleaseGdPictureImage(imageId)
End Using
Used Methods

Related Topics