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:
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.
Comments
Create a PDF document with the page in landscape orientation.
Hi, can anyone please help me.
I have read the tutorial and have managed to create a PDF document and insert a JPEG picture in to the document.
This is largely due to the very good tutorial which has been posted.
I have been given a task to create a PDF document and insert numerous JPEG's. The problem is that all the imported JPEGs must be in landscape orientation. I need to change the orientation of the PDF sheetr from portrait to landscape. I have tried to change the orientation property value within the mjwPDF class but this has no effect and the JPEG continues to be in portrait form.
Can anyone shed any light on this issue, on how to change the page orientation using the mjwPDF class.
Thanks a lot.
David Curran.
Can I create a PDF File from a template
Hi
Hope anyone can help me. I wonder if I can create a PDF File using this component, but based on a template, so I just can print some text and preserve de rest of the text from the template.
thanks in advance.
Lenin
Excellent post.I want to
Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work. great ,thanks
Good
This is a very goo example.
Thanks a lot...
I must salute the efforts you have put into this website... This article is very helpful...
thanks for the tutorials. it
thanks for the tutorials. it helps. its additional info that i could teach to my students
Watch Battery
Watch Accessories Watch Batteries Watch Battery
mens sport watches
sport watches mens sport watches sport watch wholesale clocks wholesale clock wholesale wall clock
Excellent
Kode-kode yang bagus, terima kasih atas tutorialnya. Sangat membantu sekali.
convert other fonts
how to convert other windows standard fonts to your *.afm format ?
you have any tool for this conversion ?
You are my Mentor Dr.
I have been following your VB tutorial and your explanation is so wonderful. It has never be explained to me like this before. I hope be very good in it with your teachings.
Thanks alot Doc.
Godwin, Nigeria
Thank you
This is very good.
And rather than complain or ask someone else to help sort out the more minor issues - I did it myself.
Great work, fellah!
Sceadugengan...
good , But ...
that is good module
but i modefied some thing on it i made it not open adobe reader after generating the file .
thank you
terima kasih.....
line break in pdf
how to introduce line breaks in the pdf that is created using this code?
database
How do I make my database report to be displayed in pdf format, please help me.
Thanks for your anticipated support,
Vwede
Search capability in pdf file from VB
Hi,
After converting text to pdf file from VB, I need to open pdf file with search option. This gives an error "There was an error opening the document. bad parameter.
Thanks,
Archana
Adding text line by line
This porgram is really good! unfortunately, when i add additional text on the second line and so forth it did not write the value of the . PDFTetOut!
I will create textboxes and pass the value to the PDFTextOut each line but it is not working! I really need help.. Thanks
Reg Use of this class
Hi,
I want to use this class in my VB application to generate PDF reports.
Please let me know if i can use it or not. I will be very thankful to you.
Waiting for ur response.
Thanks,
Abhishek
PDF Draq
Nice Class, it works great.
Having trouble with something. I can write any text with out any trouble but if I Draw some lines and then I start again with text. This last one would not be display in the PDF no error, no massage. It´s like if I haven´t sent any pdftextout.
suggestions?
Figure it out
The problem was with de objpdf.pdfdrawrectangle, if I draw the rectangle with .pdfdrawlinehor and .pdfdrawlinever problem solved
thank anyway.
view PDF as text
i have a question: how to read/preview/convert pdf as text file like some application preview pdf file?
And Format PDF - PDF-A
It's amazing this code...
This file is create in format PDF, of course.
How I do to create in Format PDF-A?
Thanks
Silvio
how to get the path from C:\"my folder name"
hi...i quite impress with your coding..so far i already tested it and it work properly!
i want to apply your code in my program but the problem is i already has word document and once user click button..it will automatic generate the pdf file from that word file.
Can you help and teach me on how to make it work??
thank you for ur co-operation..
Regards,
siti
PDF Document
Its fine, Really a nice one. But it doesn't show document in more than one line.
objPDF.PDFTextOut "My Second PDF File" objPDF.PDFTextOut "My Second PDF File"
I have written it two times still it doesn't work
how to solve?
hei there is an error on
hei there is an error on line 24 they say method or data member not found what should i do
Print form to pdf
It is great tool ,
I did it successfully,
My Question is:
I want to print the form to pdf.
Thank you
Database
Hi There,
What a great class!!!! One question.
I have data in a acess database that I want t write to pdf.
I will get my data like this:
Dim rs as New Recordset
rs.open "Select * from Clients", CL, 1, 2
Do while not rs.EOF
this is where I need Help
rs.movenext
loop
Can You Please assist?
Regards,
Gert
I want a c#.net code to read
I want a c#.net code to read text from pdf file can you help me
Coordinates
Hi,
All coordinates in the examples use the top left as 0,0.
I want to use it to produce a graph, so it makes it much simpler if 0,0 is bottom left.
How can I change the default?
Thank you.
jh
its urgent
Gautam here
hi,
its urgent
the tutorial was quite good but an error is occuring while i'm running it
the error is:
User-defineed type not defined
Error 91
I keep getting Error 91, it stoped whenever Strm.WriteLine TempStream is used. Any idea??
Thanks,
Little bug for lacalized computers
Hello
Here, in France, computers use the comma instead the point as the decimal séparator
in the PdfSetPageObject, you shoud use
PDFOutStream TempStream, "<< /Type /Page"
PDFOutStream TempStream, "/Parent " & ParentNum & " 0 R"
PDFOutStream TempStream, "/MediaBox [ 0 0 " & _
Replace(CStr(PageCanvasWidth(CurrentPDFSetPageObject + 1)), ",", ".") & " " & _
Replace(CStr(PageCanvasHeight(CurrentPDFSetPageObject + 1)), ",", ".") & "]"
PDFOutStream TempStream, "/Resources " & ResourceNum & " 0 R"
Thank you for your contribution, dear French friend.
It's not only in France. All the world where French, Spanish, Portuguese and Italian (at least) are spoken, use the comma
as the decimal separator.
This is hundreds of millions of people.
Now, in the age of globalization every programmer must think in terms of world, and not in terms of "home".
That's the reason why Unicode was invented.
By the way, you there, in France, invented the "Meter", "kilogram", "litre", and so on...
All the world accept that immediatly, except the english-speaking contries, who still use
the "yard", "mile", "pound", "foot", "gallon", etc. WHY ?
Your contribution is very useful, bacause in my computer the program didn't work at all, until I read your post, and you
save me hours (if not days) catching a very tiny bug, but that made all the difference.
Thank you
PDF
I would like to know If I can Open existing pdf documents and wright to them.
thanks
thanks for the pdf vb6,appreciate
Opening existing PDF
I'm trying to open an existing PDF, add a page number and print. Some comments above suggest tht mjwPDF can not open existing files. Is this correct. If so, could you please suggest a freeware or commercial VB library that supports this.
Thanks
Displayed PDF File
The generated PDF file is displayed at 37.1%. Is there a way in my code to have it displayed at a higher percentage, say 75% or even 100%. I realize the size can be changed with Adobe Reader once the file is displayed but I'd prefer to have it presented originally at a larger size. Thanks.
BTW, this is a great tool -- great function and very easy to understand and use.
Display PDF File
I've looked at objPDF.PDFSetZoomMode. There are options for FULLWIDTH and REAL which produce a 100% display. However, for my application these create something that is too wide. There doesn't appear to be a capability for setting a percentage such as that in Adobe Reader. I need 50% or 75%. Any way to do that in code?
Display PDF File
It would be beneficial to have something like objPDF.PDFSetZoomMode = ZOOM_PERCENT, X -- where "X" is the desired percentage for the display.. If done I assume that the specified percentage would have to be one of those supported by Acrobat Reader.
Rich Text Content in PDF
I have gone through your tutorial. Nice.
I need to create PDF with Rich Text.
Bold, Italic, Lines etc.,
Im getting "there was an error opening this document"
Hi
Im getting "there was an error opening this document" this file cannot be found...i have saved the pdf class in the same folder where i saved my project file....please give me a solution...im using the VB 6 version.
Thanking u
PDF internal link
Thanks for this class. It will be so usefull.
Is there a way to create a link that points to another page in the same PDF document?
Merci encore,
Daniel
Multiple Line to be printed
Hi,
It is working wonderfully, but i am facing problem while inserting logo in beginning fo the pdf doc and then data in multple lines from a text box.
Else it is perfect.
can you post the pdf file
can you post the pdf file that is created. I got the class to create the file but Acrobat shows there is problem reading the document. I would like to find what the error is by comparing your pdf file with mine.
Thanks for the excellent article.
Never mind. I corrected the
Never mind. I corrected the errors and converted it to VS 2008.
is it possible the contents
is it possible the contents of the pdf are from data in database??
thanks
For text file to PDF
Hi,
I am new to this PDF class that it's what I am looking for. I have two questions:
1: I have a text report created using Notepad, Now I want to open it directly using this PDF class object, how to do it?
2: I have a word file with images inside it, how can I show them in PDF?
Thanks!
Andraw
Hi, I read through the text
Hi,
I read through the text file line by line and write to pdf file, it works fine.
At first I was failed because I tried to open the text file then use READALL to write the whole contents in text file to pdf, the error is overflow.
I have a question, I use LANDSCAPE for page orientation, line space use 1.0, in this condition there are only 20 lines allowed each page. How can I control lines of each page? how can I make sure the pdf file is opened with 100% zoom? now it's 74%.
Thanks!
Neat
Hi. Just to let you know I appreciate your making this code available.
Worked "out ot the box" and I simply modified it slightly to automatically locate the location of the Program folder.
A quick question: does it handle mixing text and graphics in complex arrangements?
Best regards
Howard
VB 2008?
Hi,
Do you have something likes this that works with Visual Basic 2008 Express edition?
I try'd this but it doesnt work :D
mail me? gunther1987@gmail.com
Thx
PDF is empty
When I run the program the test_pdf seems te be empty. When I open it in professional it gives a pop-up that is gone so fast you can't read it. In the reader it says "there was an error reading document 14" ??
It opens the document anyway but it seems to be empty and very small. So small that the reader starts with zoom on 4228%. If you change it to 100% the document is 0,5 x 0,5 cm.
Any idea what might be the problem?
Jan Henk
Figured it out. Just
Figured it out. Just added
objPDF.PDFFormatPage = FORMAT_A4
objPDF.PDFOrientation = ORIENT_PAYSAGE
and now it works...
Hi, it happens on my pdf
Hi, it happens on my pdf too.
It is a bug, I think.
I have fixed it adding some code like the "Advance Pdf Example":
objPDF.PDFSetLayoutMode = LAYOUT_DEFAULT
objPDF.PDFFormatPage = FORMAT_A4
objPDF.PDFOrientation = ORIENT_PORTRAIT
objPDF.PDFSetUnit = UNIT_PT
Bye
Problem w/sample pdf when using a different computer
I downloaded and ran the pdf file generator via VB6 and it worked fine.
I downloaded the same exact files, set it up exactly as before, on a different computer and the pdf is generated. However, I also get an error message via Adobe Reader that locks up the reader. A message box is displayed that says, "There was an error opening this document. This file cannot be found."
I've checked the path generated by the program, and it is the path in which the file is located. Again, if I attempt to locate the file, it is generated and located where the program puts it.
The error generated in Adobe locks up, and clicking on the 'okay' button on the message box does not close the program.
Help!
Hi, It could be that the
Hi, It could be that the file path name is too long.
You have to use a function that convert into Format Dos name of the flle path.
Read some post below.
Bye
Excellent class
... but there is one thing more which will satisfy all my needs.
How to join, two pages in one pdf file, where the first page
is just created by the code and the next page is from existing
pdf file. This second, existing file, can be single or multipage
and should be added as a second and next pages, to that which
is just creating.
regards , Martin
Hi, I tried your code, the
Hi,
I tried your code, the PDF file was created but wasn't shown automatically at the end.
Can you help me?
Thank you.
Fix, automatically open file
Probably the directory path name where the file .pdf is stored is too long. You have to use the DOS format name for the directory path.
example: Windows Path --> C:\Programmi
DOS Format --> C:\PROGRA~1
I had your same problem. I have examined the code of the pdf class.
In the sub PdfEndDoc, in the last code rows there's the function Shell.
It opens the Adobe program e the file pdf created.
You have to modify this row.
I have found this function :
Private Declare Function GetShortPathName Lib "KERNEL32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long
You use it to convert the .pdf file name
Bye
A better way to open the file
Thanks Luca, but it still wouldn't display for me. Stepping through the code, I noticed that it wasn't finding Adobe Acrobat (I'm running Vista and have Adobe Acrobat Standard 8.0 installed). So, I decided to modify things a bit.
I inserted this API declaration into the mjwPDF class:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongAnd then replaced this in the PDFEndDoc() subroutine:
If boPDFView ThenPDFScanRepAdobe "C:\Program Files\", 0
If wsPathAdobe <> "" Then
iRet = Shell(wsPathAdobe & " " & PDFGetFileName, vbMaximizedFocus)
End If
End If
With this:
If boPDFView TheniRet = ShellExecute(0, "open", PDFGetFileName, vbNullString, vbNullString, 3)
End If
This results in the PDF file opening in the default application set up to open PDF files. You'll also notice that there is no need to use the short path name.
Great class,
Todd
Thank you so much!!!
Thank you so much!!!
That's good! It's a better
That's good!
It's a better solution.
Bye
HELP ME PLEASE!?!?!
Let me start off by saying THANK YOU SO MUCH FOR THIS!
However, I need some help.
Can you resize the pdf page to a custom width and height?
I have a vb6 form with two text boxes and a command button.
When you press the command button, I want a pdf file to be created with the width of text1.text and the height of text2.text.
If you could show me how to do this it would be wonderful!
Thanks in advance!
You can e-mail me at:
leptictidium109@yahoo.com
nevermind, i figured it out
nevermind, i figured it out
Creating a PDF file of an html table
I have a simple html table consisting of 2 lines:-
HelloWorld
GoodbyeWorld
Can you tell me how to write this to a PDF file including simple grid lines.
thanks
Charles W
Generating multiple PDF files
If you have to generate multiple PDF files at one time, remember to call objPDF.PDFInit AFTER calling objPDF.PDFEndDoc, THEN call objPDF.PDFBeginDoc as usual - if you don't call PDFInit you'll wind up with a jumbled mess and run-time errors after the first PDF file because things aren't set up properly.
text box
Hi is there a way to print a text box with more than 1 line of text in it so that each line is 1,2,3 and not in a row
Thanks Joe K
PDFfiles with multiple text lines
Like most things in life, this is pretty easy if you know how - and frustrating if you don't!
Here's how to do it from a text file - generating it from a list box or multiline text box would be similar:
LineYPos = 1LineCtr = 1
Open InputFile For Input As #1
While Not EOF(1)
Line Input #1, StrIn
If LineCtr > 85 Then ' adjust for the number of lines to print per page
objPDF.PDFEndPage
objPDF.PDFNewPage
LineYPos = 1
LineCtr = 1
End If
' here's the magic
objPDF.PDFTextOut StrIn, 1, LineYPos
LineYPos = LineYPos + 0.33
LineCtr = LineCtr + 1
Wend
Close #1
list box on vb gui to convert to pdf
Hi, I have a few check boxes that have string part number text associated with one another. They are pointed to print on the list box on the gui. I set up the pdf module and works great however dont know how to get the data to convert from the list box to the pdf. What command do I want to use? thx
Anyone? Please give an
Anyone? Please give an example.
Post new comment