---
title: "Add watermark to PDFs and images in C# .NET | Nutrient .NET SDK"
canonical_url: "https://www.nutrient.io/guides/dotnet/editor/watermark/"
md_url: "https://www.nutrient.io/guides/dotnet/editor/watermark.md"
last_updated: "2026-05-15T19:10:04.984Z"
description: "Learn how to add text or image watermarks to PDF documents and images in C# using Nutrient .NET SDK. Protect and brand your digital content programmatically."
---

# 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](#adding-a-watermark-to-a-pdf) and [images](#adding-a-watermark-to-an-image).

## 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.

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](#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`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~LoadFromFile.html) method.

3. Load the watermark image with the [`CreateGdPictureImageFromFile`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~CreateGdPictureImageFromFile.html) method.

4. Add the watermark image as an image resource with the [`AddImageFromGdPictureImage`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~AddImageFromGdPictureImage.html) method without any encoding or decoding process.

5. Create a new OCG with the [`NewOCG`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~NewOCG.html) method.

6. Set the measurement unit used to specify dimensions. The [`SetMeasurementUnit`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetMeasurementUnit.html) 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`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SelectPage.html) method.

8. Set the opacity of the watermark image with the [`SetFillAlpha`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetFillAlpha.html) 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`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawImage.html) 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`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetImageOptional.html) method.

11. Optional: Set the OCG visibility for different states with any of the following methods:
    - [`SetOCGExportState`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetOCGExportState.html) — Specifies if the OCG is visible when exporting the file.
    - [`SetOCGLockedState`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetOCGLockedState.html) — Specifies if the OCG visibility can be toggled by the user.
    - [`SetOCGPrintState`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetOCGPrintState.html) — Specifies if the OCG is visible in a printed output.
    - [`SetOCGViewState`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetOCGViewState.html) — Specifies if the OCG is visible onscreen.

12. Save the PDF with the watermark to a file with the [`SaveToFile`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SaveToFile.html) method.

### C#

```csharp

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);

```

### VB.NET

```vb

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

- [`AddImageFromGdPictureImage`]

- [`CreateGdPictureImageFromFile`]

- [`DrawImage`]

- [`LoadFromFile`]

- [`GetPageHeight`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageHeight.html)

- [`GetPageWidth`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageWidth.html)

- [`NewOCG`]

- [`ReleaseGdPictureImage`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~ReleaseGdPictureImage.html)

- [`SaveToFile`]

- [`SelectPage`]

- [`SetFillAlpha`]

- [`SetImageOptional`]

- [`SetMeasurementUnit`]

- [`SetOCGViewState`]

### 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:

### C#

```csharp

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);

```

### VB.NET

```vb

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

- [`AddImageFromGdPictureImage`]

- [`CreateGdPictureImageFromFile`]

- [`DrawImage`]

- [`GetPageCount`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageCount.html)

- [`GetPageHeight`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageHeight.html)

- [`GetPageWidth`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageWidth.html)

- [`LoadFromFile`]

- [`NewOCG`]

- [`ReleaseGdPictureImage`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~ReleaseGdPictureImage.html)

- [`SaveToFile`]

- [`SelectPage`]

- [`SetFillAlpha`]

- [`SetImageOptional`]

- [`SetMeasurementUnit`]

- [`SetOCGViewState`]

### 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`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~SetTransparencyColor.html) 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`](https://www.nutrient.io/guides/dotnet/editor/manipulation/colors/#color-object) object or using the [`ARGB`](https://www.nutrient.io/guides/dotnet/editor/manipulation/colors/#argb-method) method.

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

### C#

```csharp

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);

```

### VB.NET

```vb

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

- [`AddImageFromGdPictureImage`]

- [`CreateGdPictureImageFromFile`]

- [`DrawImage`]

- [`GetPageHeight`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageHeight.html)

- [`GetPageWidth`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageWidth.html)

- [`LoadFromFile`]

- [`NewOCG`]

- [`ReleaseGdPictureImage`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~ReleaseGdPictureImage.html)

- [`SaveToFile`]

- [`SelectPage`]

- [`SetFillAlpha`]

- [`SetImageOptional`]

- [`SetMeasurementUnit`]

- [`SetOCGViewState`]

- [`SetTransparencyColor`]

### 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`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~BeginOCGMarkedContent.html) and [`EndOCGMarkedContent`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~EndOCGMarkedContent.html) 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.

- [`DrawArc`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawArc.html)

- Barcodes:
  - [`DrawBarcode1D`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawBarcode1D.html)
  - [`DrawBarcodeAztec`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawBarcodeAztec.html)
  - [`DrawBarcodeDataMatrix`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawBarcodeDataMatrix.html)
  - [`DrawBarcodeMicroMicroQrCode`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawBarcodeMicroMicroQrCode.html)
  - [`DrawBarcodePDF417`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawBarcodePDF417.html)
  - [`DrawBarcodeQrCode`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawBarcodeQrCode.html)

- [`DrawCircle`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawCircle.html)

- [`DrawEllipse`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawEllipse.html)

- [`DrawImage`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawImage.html)

- [`DrawLine`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawLine.html)

- [`DrawPage`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawPage.html)

- [`DrawPie`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawPie.html)

- [`DrawRectangle`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawRectangle.html)

- [`DrawRotatedText`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawRotatedText.html)

- [`DrawRoundedRectangle`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawRoundedRectangle.html)

- [`DrawText`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawText.html)

- [`DrawTextBox`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawTextBox.html)

- [`DrawWrappedText`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawWrappedText.html)

- [`DrawXObjectFrom`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~DrawXObjectForm.html)

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

### C#

```csharp

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");

```

### VB.NET

```vb

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

- [`AddStandardFont`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~AddStandardFont.html)

- [`BeginOCGMarkedContent`]

- [`DrawRectangle`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~DrawRectangle.html)

- [`DrawTextBox`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~DrawTextBox.html)

- [`EndOCGMarkedContent`]

- [`GetPageCount`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageCount.html)

- [`GetPageHeight`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageHeight.html)

- [`GetPageWidth`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~GetPageWidth.html)

- [`LoadFromFile`]

- [`NewOCG`]

- [`SaveToFile`]

- [`SelectPage`]

- [`SetFillAlpha`]

- [`SetFillColor`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetFillColor.html)

- [`SetMeasurementUnit`]

- [`SetOrigin`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetOrigin.html)

- [`SetTextSize`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPicturePDF~SetTextSize.html)

#### Related topics

- [Load a file](/guides/dotnet/load-a-file.md)

- [Save a file](/guides/dotnet/save-a-file.md)

## 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`](https://www.nutrient.io/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~SetTransparencyColor.html) method. It requires specifying the image ID and the color you want to make transparent by either declaring a [`Color`](https://www.nutrient.io/guides/dotnet/editor/manipulation/colors/#color-object) object or using the [`ARGB`](https://www.nutrient.io/guides/dotnet/editor/manipulation/colors/#argb-method) method.

5. Insert the watermark image into your file.

### C#

```csharp

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);

```

### VB.NET

```vb

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

- [`CreateGdPictureImageFromFile`]

- [`DrawGdPictureImageTransparency`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~DrawGdPictureImageTransparency.html)

- [`GetHeight`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~GetHeight.html)

- [`GetWidth`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~GetWidth.html)

- [`ReleaseGdPictureImage`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~ReleaseGdPictureImage.html)

- [`SaveAsJPEG`](/api/gdpicture/GdPicture.NET.14~GdPicture14.GdPictureImaging~SaveAsJPEG.html)

- [`SetTransparencyColor`]

#### Related topics

- [Load a file](/guides/dotnet/load-a-file.md)

- [Save a file](/guides/dotnet/save-a-file.md)
---

## Related pages

- [Add an image to another image using C#](/guides/dotnet/editor/add-image-to-image.md)
- [Add an image to a PDF using C#](/guides/dotnet/editor/add-image-to-pdf.md)
- [Add a page to a PDF or a TIFF image in C# .NET](/guides/dotnet/editor/add-page.md)
- [Add text to an image using C#](/guides/dotnet/editor/add-text-to-image.md)
- [Modify EXIF metadata using C#](/guides/dotnet/editor/metadata-exif.md)
- [PDF and image editor in C#.NET](/guides/dotnet/editor.md)
- [Attach a file to a PDF in C#](/guides/dotnet/editor/attach-a-file.md)
- [Add text to PDFs in C#](/guides/dotnet/editor/add-text-to-pdf.md)
- [Merge PDFs in C#](/guides/dotnet/editor/merge-or-combine.md)
- [Edit IPTC metadata using C#](/guides/dotnet/editor/metadata-iptc.md)
- [Read and edit PDF XMP metadata using C#](/guides/dotnet/editor/metadata-xmp.md)
- [Read and edit PDF page label metadata using C#](/guides/dotnet/editor/metadata-pdf-page-label.md)
- [Split PDF pages into multiple pages in C#](/guides/dotnet/editor/split.md)

