Docotic.Pdf Library Help > Samples > Layers > Add XObjects to layers

This samples shows how to add XObjects (watermarks) to layers in a PDF document.

PdfXObject classe has Layer property. All you need to do to associate an XObject with a layer is to assign the layer to the property.

To remove XObject from a layer you only need to set the XObject property to null.

C#
using System.Diagnostics;
using System.Drawing;
using System.IO;

using BitMiracle.Docotic.Pdf;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class AddXObjectsToLayers
    {
        public static void Main()
        {
            // NOTE: 
            // When used in trial mode, the library imposes some restrictions.
            // Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            // for more information.

            string pathToFile = "AddXObjectsToLayers.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                pdf.PageMode = PdfPageMode.UseOC;

                PdfLayer firstWatermarkLayer = pdf.CreateLayer("First XObject");
                PdfLayer secondWatermarkLayer = pdf.CreateLayer("Second XObject");

                PdfXObject firstObject = pdf.CreateXObject();
                firstObject.Canvas.DrawString("Text on the first XObject");
                firstObject.Layer = firstWatermarkLayer;

                PdfPage page = pdf.GetPage(0);
                page.Canvas.DrawXObject(firstObject, 0, 100);

                PdfXObject secondObject = pdf.CreateXObject();
                secondObject.Canvas.DrawString("Text on the second XObject");
                secondObject.Layer = secondWatermarkLayer;

                page.Canvas.DrawXObject(secondObject, 100, 200);

                pdf.Save(pathToFile);
            }

            Process.Start(pathToFile);
        }
    }
}
VB
Imports System.Diagnostics
Imports System.Drawing
Imports System.IO

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class AddXObjectsToLayers
        Private Sub New()
        End Sub
        Public Shared Sub Main()
            ' NOTE: 
            ' When used in trial mode, the library imposes some restrictions.
            ' Please visit http://bitmiracle.com/pdf-library/trial-restrictions.aspx
            ' for more information.

            Dim pathToFile As String = "AddXObjectsToLayers.pdf"

            Using pdf As New PdfDocument()
                pdf.PageMode = PdfPageMode.UseOC

                Dim firstWatermarkLayer As PdfLayer = pdf.CreateLayer("First XObject")
                Dim secondWatermarkLayer As PdfLayer = pdf.CreateLayer("Second XObject")

                Dim firstObject As PdfXObject = pdf.CreateXObject()
                firstObject.Canvas.DrawString("Text on the first XObject")
                firstObject.Layer = firstWatermarkLayer

                Dim page As PdfPage = pdf.GetPage(0)
                page.Canvas.DrawXObject(firstObject, 0, 100)

                Dim secondObject As PdfXObject = pdf.CreateXObject()
                secondObject.Canvas.DrawString("Text on the second XObject")
                secondObject.Layer = secondWatermarkLayer

                page.Canvas.DrawXObject(secondObject, 100, 200)

                pdf.Save(pathToFile)
            End Using

            Process.Start(pathToFile)
        End Sub
    End Class
End Namespace