This sample shows how to handle PDF-specific exceptions thrown by Docotic.Pdf library.

In addition to standard .NET exceptions (such as ArgumentNullException or ArgumentOutOfRangeException) Docotic.Pdf library can throw exceptions of PdfException type. These exceptions are specific to PDF format. So, they are thrown when you try to do something that is not allowed or invalid from PDF format point of view. This sample shows one of such situations: a try to draw some text with characters which are not supported by the current font.

C#
using System.Diagnostics;

namespace BitMiracle.Docotic.Pdf.Samples
{
    public static class ErrorHandling
    {
        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 = "ErrorHandling.pdf";

            using (PdfDocument pdf = new PdfDocument())
            {
                PdfPage page = pdf.Pages[0];

                try
                {
                    // tries to draw Russian string with built-in Helvetica font which doesn't
                    // define glyphs for Russian characters. So, we expect that a PdfException will be thrown.
                    page.Canvas.DrawString(10, 50, "Ïðèâåò, ìèð!");
                }
                catch (PdfException ex)
                {
                    page.Canvas.DrawString(10, 50, "Expected exception occurred:");
                    page.Canvas.DrawString(10, 65, ex.Message);
                }

                pdf.Save(pathToFile);
            }

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

Imports BitMiracle.Docotic.Pdf

Namespace BitMiracle.Docotic.Pdf.Samples
    Public NotInheritable Class ErrorHandling
        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 = "ErrorHandling.pdf"

            Using pdf As New PdfDocument()
                Dim page As PdfPage = pdf.Pages(0)

                Try
                    ' tries to draw Russian string with built-in Helvetica font which doesn't
                    ' define glyphs for Russian characters. So, we expect that a PdfException will be thrown.
                    page.Canvas.DrawString(10, 50, "Ïðèâåò, ìèð!")
                Catch ex As PdfException
                    page.Canvas.DrawString(10, 50, "Expected exception occurred:")
                    page.Canvas.DrawString(10, 65, ex.Message)
                End Try

                pdf.Save(pathToFile)
            End Using

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