WELCOME TO OUR BLOGGER

Friday, December 23, 2011

Creating PDF files in Visual Basic

In the previous tutorial we looked out how to create a simple PDF document that was one page and was only text. If you've seen many PDF documents on the web or elsewhere you know that they usually include more than just text. Fortunately we are able to do these things as well. In this VB PDF tutorial we're going to look at how to further use the mjwPDF class accomplish what we need. By the end of the tutorial you should be able to create a multi-page document that has headers, footers with page numbers, shapes, graphics, and web links.If you want, you can download the source code for this VB PDF tutorial and follow along with it.
If you haven't read the introductory tutorial about Creating a PDF document using Visual Basic. Please do so first. This VB tutorial builds off the previous one. In fact to start with lets look at the code we created before:
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
Great. With this code we've done all the initializing and created a basic file. Now lets add some formatting options. Right after we set the fonts folder lets add a few lines of code that tell mjwPDF what the document layout should be like.
' We must tell the class where the PDF fonts are located
    objPDF.PDFLoadAfm = App.Path & "\Fonts"
    
    ' Set the file properties
    objPDF.PDFSetLayoutMode = LAYOUT_DEFAULT
    objPDF.PDFFormatPage = FORMAT_A4
    objPDF.PDFOrientation = ORIENT_PORTRAIT
    objPDF.PDFSetUnit = UNIT_PT
    
    ' View the PDF file after we create it
    objPDF.PDFView = True
This code sets up a standard page (letter size) in portrait orientation and our units of measure as points.
Next lets do something fun. Often times you want to add a heading to a PDF document such as "Very Important Report Blah Blah" Lets figure out how to add a heading such as this to our document. Delete the bold lines of code below:
' 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
And add these lines of code in their place:
' Lets add a heading
objPDF.PDFSetFont FONT_ARIAL, 15, FONT_BOLD
objPDF.PDFSetDrawColor = vbRed
objPDF.PDFSetTextColor = vbWhite
objPDF.PDFSetAlignement = ALIGN_CENTER
objPDF.PDFSetBorder = BORDER_ALL
objPDF.PDFSetFill = True
objPDF.PDFCell "A centered heading", 15, 15, _
    objPDF.PDFGetPageWidth - 30, 40
Let me explain what these mean. You should recognize the first line of code. It just sets the font info. Next we set the DrawColor (which in this case will be the highlight or inside color of our box). Next the text color is set and our alignment and border. The PDFSetFill=true tells mjwPDF to fill this box in when it prints out our text. The next line is what displays it all.
Let me break it down. The first parameter is simply the text we want displayed. Next we tell it how far over from the left we want the box (or cell) in our case we said 15 points over from the left. The next parameter is 15 points down from the top. Next we have to specify how wide the box is going to be. We want it to stretch all the way over to the right side of the page (minus the 15 point right border). To accomplish this we can use the mjwPDF classes PDFGetPageWidth function. This will give us the full width of the page we then subtract 30 off of it (15 for the left border and 15 for the right border), the last parameter is the height of the cell, 40 will be plenty high to accommodate our text.
If you run the code you should see your PDF pop up with a beautiful header at the top of the page.

Another fun thing is to create shapes in your PDF files. This can be used to create bar graphs or to highlight certain areas. Here is some sample code that creates a square.
' Lets draw a dashed red square
        objPDF.PDFSetLineColor = vbRed
        objPDF.PDFSetFill = True
        objPDF.PDFSetLineStyle = pPDF_DASHDOT
        objPDF.PDFSetLineWidth = 1
        objPDF.PDFSetDrawMode = DRAW_NORMAL
        objPDF.PDFDrawPolygon Array(300, 150, 400, 150, 400, 250, 300, 250)
Most the settings are self explanatory. Notice that you can specify the line style and the line width. Also notice that there is no draw square function. Instead there is a draw polygon function. It takes one parameter, but that parameter is an array of points specified in x y coordinates. X being how far from left to right to draw the point and Y being how far from top to bottom. So in our example we are specifying 4 points (the four corners of the square).
  • Point 1 is 300 pixels to the right, 150 pixels from the top
  • Point 2 is 400 pixels to the right, 150 pixels from the top
  • Point 3 is 400 pixels to the right, 250 pixels form the top
  • Point 4 is 300 pixels to the right, 250 pixels from the top.
Next lets draw an ellipse. An ellipse is simply a circle that can be squeezed either vertically or horizontally. To define it correctly we have to use some mathematical terms. If you remember from geometry class a circle has a radius. The radius is the distance from the center of the circle to the edge of the circle. An ellipse has two radiuses. One is horizontal the other is vertical. So the code for our ellipse is this:
' Lets draw an ellipse
        objPDF.PDFSetDrawColor = vbYellow
        objPDF.PDFSetLineColor = vbBlack
        objPDF.PDFSetLineStyle = pPDF_DASHDOT
        objPDF.PDFSetLineWidth = 1.25
        objPDF.PDFSetDrawMode = DRAW_DRAWBORDER
        objPDF.PDFDrawEllipse 300, 150, 75, 25
All the parameters should make sense by now. The new line is the PDFDrawEllipse call. Its a very simple call except that many times you think the x and y coordinates would correspond to the center of the circle. However, you would be wrong. Instead the first to parameters correspond to the upper left corner of the square that bounds the ellipse. The next two parameters specify the horizontal radius and the vertical radius respectively. If this seems confusing just run the program and you will see what I mean. The x & y parameters for our ellipse are the same as the x & y parameters for our first point in the square so you will see how it works. If you run the program you should see this:

Lets step back to text manipulation in PDF documents again. One thing you usually see in a professional document is the header like we did above. Another thing is usually page numbers in the footer. We can use the same logic we used for our header to add page numbers. I would like to add the numbers in the footer of the page on the right side, like most documents have. I'm not going to walk through how you can do this step by step, but here is the code for a visual basic subroutine that adds the page number to the bottom right corner of your PDF document.
' Adds the page number to the current page
Private Sub AddPageNumber(objPDF As mjwPDF, pageNumber As Integer)
    Dim sPageInfo As String
    Dim fontSize As Double
    Dim margin As Double

    fontSize = 10       'Size of font to use
    margin = 40         'Size of margin (left, right, bottom)
    
    ' Set what we want to print for page info
    sPageInfo = "Page " & pageNumber
    
    ' Should save these settings and change them back for more robust code
    objPDF.PDFSetTextColor = vbBlack
    objPDF.PDFSetAlignement = ALIGN_RIGHT
    objPDF.PDFSetFont FONT_ARIAL, Conversion.CInt(fontSize), FONT_NORMAL
    objPDF.PDFSetFill = False

    ' Uncomment the below line if you want to see how our formatting works
    'objPDF.PDFSetBorder = BORDER_ALL
    
    ' Draw the page number at the bottom of the page to the right
    objPDF.PDFCell sPageInfo, margin,  _
        objPDF.PDFGetPageHeight - margin - fontSize, _
        objPDF.PDFGetPageWidth - (margin * 2), fontSize

End Sub
Now that we know how to add page numbers how do we actually create multiple pages? Its very simple. When you are done with the first page, simply call the PDFEndPage method. Next call the PDFNewPage method to start the next page. Than just call the commands to add your text or shapes to the next page. You can do this as many times as you want. Don't forget to call the AddPageNumber method on each page though.
Another useful feature of PDF documents is adding bookmarks. Bookmarks allow you to jump from section to section in a PDF document easily. When the user views a PDF document with bookmarks, they are able to see a table of contents type tab on the left side of the screen. Note: if you want that pane to be visible by default you should add this line of code to the initializing section of your program.
' Lets us set see the bookmark pane when we view the PDF
    objPDF.PDFUseOutlines = True
Adding a bookmark is very easy in Visual Basic using mjwPDF. For instance lets add four bookmarks to our first page of our document. Call these anywhere in your code before you call the PDFEndPage method.
'Lets add a bookmark to the start of page 1
        objPDF.PDFSetBookmark "A. Page 1", 0, 0
        
        'Now a bookmark half way down page 1
        objPDF.PDFSetBookmark "A1. Page 1 Halfway down", 1, 300
        
        'Now one at the end page 1
        objPDF.PDFSetBookmark "A2. End of Page 1", 1, 500
        
        'Another one a little further down and shows nesting
        objPDF.PDFSetBookmark "A2-Sub1.", 2, 800
The first call to PDFSetBookmark creates a bookmark labeled "A. Page 1". The next parameter is the depth of this bookmark. Note: Start your depth at 0. The last parameter is the y position to where the bookmark will move the page. So the first call created a bookmark titled "A. Page 1" that points to the top of page 1. The next call creates a bookmark titled "A1. Page 1 halfway down". It has a depth of 1 (so it will be a child under our first bookmark) and it will scroll the page 300 points down. If you run the program you will see all the bookmarks created like this screen shows.

Another necessity to learn when creating PDF documents is how to add images to them. The mjwPDF class allows you to add any .jpg images to your PDF document. If the image is in a different format you will need to convert it to .jpg before you will be able to add it to your PDF file. However, if the image is a jpeg it is very easy to add it to the PDF doc. In the sample source code included with this tutorial you will see a logo.jpg file. Below is the code to end our first page and to start our second page. On the second page we add our logo to the upper left corner of the page.
objPDF.PDFEndPage
        
        'Start page 2
        objPDF.PDFNewPage
        
        'Lets add an image to page 2
        objPDF.PDFImage App.Path & "\logo.jpg", _
            15, 15, 50, 50, "http://www.vb6.us"
The highlighted code is what adds the logo. We call the PDFImage function. The first parameter is the path to the jpeg file. The next two parameters are the x and y coordinates for the logo. The next two parameters specify the width and height of the image. These parameters can be left off and then it will just display the picture in its original size. You can also specify just the height or width and it will scale the other side of the picture to keep it in proportion. The last parameter is also optional, but it allows you to specify a web site to go to if someone clicks on the image.
If you run your program now you will see a PDF file that has all the properties of a complete PDF document. Headers, shapes, images, and page numbers. Combining all these techniques you should be able to do just about anything you would want to. Download the Advanced PDF VB Tutorial source code to see the full sample.

No comments:

Post a Comment

Search

Blogroll