Sharp PDF Usage in VB6

Most people are looking on how to create PDF documents through their VB applications, but don’t want to spend money to buy Adobe Acrobat SDK or other third party components. If you don’t want to invest money, there is an open source solution for you, SharpPDF. SharpPDF is a very cool C# library based on .NET Framework 1.1 and created 100% compatible PDF document. More information about SharpPDF library can be found on sharppdf.sourceforge.net.

Visual Basic developers can’t use this library straight. If you register the SharpPDF and try to create the pdfDocument method you’ll get an error. So how can you take advantage of sharpPDF? A choice I can think of is to change the code of sharpPDF based on your needs. If you don’t want to mess up with the code of sharpPDF you can simply create a wrapper library with VB.NET, register the assembly and reference the wrapper through you VB6 project.

To register the assembly use the following command:
regasm /tlb: mywrapper.tlb /codebase mywrapper.dll

and from your VB6 project add a refence to mywrapper.tlb

Sample Code of wrapper created with VB.NET:

Imports sharpPDF

Public Class clsSharpPDFWrapper

Private m_sTitle As String

Private m_sAuthor As String

Private m_sContent As String

Public WriteOnly Property Title() As String

Set(ByVal Value As String)

m_sTitle = Value

End Set

End Property

Public WriteOnly Property Author() As String

Set(ByVal Value As String)

m_sAuthor = Value

End Set

End Property

Public WriteOnly Property Content() As String

Set(ByVal Value As String)

m_sContent = Value

End Set

End Property

Public Function CreatePDFDocument()

Dim oPDF As New sharpPDF.pdfDocument(m_sTitle, m_sAuthor)

Dim oPDFPage As sharpPDF.pdfPage =
oPDF.addPage(sharpPDF.Enumerators.predefinedPageSize.csA4Page)

oPDFPage.addText(m_sContent, 200, 450,

oPDF.getFontReference(Enumerators.predefinedFont.csHelvetica), 20,
pdfColor.Black)

oPDF.createPDF(“c:MyPDFDoc.pdf”)

oPDFPage = Nothing

oPDF = Nothing

End Function

End Class

Sample Code for calling the wrapper through your VB6 project:

Dim o As New clsSharpPDFWrapper

o.Title = “My First PDF Document”

o.Author = “Thomas Kaloyani”

o.Content = “Hello World!”

o.createPDFDocument

Happy coding!

Leave a Reply