'-------------------------------------------------------------------------------- ' Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. '-------------------------------------------------------------------------------- Imports System Imports System.Runtime.InteropServices Module Module1 Public Delegate Function PDFSecureCallback(ByVal mode As Integer, ByVal msg As String, ByVal user_data As IntPtr) As Int32 ' The following lines import the PDFSecure functions from pdfsecure.dll (via PInvoke). _ Public Function PDFSecureInit _ (ByVal user_name As String, ByVal company As String, ByVal license_key As String) As IntPtr End Function _ Public Function PDFSecureRun _ (ByVal command_str As String, ByVal funct As PDFSecureCallback, ByVal user_data As IntPtr) As Int32 End Function ' 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 PDFSECURE_ERROR As Integer = 1 ' Show the error message Dim PDFSECURE_MSG As Integer = 2 ' Report the message Dim PDFSECURE_GETPASS As Integer = 3 ' Get the password Dim PDFSECURE_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 = PDFSECURE_ERROR Then Console.WriteLine("Error: {0}", msg) ElseIf mode = PDFSECURE_MSG Then Console.Write("{0}", msg) ElseIf mode = PDFSECURE_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 PDFSecureCallback mycbk = AddressOf MyCallback PDFSecureInit(username, company, lic_key) ' Variable used to switch between different code samples. Dim example As Integer = 1 If example = 1 Then ' The simplest PDFSecure program: Pass an explicit command-line string. ' ' This example will secure all PDF documents in 'TestFiles' using ' 128-bit encryption. The password required to open processed files is ' 'secret'. ' ' - The ‘-o’ parameter is used to specify the output folder/file. ' - The ‘-s’ parameter specifies that the output document should be encrypted ' using 128-bit RC4 encryption. ' - The ‘-u’ parameter specifies the password required to access the secured ' document. ' ' For a detailed explanation of all commands, please refer to PDFSecure ' User Manual. Dim str As String = "-o TestOutput/test1 -s 128 -u secret TestFiles" PDFSecureRun(str, mycbk, New IntPtr(0)) ElseIf example = 2 Then ' An example of how to use PDFSecure SDK to implement a command-line ' application for processing of PDF documents. Dim s As String = "" Dim arg As String For Each arg In args s += arg s += " " Next PDFSecureRun(s, mycbk, New IntPtr(0)) ElseIf example = 3 Then ' A more comlicated example that illustrates how to dynamically build a command ' string based on user security settings... ' Please note that this sample does not cover all possible options offered by ' PDFSecure. For a detailed explanation of all commands, please refer to PDFSecure ' User Manual. Dim linearized_output As Boolean = True Dim aes_encryption As Boolean = False Dim output_folder As String = "TestOutput/test1" Dim open_password As String = "mypass" Dim title As String = "My Title" Dim author As String = "Joe Doe" Dim subject As String = "My Subject" Dim keywords As String = "key1 key2 key2" Dim creator As String = "PDFTron PDFNet" Dim producer As String = "PDFSecure" ' Permissions Dim printing As Boolean = False Dim high_rez_printing As Boolean = False Dim doc_modifications As Boolean = True ' Changing the document Dim content_extraction As Boolean = True Dim commenting As Boolean = True Dim forms_editing As Boolean = True Dim accessibility As Boolean = True Dim document_assembly 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 aes_encryption Then s += "-s AES " ' Use 128-bit AES (Advanced Encryption Standard); Supported in PDF 1.6 (Acrobat 7) and above. Else s += "-s 128 " ' Use 128-bit RC4 encryption. Supported in PDF 1.4 (Acrobat 5) and above. ' To remove security use: ' s += "-s R"; etc. End If If Not open_password = "" Then s += "--userpass " + open_password + " " End If ' Set the permissions. s += "-e a -d """ If printing = False Then s += "p" End If If high_rez_printing = False Then s += "h" End If If doc_modifications = False Then s += "m" End If If content_extraction = False Then s += "c" End If If commenting = False Then s += "o" End If If forms_editing = False Then s += "f" End If If accessibility = False Then s += "x" End If If document_assembly = False Then s += "s" End If s += """ " If linearized_output Then s += "-l " End If ' Set document information. If Not title = "" Then s += "--title """ + title + """ " End If If Not author = "" Then s += "--author """ + author + """ " End If If Not subject = "" Then s += "--subject """ + subject + """ " End If If Not keywords = "" Then s += "--keywords """ + keywords + """ " End If If Not creator = "" Then s += "--creator """ + creator + """ " End If If Not producer = "" Then s += "--producer """ + producer + """ " End If ' specify input files and folders... s += "TestFiles/black.pdf " s += "TestFiles/blue.pdf " ' ... '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. PDFSecureRun(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 End If Console.WriteLine("Done.") End Sub End Module