Blend modes

Docotic.Pdf Library Help > Samples > Graphics > Blend modes

This sample shows how to customize a way in which semi-transparent objects will blend on a canvas.

You can use PdfCanvas.BlendMode property to customize how semi-transparent objects (text, images and so on) will blend on a canvas. There is a set of predefined blend modes, please take a look at PdfBlendMode enumeration.

CopyC#
using System.Diagnostics;
using System.Drawing;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class BlendModes
    {
        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.

            PdfDocument pdf = new PdfDocument();

            PdfCanvas canvas = pdf.Pages[0].Canvas;

            PointF location = new PointF(50, 80);
            SizeF size = new SizeF(50, 50);

            PdfBlendMode[] modes = new PdfBlendMode[] { PdfBlendMode.Hue, PdfBlendMode.Lighten, PdfBlendMode.Darken };
            for (int i = 0; i < modes.Length; ++i)
            {
                if (i != 0)
                    location.X = location.X + size.Width * 2;

                canvas.BlendMode = PdfBlendMode.Normal;

                canvas.Brush.Color = new PdfRgbColor(0, 0, 0);
                canvas.DrawString(location.X, location.Y - 20, "BlendMode: " + modes[i].ToString());

                canvas.Brush.Color = new PdfRgbColor(200, 140, 7);
                canvas.DrawRectangle(new RectangleF(location, size), 0, PdfDrawMode.Fill);

                canvas.BlendMode = modes[i];
                canvas.Brush.Color = new PdfRgbColor(125, 125, 255);
                RectangleF secondRect = new RectangleF(location.X + 15, location.Y + 15, size.Width, size.Height);
                canvas.DrawRectangle(secondRect, 0, PdfDrawMode.Fill);
            }

            string pathToFile = "BlendModes.pdf";
            pdf.Save(pathToFile);
            pdf.Dispose();

            Process.Start(pathToFile);
        }
    }
}
CopyVB.NET
Imports System.Diagnostics
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.IO

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class BlendModes
        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 pdf As New PdfDocument()
            Dim canvas As PdfCanvas = pdf.Pages(0).Canvas

            Dim location As New PointF(50, 80)
            Dim size As New SizeF(50, 50)

            Dim modes As PdfBlendMode() = New PdfBlendMode() {PdfBlendMode.Hue, PdfBlendMode.Lighten, PdfBlendMode.Darken}
            For i As Integer = 0 To modes.Length - 1
                If i <> 0 Then
                    location.X = location.X + size.Width * 2
                End If

                canvas.BlendMode = PdfBlendMode.Normal

                canvas.Brush.Color = New PdfRgbColor(0, 0, 0)
                canvas.DrawString(location.X, location.Y - 20, "BlendMode: " + modes(i).ToString())

                canvas.Brush.Color = New PdfRgbColor(200, 140, 7)
                canvas.DrawRectangle(New RectangleF(location, size), 0, PdfDrawMode.Fill)

                canvas.BlendMode = modes(i)
                canvas.Brush.Color = New PdfRgbColor(125, 125, 255)
                Dim secondRect As New RectangleF(location.X + 15, location.Y + 15, size.Width, size.Height)
                canvas.DrawRectangle(secondRect, 0, PdfDrawMode.Fill)
            Next

            Dim pathToFile As String = "BlendModes.pdf"
            pdf.Save(pathToFile)
            pdf.Dispose()

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