PDF generation with Visual Basic .NET

The example will show how you can use the COM interface of our PDF Maker with the .NET Framework for PDF generation. In the code example, we use VB.NET as the programming language. However, the example can be easily abstracted to C# or even ASP.NET web applications.

The use of the component in the example also shows the exemplary setting of PDF setting options using the PDF Setting functions of the component and the actual PDF conversion of the input file via the PDF conversion function convertToPDF.

The return code of the PDF engine is then output in a text box. Using the return code, the developer knows exactly when and if the PDF generation has been completed successfully, and can respond flexibly to this program-programmatically.

Execution errors due to different uses of .NET versions

The VB.NET example that can be downloaded here was created with the .NET Framework 3.5. If when executing the sample project a message appears that the original runtime environment is more current than the currently loaded one, you must select the newer .NET Framework version (e.g. 4.7.2) in the project configuration of our example for VB.NET. Then clean up the project before you recreate or compile it, and run it without errors. Also make sure that the target CPU x86 is selected under the project properties!

Source text extract of the source code example (VB.NET)

The code sample below is available for download at the bottom of the page.

  1. Public Class frmSevenPDFCOMClient
  2.  
  3.     Private Sub btnSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectFile.Click
  4.         Dim strDatei As String
  5.  
  6.         'Select a File that should be convert to PDF here
  7.         With OpenFileDialog1
  8.  
  9.             .FilterIndex = 1
  10.  
  11.             If .ShowDialog() = Windows.Forms.DialogResult.OK Then
  12.                 strDatei = .FileName
  13.                 txtFile.Text = strDatei
  14.             End If
  15.  
  16.         End With
  17.  
  18.     End Sub
  19.  
  20.     Private Sub btnConvertToPDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConvertToPDF.Click
  21.         Dim SevenPDFComObj As New SevenPDFComObj.SevenPDFObjConverter
  22.  
  23.         'COM DLL INVOKE CALL TO SevenPDFComObj.dll
  24.         'Note that you must register the ActiveX DLL first
  25.         'Use [regsvr32 "FULLPATH TO DLL FOLDER\SevenPDFComObj.dll"]
  26.         'Pleas Notice: You need administrative privileges for that!
  27.  
  28.         Try
  29.             txtReturnCode.Text = ""
  30.  
  31.             '******* INITIALIZATION *********
  32.  
  33.             SevenPDFComObj.Init()
  34.             'SevenPDFComObj.UnlockKey ("INSERT LICENSEKEY")
  35.  
  36.             '********************************
  37.  
  38.             '****** Customize some PDF Settings *******
  39.             'Notice: PDF encryption works only in registered version
  40.             '******************************************
  41.  
  42.             SevenPDFComObj.setExportNotes(0)
  43.             SevenPDFComObj.setExportNotesPages(0)
  44.             SevenPDFComObj.setExportBookmarks(0)
  45.  
  46.             'Set PDF Security Options
  47.             'SevenPDFComObj.setEncryptFile (1)
  48.             'SevenPDFComObj.setPermissionPassword ("test1232")
  49.             'SevenPDFComObj.setRestrictPermissions (1)
  50.             'SevenPDFComObj.setChanges (0)
  51.             'SevenPDFComObj.setPrinting (1)
  52.             'SevenPDFComObj.setEnableCopyingOfContent (0)
  53.             'SevenPDFComObj.setEnableTextAccessForAccessibilityTools (0)
  54.  
  55.             'Make the Conversion
  56.             txtReturnCode.Text = SevenPDFComObj.convertToPdf(txtFile.Text, txtPDF.Text, 0).ToString()
  57.  
  58.         Catch ex As Exception
  59.             MsgBox("Error occurs: " & ex.Message)
  60.         End Try
  61.  
  62.     End Sub
  63.  
  64.     Private Sub btnSelectPDF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectPDF.Click
  65.         Dim strDatei As String
  66.  
  67.         'Select a File that should be convert to PDF here
  68.         With SaveFileDialog1
  69.  
  70.             .FilterIndex = 1
  71.  
  72.             If .ShowDialog() = Windows.Forms.DialogResult.OK Then
  73.                 strDatei = .FileName
  74.                 txtPDF.Text = strDatei
  75.             End If
  76.  
  77.         End With
  78.     End Sub
  79. End Class

Downloads

Attachement Size
Download the code sample 129.23 KB
Top