'-------------------------------------------------------------------------------- ' Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. '-------------------------------------------------------------------------------- Imports System Imports System.Runtime.InteropServices Module Module1 Public Delegate Function PDF2ImageCallback(ByVal mode As Integer, ByVal msg As String, ByVal user_data As IntPtr) As Int32 ' The following lines import the PDF2Image functions from PDF2Image.dll (via PInvoke). _ Public Function PDF2ImageInit _ (ByVal user_name As String, ByVal company As String, ByVal license_key As String, ByVal resource_path As String) As Int32 End Function _ Public Function PDF2ImageRun _ (ByVal command_str As String, ByVal funct As PDF2ImageCallback, ByVal user_data As IntPtr) As Int32 End Function ' Return codes for PDF2ImageRun function. Dim PDF2IMAGE_OK As Integer = 0 ' No errors Dim PDF2IMAGE_ERR As Integer = 1 ' Unspecified error Dim PDF2IMAGE_ERR_BADKEY As Integer = 2 ' Bad license key Dim PDF2IMAGE_ERR_DIRCREATE As Integer = 3 ' Failed to create the output file/directory Dim PDF2IMAGE_ERR_READINGPDF As Integer = 4 ' Failed to read input document Dim PDF2IMAGE_ERR_PASSWORD As Integer = 5 ' The password required to open PDF is incorrect Dim PDF2IMAGE_ERR_CONVERT As Integer = 6 ' A conversion error ' You can modify the following lines with your registration information. Dim username As String = "John Doe" Dim company As String = "My Company" Dim lic_key As String = "my license key" Dim PDF2IMAGE_ERROR As Integer = 1 ' Show the error message Dim PDF2IMAGE_MSG As Integer = 2 ' Report the message Dim PDF2IMAGE_GETPASS As Integer = 3 ' Get the password Dim PDF2IMAGE_OUT_FILENAME As Integer = 4 ' Get the saved filenames ' A custom callback that can be used to report errors and other messages. Public Function MyCallback(ByVal mode As Integer, ByVal msg As String, ByVal user_data As IntPtr) As Int32 If mode = PDF2IMAGE_ERROR Then Console.WriteLine("Error: {0}", msg) ElseIf mode = PDF2IMAGE_MSG Then Console.Write("{0}", msg) ElseIf mode = PDF2IMAGE_OUT_FILENAME Then If user_data.ToInt32 <> 0 Then 'add the next saved filename to the given ArrayList Dim gch As GCHandle = GCHandle.op_Explicit(user_data) Dim list As ArrayList = CType(gch.Target, ArrayList) list.Add(msg) End If End If Return 0 End Function 'MyCallback Sub Main(ByVal args() As String) Dim mycbk As PDF2ImageCallback mycbk = AddressOf MyCallback PDF2ImageInit(username, company, lic_key, Nothing) ' Variable used to switch between different code samples. Dim example As Integer = 1 If example = 1 Then ' The simplest PDF2Image program: Pass an explicit command-line string. ' ' This example converts 'tiger.pdf' to PNG image 'tiger.png' ' To convert all PDF documents in 'TestFiles' folder simply delete 'tiger.pdf' ' from the command string. ' ' For a detailed explanation of all commands, please refer to PDF2Image SDK User ' Manual. Also, PDF2Image Command-Line application is a great tool to experiment ' with the available options. Dim str As String = "-f png -o TestOutput/test1 TestFiles/tiger.pdf" PDF2ImageRun(str, mycbk, New IntPtr(0)) ElseIf example = 2 Then ' An example of how to use PDF2Image SDK to implement a command-line application ' for converting PDF pages to images. Dim s As String = "" Dim arg As String For Each arg In args s += arg s += " " Next PDF2ImageRun(s, mycbk, New IntPtr(0)) ElseIf example = 3 Then ' An example of how to dynamically build a command string based on ' user settings... ' ' Please note that this sample does not cover all possible options offered by ' PDF2Image. For a detailed explanation of all commands, please refer to PDF2Image ' SDK User Manual. Dim output_folder As String = "TestOutput/test1" Dim open_password As String = "secret" Dim output_format As String = "png" ' or png, png8, gif, tif, bmp, raw, etc. ' Controls for the output name of rasterized/converted images... Dim filename_prefix As String = "" Dim filename_digits As Integer = 4 Dim multipage As Boolean = False ' used if the output format is 'tiff'. Dim grayscale As Boolean = False ' the output image resolution (DPI := Dots Pet Inch). Dim dpi As Integer = 96 ' dimensions of the output image in pixels '/ Used if dpi is 0. Dim hres As Integer = 500 Dim vres As Integer = 500 ' compression_quality is used to control lossy compression ' algorithms (e.g. jpeg) Dim compression_quality As Integer = 60 Dim rotate As Integer = 0 ' rotates the page 90/180/270 degrees Dim antialias As Boolean = True Dim printmode As Boolean = True ' -------------------------------------------------- ' Given the above settings build a command string. Dim s As String = "" If Not output_folder = "" Then s += "-o " + output_folder + " " End If If Not open_password = "" Then s += "-p " + open_password + " " End If If Not output_format = "" Then s += "-f " + output_format + " " End If If Not filename_prefix = "" Then s += "--prefix " + filename_prefix + " " End If If filename_digits > 0 Then s += "--digits " + filename_digits.ToString + " " End If If multipage Then s += "--multipage " End If If grayscale Then s += "--gray " End If If dpi > 0 Then s += "--dpi " + dpi.ToString + " " Else ' use explicit image resolution s += "--hres " + hres.ToString + " --vres " + vres.ToString + " " End If If compression_quality > 0 Then s += "-q " + compression_quality.ToString + " " End If If Not antialias Then s += "--nosmooth " End If If printmode Then s += "--printmode " End If If rotate = 90 Then s += "--rotate 90 " End If If rotate = 180 Then s += "--rotate 180 " End If If rotate = 270 Then s += "--rotate 270 " End If ' specify input PDF files and folders... s += "TestFiles/tiger.pdf " s += "TestFiles/tai962.pdf " ' s += "c:/my_pdf_folder "; etc ... 'An ArrayList to store the paths of saved files Dim saved_files As New ArrayList 'Get a handle for the ArrayList so that it 'can be used in the MyCallback function Dim gch As GCHandle = GCHandle.Alloc(saved_files) ' Execute the command string. PDF2ImageRun(s, mycbk, GCHandle.op_Explicit(gch)) gch.Free() 'print the saved file(s) Console.WriteLine("The Saved Files Are:") For i As Integer = 0 To saved_files.Count - 1 Console.WriteLine(saved_files(i)) Next Console.WriteLine("Done.") End If End Sub End Module