Transparency

Docotic.Pdf Library Help > Samples > Graphics > Transparency

This sample shows how to draw on a canvas using semi-transparent colors.

Use PdfCanvas.Pen.Opacity and/or PdfCanvas.Brush.Opacity properties to make current pen or brush color semi-transparent. Opacity value ranges from 0 (fully transparent) to 100 (fully opaque).

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

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

            RectangleF rect = new RectangleF(50, 50, 50, 50);

            canvas.Brush.Opacity = 100;
            canvas.Pen.Opacity = 100;
            canvas.Brush.Color = new PdfRgbColor(200, 140, 7);
            canvas.DrawRectangle(rect, 0, PdfDrawMode.Fill);

            canvas.Brush.Opacity = 40;
            canvas.Brush.Color = new PdfRgbColor(125, 125, 255);
            rect.Location = new PointF(rect.X + 15, rect.Y + 15);
            canvas.DrawRectangle(rect, 0, PdfDrawMode.Fill);

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

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

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class Transparency
        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 rect As New RectangleF(50, 50, 50, 50)

            canvas.Brush.Opacity = 100
            canvas.Pen.Opacity = 100
            canvas.Brush.Color = New PdfRgbColor(200, 140, 7)
            canvas.DrawRectangle(rect, 0, PdfDrawMode.Fill)

            canvas.Brush.Opacity = 40
            canvas.Brush.Color = New PdfRgbColor(125, 125, 255)
            rect.Location = New PointF(rect.X + 15, rect.Y + 15)
            canvas.DrawRectangle(rect, 0, PdfDrawMode.Fill)

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

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