Link Search Menu Expand Document

PDF To Image Rendering In Background Thread - VB.NET

PDF Renderer SDK sample in VB.NET demonstrating ‘PDF To Image Rendering In Background Thread’

Imports System.IO
Imports System.Threading
Imports Bytescout.PDFRenderer

Class Program

    Private Shared _doneEvent As New ManualResetEvent(False) ' synchronization event
    Private Shared _counter As Integer ' thread counter

    Friend Shared Sub Main(args As String())

        ' Get all PDF files in current directory
        Dim pdfFiles As String() = Directory.GetFiles(".", "*.pdf")
        _counter = pdfFiles.Length

        ' Render PDF files in separate threads
        For Each pdfFile As String In pdfFiles
            Dim backgroundThread As New Thread(AddressOf BackgroundThreadProc)
            Console.WriteLine(pdfFile & " - conversion started.")

        ' Wait until threads finished

        Console.WriteLine("Press any key to continue...")
    End Sub

    ' Rendering thread function
    Private Shared Sub BackgroundThreadProc(data As Object)

        Dim fileName As String = DirectCast(data, String)

            ' Create renderer
            Using renderer As New RasterRenderer()

                ' Setup rendering
                Dim renderingOptions As New RenderingOptions()
                renderingOptions.JPEGQuality = 90

                Dim renderingResolution As Single = 300

                ' Render document pages
                For i As Integer = 0 To renderer.GetPageCount() - 1
                    renderer.Save(fileName & ".page" & i & ".jpg", RasterImageFormat.JPEG, i, renderingResolution, renderingOptions)
            End Using

            Console.WriteLine(fileName & " - successfully converted.")
        Catch exception As Exception
        End Try

        If Interlocked.Decrement(_counter) = 0 Then
            ' Set event if all threads finished
        End If
    End Sub
End Class

Download Source Code (.zip)

Return to the previous page Explore PDF Renderer SDK