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.
it doesn't work
Hi!
I copied the code. But it doesn't work. The error "member data not found" always appear.. pertaining to the line "objPdf.PdfTitle"
I hope you could help me solve this.
Thanks..
Watermark semi-transparent
First of all, thanks for the code. I use it a lot in my VB6 app and works quite well. I am using the PDF.Watermark property, but wondered if you knew how to create it with some kind of semi-transparent factor?
print the created pdf
Great.
How to print the just created pdf through vb6?
http://amzn.to/ABCbloglink
That is so true PDF files in Visual Basic As an author and business man, I like how you said "PDF stands for portable document format. It is a format that every operating system on virtually any computer can read". I hope more people discover your blog because you really know what you're talking about. Can't wait to read more from you!
how to creat pdf file through vb6 with Access data
How to creat pdf file through vb6 with Access data ????
plz. detail
Error in opening by reader (error 14)
What's this. Who can help me?
When I try to open test.pdf with Acrobat Reader 9, I see an Acr.Reader's blank page with report:
error on opening file (error 14) file corrupt.
WindowsXPsp3 + Acrobat Reader 9
sie8(_at_)rambler.ru
Thank you
Try adding the uncommented
Try adding the uncommented code at the end of the PDFEndDoc subroutine.
At least for me this finds my default PDF viewer (Foxit rader)
If boPDFView Then
' PDFScanRepAdobe "C:\Program Files\", 0
' If wsPathAdobe <> "" Then
' iRet = Shell(wsPathAdobe & " " & PDFGetFileName, vbMaximizedFocus)
' End If
'ShellExecute Me.hWnd, "open", App.Path & "\your.pdf", vbNullString, vbNullString, SW_SHOWNORMAL
ShellExecute lngHandle, "open", PDFGetFileName, vbNullString, vbNullString, SW_SHOWNORMAL
End If
hey genius!
.. How can set the text to the next line if it is in the end line.. THank you so much!!
How to Set PDF version in the output PDF
I am using a vb application which generates a PDF using Acrobat Exchange, however the version of Adobe Acrobat I am using is Acrobat 9.0 and I would like the output PDF generated to be PDF 1.4. Is there any explicit API to set the version of the output PDF in the VB program itself to 1.4 as currently it is generating PDF 1.6 with Acobat 9.0?
Code to convert pdf file to jpeg using vb.net
Code to convert pdf file to jpeg using vb.net
You absolutely rock!! thanks
You absolutely rock!! thanks a bunch
PDF files in Visual Basic
I've found this... can't figure out what the syntax is to actually call the subroutine though.
Sub Export(FileName As String, Extension As String, [SelectionSet As SelectionSet])
Member of DWGeditor.Document
Exports the file to any of the supported formats. Optionally, pass in a selection set to export only a part of the file. Format is based on the extension in the FileName (default is the DXF format).
I was thinking something like:
error 70 with windows 7
Listings in PDF work fine with Windows XP but run the program with windows 7 gives error 70, and breaks the program. Please .. help me
need source code for softwae develop in vb 6.0
Any body help me to develop shop bill software in visual basic 6.0
Your PDF tutorial in VB6
I downloaded the tutorial and ran it. Then I began to write different things with different fonts. Everything works just fine.
I undertsand that this code works perfectly well to create a PDF from scratch.
My question is how can I write over an existing PDF?
I will give you a very simple example. I receive a printed page (single sided) with three lines of text on it
First line: one digit number
Second line: "Box(xx) YY" [xx YY are doubel sigit numbers]
Third line: LastNeme, FirstName, date of birth, five digit ID number
I scan this page and produce a PDF file
How can I possibly add a fourth text of line that would be the PDF file name itself?
I know how to get the file name: Cuerda$ = Dir$(Path$+"*.PDF")
Where in the PDF format do I find the appropriate place to insert new text into an existing PDF i did not create myself in the first palce?
How do I subscribe to your forum???
how to set password to pdf file
Sir,
How do we set password to pdf file?
Regards
Help with the Shell Command
I have written a program in VB5.0Pro and it uses a shell command to run a bat file opening a certain page in an Adobe reader. PDF File It works by running the bat file ok. It works using a shell command with Borlands Turbo Basic Program but will not work with VB. Please can you help !! The Shell comand will run other programs ok so the path is working. Adobe does not seem to like something is comes up with an error and says it needs to close every time.
mjwPDF
I got run-time error 70. Help!
Hey I like to make a magazine
Hey
I like to make a magazine by PDF ... So how will the interface of the magazine will be ?
Help for Ideas
Thanks all
Images
Hi,
I think this is really cool.
Can you also insert images?
Juergen
Rights
I have some interest in using this pdf function in an application. Could you fill me in on rights with respect to font data? Thanks.
Getting AddPageNumber Error
Hello,
i have seen in coding a line which shows AddPageNumber but while runing the programe that is the main erro... pls advicxe me what is AddPageNumber??????
Re: Happy
Hi All Users.......
I'm the new User and recently I registered.
I'm gettinhg great thanks to evryone which connects with ur special queries???????
Thanks withamling...urs..
Saini
jpeg not written
Hi,
So far I can get this to work, except for any jpeg file which is not embedded in the pdf. The pdf is made without error, but is only 312 bytes big...
Any idea?
thanks!
Vis
ok, pilot error... Figured
ok, pilot error... Figured that when only wrapping a jpg image in a pdf it wasn't necessary to include the fonts. Not so...
It works now.
Best, vis
VB 2008 - doesn't work.
Hi, this looks incredibly useful for a yr 12 project i am doing - is there another version anywhere that will work with vb 2008? When i try and run it i get heaps of errors - mainly just incorrect declarations and incorrect syntax.
Thank you in advance.
wooowww,,, greattttt,,,
wooowww,,, greattttt,,, b(^_^)d
but How can i make the table with an array ???
like this...
-------------------------------------------
| No | Name |
-------------------------------------------
| 1 . | xxxxxxxxxxxxxxxxxxxxxxx |
| 2. | yyyyyyyyyyyyyyyyyyyyyyyy |
| N. | zzzzzzzzzzzzzzzzzzzzzzzz |
-------------------------------------------
but with the true line..
thanx for respect,,,
Multi lines?
Hello
How can we have two line in our PDF document?
for example we have a textbox and we want to copy it's text to a PDF and textbox is:
Hello
and ...
Thanks a lot
Converting DOC Files
I have some DOC files that I want to programatically transform into PDF files.
How can this be done?
mjwPDF
I have run the program but show error 19 if any help me I will remain thanks
mjwPDF
I faced troubles too. But the error number was 70.
Creating PDF files in Visual Basic
Great stuff - and really appreciated. Just happened to stumble on it and Hey Presto a lot of my problems solved.
Many thanks.
What's your Adobe Reader
What's your Adobe Reader version?
GOOD JOB! I 'll use
GOOD JOB!
I 'll use this code on my new project.!
Tables in PDF
Hi!
The tool is great !
Can I create a table of rows and columns with this great tool?
Awesome
I tried inserting database files on the pdf and it works. thanks
Private Sub Command1_Click()
Dim xTotAl As Integer
Dim xCtR As Integer
Dim xPage, mCaptioN
mCaptioN = Me.Caption
With Adodc1
.ConnectionString = "Provider=MSDASQL.1;Password=pwd1234;Persist Security Info=True;Data Source=NwindVBP;Mode=ReadWrite"
.CursorLocation = adUseClient
.RecordSource = "Select * From tblProducts"
.Refresh
End With
With objPDF
.PDFTitle = "Test PDF Document"
.PDFFileName = App.Path & "\test2.pdf"
.PDFLoadAfm = App.Path & "\Fonts"
.PDFView = True
' Lets us set see the bookmark pane when we view the PDF
.PDFUseOutlines = True
.PDFBeginDoc
.PDFSetLayoutMode = LAYOUT_DEFAULT
.PDFFormatPage = FORMAT_A4
.PDFOrientation = ORIENT_PORTRAIT
.PDFSetUnit = UNIT_PT
'--------------------------------------------
' Lets add a heading
xHeaders
'ProductID
' ProductName
' SupplierID
' QuantityPerUnit
' Unitprice
' UnitsOnOrder
Adodc1.Recordset.MoveFirst
xCtR = 75
xPage = 0
Do While Not Adodc1.Recordset.EOF
'------------------------left, height
Me.Caption = "Reading file/s .." & Adodc1.Recordset!ProductName
.PDFSetFont FONT_ARIAL, 8, FONT_NORMAL
.PDFTextOut Adodc1.Recordset!ProductID, 20, Str(xCtR)
.PDFTextOut Adodc1.Recordset!ProductName, 75, Str(xCtR)
If Adodc1.Recordset!SupplierID <> "" Then
.PDFTextOut Str(Adodc1.Recordset!SupplierID), 285, Str(xCtR)
End If
.PDFTextOut Adodc1.Recordset!QuantityPerUnit, 335, Str(xCtR)
.PDFTextOut Str(Adodc1.Recordset!unitprice), 415, Str(xCtR)
.PDFTextOut Str(Adodc1.Recordset!UnitsonOrder), 460, Str(xCtR)
xTotAl = Val(Adodc1.Recordset!unitprice) * Val(Adodc1.Recordset!UnitsonOrder)
.PDFTextOut Str(xTotAl), 520, Str(xCtR)
xCtR = xCtR + 10
Adodc1.Recordset.MoveNext
If xCtR >= 790 Then
xPage = xPage + 1
AddPageNumber objPDF, Str(xPage)
.PDFEndPage
.PDFNewPage
xHeaders
xCtR = 75
End If
Loop
xPage = xPage + 1
AddPageNumber objPDF, Str(xPage)
.PDFEndPage
'--------------------------------------------
.PDFEndDoc
Adodc1.Recordset.Close
End With
Me.Caption = mCaptioN
'Unload Me
End Sub
Sub xHeaders()
With objPDF
.PDFSetFont FONT_ARIAL, 10, FONT_BOLD
.PDFSetDrawColor = vbRed
.PDFSetTextColor = vbWhite
.PDFSetAlignement = ALIGN_CENTER
.PDFSetLineWidth = 2
.PDFSetBorder = BORDER_ALL
.PDFSetFill = True
.PDFCell "Product/s Description", 15, 15, objPDF.PDFGetPageWidth - 20, 20, "http://www.svi.com.ph"
'--------------------------------------------
.PDFSetFont FONT_ARIAL, 8, FONT_BOLD
.PDFSetTextColor = vbBlack
.PDFSetAlignement = ALIGN_FJUSTIFY
'------------------------left, height
.PDFTextOut "Product ID", 20, 60
.PDFTextOut "Product Name", 75, 60
.PDFTextOut "Suppliers", 285, 60
.PDFTextOut "Q/Per Unit", 335, 60
.PDFTextOut "Unit Price", 405, 60
.PDFTextOut "Units Order", 450, 60
.PDFTextOut "Total", 520, 60
End With
End Sub
Really awesome
Thanks alot for this, very nice.
I wish to know whether it's possible to produce txt file from a pdf file.In short is it possible for me to read my PDF file and return a corresponding txt file in vb6.Thanks in advance
Thank you~
Thanks a Lot~~~~~~
VB PDF Tutorial
Just downloaded it, tried it, and it works. Amazing stuff. Many, many thanks. This should give me hours of entertainment.
VB PDF Tutorial
Do you really succeed? Can you help me?
thanks
thanks....
honestly, i have no idea in using visual basic yet. i downloaded your code and simply run it. i also tried modifying the lines such as hello, world ..... and it worked!!! i am so amazed that i started appreciating the significance of vb. just wanna ask you how to add some lines... i tried to write some lines including your hello, world but there's an error. what should i do for me to add some lines if ever?
load code of the file in the vb6.0 on run_time
Hello.Can you say me how to create a program with these details:
form1 (for load code)
form2 (with command1 and label1 in the form without any code)
Form_Load of Form1 should send the code from "c:\a.txt" (or other address) to the form2 (in the run_time)
c:\a.txt :-------------------------------
dim count as integer
private sub form_load()
command1.caption="Run"
label1.caption="0"
count=0
end sub
private sub command1_click()
count=count+1
label1.caption=count
end sub
--------------------------------------
Please put it's source file to download . "Thank You"
Error in opening by reader (error 14)
Hi, great class! Thanks
When I try to open test.pdf with Acrobat Reader 9 show me a blank page and report and error on opening file (error 14) file corrupt. I tryed also Acrobat 4.0 but is the same.
Any suggestions?
Thank you
THANK YOU!
THANK YOU, SIR! IT SEEMS NICE UTILITY..!
very very thanks
very very thanks
error in the above code,, plz help me
i m getting error in SETHEADER function ,,, in this line
strm.WriteLine "%PDF-" & mjwPDF
THANKS A LOT 谢谢
THANKS A LOT
谢谢
accesso negato
ciao,
ho scarico ed eseguito le istruzioni del tutorial e tt ok, però quando quando lo eseguo mi dice autorizzazione negata....come posso fare??
Very very nice contribution
Very very nice contribution in the web....
Both bold and italic font
How do I set the font to be both bold and italic for the PDFTextOut function?
Thanks.....Marv
I think you should specify
I think you should specify "FONT_BOLD Or FONT_ITALIC".
Don't know, just try it.
Sockla
how to add data in pdf from textbox?
how i can add data from textbox to pdf?
here only code for providing fix string or data.
please help me on this topic and send me code on my email.
vs7527@gmail.com
If you can't figure this
If you can't figure this out, you probably shouln't be coding at all.
Try this code:
objPDF.PDFTextOut (txttesting.Text), 1, 1
objPDF.PDFTextOut (txtname.Text), 1, 2
objPDF.PDFTextOut (txtisa.Text), 1, 2
NOTE:
The Number there is the x and y axis of the PDF..
Display image from Picture
Hi,
I want to display image in PDF File not from *.jpg but from obj.Picture. It's possible...
Help-me
Best Regard
Elisio
most picture load fails come
most picture load fails come from people not relizing that .picture is a place holder not a local data hold
try loading the picture to a image file then root your code product from image to picture
like :
image.path = "some file"
picture1.pic = image1.picture
picture1.picture = pdfobj.picuture or (whatever)
picture1 loads fail most commonly because they are orignaly designed to be used as buffers
some people will tell you its the global local call idea that caused all this its not...
there buffers.. ever wonder why a computer will just strart running slow after a while.
its because most programmers dont know this...
it seems like its not importiant but computers run at funny speeds
the double load under images allows the processor to set it self for new information
instead of guessing what is to come next and creates the higher resolution speed machine process
allowing the computers ai to catch its next idea. this all has to do with vb's API code,,,,,
next just remember that vb works this way because the creators new about the machine code
and vb being known as a beginner language. they put many small work arounds like this in place
to option place markers for study.. I am quoteing all this from a book I have on vb and by memory
so sorry if its a little complicated.
Can any one help me to make
Can any one help me to make a program to search and open specified files using vb 6.0, c++6.0 or Visual Fox Pro 6.0
alignment prblm in class file
some alignment prblm using arial font
how to solve
Thats great
Thats great
I tried this tutorial and
I tried this tutorial and it's very straight forward I will try the advanced lesson
thank you very much for this.
Stand Alone
Hi there,
Is ghostscript a requirement for the mjwPDF Class to work?
Many thanks...
Invoice Printing In PDF
Creating A invoice to visual basic as output as PDF please help to create them
textbox text into PDF
hello there.)
can some one teach me..how can i make text in the textbox appear in the PDF file..?
hope can help..
sorry my writing..?
Multi-Page PDF File
First of all, thanks for a great tool. I have the following situation. When I create a multi-page PDF file and it's presented for viewing with the Adobe Reader the document is presented at the bottom of the last page instead of at the top of the first page. Is there some way within the VB code to have the document presented at the top of the first page? Thanks.
(Note: I get the same result if I open the file directly with the Acrobat Reader. However, if I open the file directly with the NitroPDF program it is presented starting at the top of the first page.)
Multi-Page PDF File
I've figured it out. In the Class Module find the PDFSetCatalog Sub. In the code for PDFZoomMode you will see four statements that have "/ OpenAction [3 0 R ......" in them. Change the 3 to 1 in all four statements.
This resolved the situation. The displayed document now begins at page 1, both within the VB application and when the file is opened directly with Adobe Acrobat.
As an aside, why would the default ever be to display the document beginning with the last page?
Error path not found
Hello,
I am getting a path not found error.
The debugger highlights this line:
Set oRep = Fso.GetFolder(sPathAFM)
in the routine:
Property Let PDFLoadAfm(sPathAFM As String)
Can someone help with this?
Maybe because sPathAFM is set
Maybe because sPathAFM is set to "C:\Program Files" and you are working with a Windows version other than English.
i.e. for Spanish you should search the source and change "C:\Program Files" to "C:\Archivos de programa"
Please help me.
Hello everyone,
I am so impressed of what i saw on your example. Actually I have a project that have a similarity on your tutorial but then i saw myself so difficult because I am so new on developing a program and gaining some idea for it because i develop the program in web. My problem here is, how can i convert the .jpg file to .pdf file? Please help me sir/mam... and i really appreciate it your kindness...
thanks...
regards
silverstaind
Awesome ~ cheers !!! How
Awesome ~ cheers !!!
How about populate data into PDF template file?
Excellent Tutorials
Thank you for this great tutorial .. I can say that I have checkout many sites related to vb6 but none like this one.. good job
Greetz from Mexico
Post new comment