PDF stands for portable document format. It is a format that every operating system on virtually any computer can read. Its become the default standard for transferring and viewing files in an easy way. The problem is that Visual Basic has no built in VB6 PDF functions. Because of this it has been very hard in the past to create PDF files from VB. Hopefully by the end of this Visual Basic Tutorial, PDF creation will be an easy thing for you to understand and do first hand.
PDF is a pretty complex data format. Because, of this usually you have to buy a third party control from someone who's invested a large amount of time in navigating through the PDF format and written an easy way for you to access it. This tutorial explains how you can create a simple PDF file without a third party control. If you run into limitations down the road you might still end up having to purchase a control. However, you will then know exactly what to look for in the control and know how much you are willing to spend to get the functionality you need. Hopefully you won't even need to purchase one. If there is something you are trying to do, add a comment here or in the forum, and we will try to help you with it. Also there is a followup to this tutorial: Creating Advanced PDF files using Visual Basic. It goes a lot more in depth into things like adding pictures, headers, page numbers and bookmarks.
The first thing you will need to do to follow this tutorial is to download the mjwPDF class and the PDF fonts. This class allows you to easily create PDF files and is what we will be using in this Visual Basic Tutorial. PDF creation is a snap once you've downloaded the class. Now copy the class and the fonts folder to the area where you have your project saved.
If you created a default project (Project1.vbp) and didn't change the name of the form it creates (From1.frm) your project folder should look like this once you've copied the mjwPDF class and the Fonts folder.
If this seems confusing, just download this VB PDF tutorial source code. It has everything zipped into one location for you to see how to set it all up.
Now that the fonts folder and mjwPDF class are in the right spot we need to add the mjwPDF class to our project:
Great. Now add a button to your form. Double click on it and add the following code to it:
Private Sub Command1_Click() ' Create a simple PDF file using the mjwPDF class Dim objPDF As New mjwPDF ' Set the PDF title and filename objPDF.PDFTitle = "Test PDF Document" objPDF.PDFFileName = App.Path & "\test.pdf" ' We must tell the class where the PDF fonts are located objPDF.PDFLoadAfm = App.Path & "\Fonts" ' View the PDF file after we create it objPDF.PDFView = True ' Begin our PDF document objPDF.PDFBeginDoc ' Set the font name, size, and style objPDF.PDFSetFont FONT_ARIAL, 15, FONT_BOLD ' Set the text color objPDF.PDFSetTextColor = vbBlue ' Set the text we want to print objPDF.PDFTextOut _ "Hello, World! From mjwPDF (www.vb6.us)" ' End our PDF document (this will save it to the filename) objPDF.PDFEndDoc End Sub
Lets walk through this code so we know what it does. I've added comments to explain each section. First we create the mjwPDF class object. We set a title for the PDF file. This will show up if someone looks at the file properties for this PDF document. Next set where the document will be saved. We are just saving it as test.pdf in the same folder as our program is. Next we tell it where the fonts folder is located. This is why we had to copy that folder to our running directory. The class uses this information to add those fonts to the file when they are needed.
The next line (line 13) is completly optional. By specifying true, our PDF document will be opened once we finish writing to it. Obviously on many applications you simply want to save the document and not show it to the user. If this is the case set this property to false.
Starting at line 16 we get into the meat of this Visual Basic tutorial. PDF file creation is a simple step by step process. First we decide what font we want. What size it is and if we want any special characteristics (like bold or italic). Next we set the color we want our text to be. Lastly we call the PDFTextOut to actually write the text to the PDF file. NOTE: I am saying write the text to the PDF file, but really its simply stored in a buffer until we call PDFEndDoc. We do this next and that is when all of the buffer is actually saved to the filename you specified in line 7. Also, since we set the PDFView property to true the PDF file is then opened for us automatically and we can see a simple file with our text added to it.
Download the VB PDF tutorial source code and have fun! Please leave comments below as to how well this worked for you. Also, I have written another tutorial: Visual Basic and Advanced PDF Documents. It goes a lot more in depth into how to add pictures, shapes, headers, hyperlinks, multiple pages, and page numbers.
Note: The original source code for this class (before modifications) can be found here.