# VBForums UtilityBank > UtilityBank - Components >  VB2005 PDF Document Creator Component

## Ken B

Title: vbPDF
Description: PDF document creator, written in VB 2005
Author name: Ken Bradford
License info: Open Source, GNU General Public License

To use, just add the following three files to the your project: vbPdf.vb, vbPdfFonts.vb and vbPdfRoutines.vb.



```
    'This example will show you how to display text in a document.

    Me.Cursor = Cursors.WaitCursor

    'Creates a new instance of cPdf.
    Dim cPDF As New vbPDF

    With cPDF
      .ReportFileName = "c:\Text.pdf"
      .ScaleMode = pdf.ScaleMode.Inch
      .PaperSize = pdf.PaperSize._8_5x11
      .Author = "Ken Bradford"
      .Title = "Example 2a - Text"

      If Not .CreatePDFFile() Then
        Me.Cursor = Cursors.Default
        Exit Sub
      End If

      'This example will be using the following fonts: Arial, ArialBold, ArialItalic, CourierNew and CourierNewBold.
      .LoadFont("Arial", pdf.FontName.Arial, pdf.FontStyle.Regular)
      .LoadFont("ArialItalic", pdf.FontName.Arial, pdf.FontStyle.Italic)
      .LoadFont("ArialBold", pdf.FontName.Arial, pdf.FontStyle.Bold)
      .LoadFont("CourierNew", pdf.FontName.CourierNew, pdf.FontStyle.Regular)
      .LoadFont("CourierNewBold", pdf.FontName.CourierNew, pdf.FontStyle.Bold)

      .StartPage()

      'PDF DRAWS A PAGE FROM THE BOTTOM TO THE TOP.

      'PageWidthInInch returns the width of the page. In this case it will return 11, since .PaperSize = pdf.PaperSize._8_5x11.
      'x will be set to 5.5 (11 / 2).
      Dim x As Single = .PageWidthInInch / 2

      'PageHeightInInch returns the height of the page. In this case it will return 8.5, since .PaperSize = pdf.PaperSize._8_5x11.
      'y will be set to 4.25 (8.5 / 2).
      Dim y As Single = .PageHeightInInch / 2

      'Text Alignment.
      .DrawText(x, 10.25, "This text is align left from the center of the page", "Arial", 12, pdf.TextAlignment.Left, Color.Red)
      .DrawText(x, 10, "This text is align center from the center of the page", "Arial", 12, pdf.TextAlignment.Center, Color.Green)
      .DrawText(x, 9.75, "This text is align right from the center of the page", "Arial", 12, pdf.TextAlignment.Right, Color.BlueViolet)

      'Different font styles on the same line.
      .DrawText(2.5, 8, "Name", "Arial", 12, pdf.TextAlignment.Right, Color.Black)
      .DrawText(2.6, 8, "Jack Benny", "ArialBold", 12, pdf.TextAlignment.Left, Color.Black)
      .DrawText(5, 8, "Age", "Arial", 12, pdf.TextAlignment.Right, Color.Black)
      .DrawText(5.1, 8, "39", "ArialBold", 12, pdf.TextAlignment.Left, Color.Black)
      .DrawText(2.5, 7.8, "Profession", "Arial", 12, pdf.TextAlignment.Right, Color.Black)
      .DrawText(2.6, 7.8, "Star of Stage, Screen, Radio and Television", "ArialBold", 12, pdf.TextAlignment.Left, Color.Black)
      .DrawText(2.5, 7.6, "Place of Birth", "Arial", 12, pdf.TextAlignment.Right, Color.Black)
      .DrawText(2.6, 7.6, "Waukegan, Illinois", "ArialBold", 12, pdf.TextAlignment.Left, Color.Black)


      'Underlining text.
      .DrawText(0.25, 6.2, "This is Arial text, underlined.", "Arial", 15, pdf.TextAlignment.Left, Color.SeaGreen, Color.SeaGreen, 0.001)
      .DrawText(0.25, 6.0, "This is Arial Italic text, underlined.", "ArialItalic", 15, pdf.TextAlignment.Left, Color.SeaGreen, Color.SeaGreen, 0.001)
      .DrawText(0.25, 5.8, "This is Arial Bold text, underlined.", "ArialBold", 15, pdf.TextAlignment.Left, Color.SeaGreen, Color.SeaGreen, 0.001)
      .DrawText(0.25, 5.6, "This is CourierNew text, underlined.", "CourierNew", 15, pdf.TextAlignment.Left, Color.SeaGreen, Color.SeaGreen, 0.001)
      .DrawText(0.25, 5.4, "This is CourierNew Bold text, underlined.", "CourierNewBold", 15, pdf.TextAlignment.Left, Color.SeaGreen, Color.SeaGreen, 0.001)

      .DrawText(0.25, 4.5, "This text is underlined with a line thickness of .01 inch", "CourierNew", 15, pdf.TextAlignment.Left, Color.Firebrick, Color.Firebrick, 0.01)
      .DrawText(0.25, 4.25, "This text is underlined with a line thickness of .02 inch", "CourierNew", 15, pdf.TextAlignment.Left, Color.Firebrick, Color.Firebrick, 0.02)
      .DrawText(0.25, 4, "This text is underlined with a line thickness of .03 inch", "CourierNew", 15, pdf.TextAlignment.Left, Color.Firebrick, Color.Firebrick, 0.03)
      .DrawText(0.25, 3.75, "This text is underlined with a line thickness of .04 inch", "CourierNew", 15, pdf.TextAlignment.Left, Color.Firebrick, Color.Firebrick, 0.04)
      .DrawText(0.25, 3.5, "This text is underlined with a line thickness of .05 inch", "CourierNew", 15, pdf.TextAlignment.Left, Color.Firebrick, Color.Firebrick, 0.05)
      .DrawText(0.25, 3.25, "This text is underlined with a line thickness of .06 inch", "CourierNew", 15, pdf.TextAlignment.Left, Color.Firebrick, Color.Firebrick, 0.06)
      .DrawText(0.25, 3, "This text is underlined with a line thickness of .07 inch", "CourierNew", 15, pdf.TextAlignment.Left, Color.Firebrick, Color.Firebrick, 0.07)

      .DrawText(3, 2.5, "Black text underlined in Blue.", "Arial", 15, pdf.TextAlignment.Left, Color.Black, Color.Blue, 0.001)
      .DrawText(3, 2.25, "Yellow text underlined in Green.", "Arial", 15, pdf.TextAlignment.Left, Color.Yellow, Color.Green, 0.001)
      .DrawText(3, 2, "Purple text underlined in Violet.", "Arial", 15, pdf.TextAlignment.Left, Color.Purple, Color.Violet, 0.001)

      .EndPage()

      .ClosePDFFile()

    End With

    Try
      System.Diagnostics.Process.Start("c:\Text.pdf")
    Catch ex As Exception
      Me.Cursor = Cursors.Default
      MessageBox.Show("Unable to display PDF Document 'c:\Text.pdf" & "'.'" & Environment.NewLine & Environment.NewLine & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    Me.Cursor = Cursors.Default
```

----------


## Ken B

This example display all fonts currently available in regular, bold, bold italic and italic styles.    



```
Me.Cursor = Cursors.WaitCursor

    Dim cPDF As New vbPDF

    With cPDF
      .ReportFileName = "c:\Fonts.pdf"
      .ScaleMode = pdf.ScaleMode.Inch
      .PaperSize = pdf.PaperSize._8_5x11
      .Author = "Ken Bradford"
      .Title = "Example 4 - Fonts"

      If Not .CreatePDFFile() Then
        Me.Cursor = Cursors.Default
        Exit Sub
      End If

      'Load all fonts.
      .LoadFont("Arial", pdf.FontName.Arial, pdf.FontStyle.Regular)
      .LoadFont("ArialBold", pdf.FontName.Arial, pdf.FontStyle.Bold)
      .LoadFont("ArialItalic", pdf.FontName.Arial, pdf.FontStyle.Italic)
      .LoadFont("ArialBoldItalic", pdf.FontName.Arial, pdf.FontStyle.BoldItalic)

      .LoadFont("Comic", pdf.FontName.ComicSansMS, pdf.FontStyle.Regular)
      .LoadFont("ComicBold", pdf.FontName.ComicSansMS, pdf.FontStyle.Bold)
      .LoadFont("ComicItalic", pdf.FontName.ComicSansMS, pdf.FontStyle.Italic)
      .LoadFont("ComicBoldItalic", pdf.FontName.ComicSansMS, pdf.FontStyle.BoldItalic)

      .LoadFont("Courier", pdf.FontName.CourierNew, pdf.FontStyle.Regular)
      .LoadFont("CourierBold", pdf.FontName.CourierNew, pdf.FontStyle.Bold)
      .LoadFont("CourierItalic", pdf.FontName.CourierNew, pdf.FontStyle.Italic)
      .LoadFont("CourierBoldItalic", pdf.FontName.CourierNew, pdf.FontStyle.BoldItalic)

      .LoadFont("LucidaConsole", pdf.FontName.LucidaConsole, pdf.FontStyle.Regular)
      .LoadFont("LucidaConsoleBold", pdf.FontName.LucidaConsole, pdf.FontStyle.Bold)
      .LoadFont("LucidaConsoleItalic", pdf.FontName.LucidaConsole, pdf.FontStyle.Italic)
      .LoadFont("LucidaConsoleBoldItalic", pdf.FontName.LucidaConsole, pdf.FontStyle.BoldItalic)

      .LoadFont("PalatinoLinotype", pdf.FontName.PalatinoLinotype, pdf.FontStyle.Regular)
      .LoadFont("PalatinoLinotypeBold", pdf.FontName.PalatinoLinotype, pdf.FontStyle.Bold)
      .LoadFont("PalatinoLinotypeItalic", pdf.FontName.PalatinoLinotype, pdf.FontStyle.Italic)
      .LoadFont("PalatinoLinotypeBoldItalic", pdf.FontName.PalatinoLinotype, pdf.FontStyle.BoldItalic)

      .LoadFont("SansSerif", pdf.FontName.MicrosoftSansSerif, pdf.FontStyle.Regular)
      .LoadFont("SansSerifBold", pdf.FontName.MicrosoftSansSerif, pdf.FontStyle.Bold)
      .LoadFont("SansSerifItalic", pdf.FontName.MicrosoftSansSerif, pdf.FontStyle.Italic)
      .LoadFont("SansSerifBoldItalic", pdf.FontName.MicrosoftSansSerif, pdf.FontStyle.BoldItalic)

      .LoadFont("TimesNewRoman", pdf.FontName.TimesNewRoman, pdf.FontStyle.Regular)
      .LoadFont("TimesNewRomanBold", pdf.FontName.TimesNewRoman, pdf.FontStyle.Bold)
      .LoadFont("TimesNewRomanItalic", pdf.FontName.TimesNewRoman, pdf.FontStyle.Italic)
      .LoadFont("TimesNewRomanBoldItalic", pdf.FontName.TimesNewRoman, pdf.FontStyle.BoldItalic)

      .LoadFont("Verdana", pdf.FontName.Verdana, pdf.FontStyle.Regular)
      .LoadFont("VerdanaBold", pdf.FontName.Verdana, pdf.FontStyle.Bold)
      .LoadFont("VerdanaItalic", pdf.FontName.Verdana, pdf.FontStyle.Italic)
      .LoadFont("VerdanaBoldItalic", pdf.FontName.Verdana, pdf.FontStyle.BoldItalic)

      .StartPage()
      .DrawText(0.25, 10, "Arial Regular 18", "Arial", 18, pdf.TextAlignment.Left, Color.Blue)
      .DrawText(0.25, 9.75, "Arial Bold 18", "ArialBold", 18, pdf.TextAlignment.Left, Color.Blue)
      .DrawText(0.25, 9.5, "Arial Italic 18", "ArialItalic", 18, pdf.TextAlignment.Left, Color.Blue)
      .DrawText(0.25, 9.25, "Arial Bold Italic 18", "ArialBoldItalic", 18, pdf.TextAlignment.Left, Color.Blue)

      .DrawText(0.25, 9, "Comic Regular 18", "Comic", 18, pdf.TextAlignment.Left, Color.Red)
      .DrawText(0.25, 8.75, "Comic Bold 18", "ComicBold", 18, pdf.TextAlignment.Left, Color.Red)
      .DrawText(0.25, 8.5, "Comic Italic 18", "ComicItalic", 18, pdf.TextAlignment.Left, Color.Red)
      .DrawText(0.25, 8.25, "Comic Bold Italic 18", "ComicBoldItalic", 18, pdf.TextAlignment.Left, Color.Red)

      .DrawText(0.25, 8, "Courier Regular 18", "Courier", 18, pdf.TextAlignment.Left, Color.Green)
      .DrawText(0.25, 7.75, "Courier Bold 18", "CourierBold", 18, pdf.TextAlignment.Left, Color.Green)
      .DrawText(0.25, 7.5, "Courier Italic 18", "CourierItalic", 18, pdf.TextAlignment.Left, Color.Green)
      .DrawText(0.25, 7.25, "Courier Bold Italic 18", "CourierBoldItalic", 18, pdf.TextAlignment.Left, Color.Green)

      .DrawText(0.25, 7, "Lucida Console Regular 18", "LucidaConsole", 18, pdf.TextAlignment.Left, Color.DarkOrange)
      .DrawText(0.25, 6.75, "Lucida Console Bold 18", "LucidaConsoleBold", 18, pdf.TextAlignment.Left, Color.DarkOrange)
      .DrawText(0.25, 6.5, "Lucida Console Italic 18", "LucidaConsoleItalic", 18, pdf.TextAlignment.Left, Color.DarkOrange)
      .DrawText(0.25, 6.25, "Lucida Console Bold Italic 18", "LucidaConsoleBoldItalic", 18, pdf.TextAlignment.Left, Color.DarkOrange)

      .DrawText(0.25, 6, "Palatino Linotype Regular 18", "PalatinoLinotype", 18, pdf.TextAlignment.Left, Color.Brown)
      .DrawText(0.25, 5.75, "Palatino Linotype Bold 18", "PalatinoLinotypeBold", 18, pdf.TextAlignment.Left, Color.Brown)
      .DrawText(0.25, 5.5, "Palatino Linotype Italic 18", "PalatinoLinotypeItalic", 18, pdf.TextAlignment.Left, Color.Brown)
      .DrawText(0.25, 5.25, "Palatino Linotype Bold Italic 18", "PalatinoLinotypeBoldItalic", 18, pdf.TextAlignment.Left, Color.Brown)

      .DrawText(0.25, 5, "Sans Serif Regular 18", "SansSerif", 18, pdf.TextAlignment.Left, Color.Violet)
      .DrawText(0.25, 4.75, "Sans Serif Bold 18", "SansSerifBold", 18, pdf.TextAlignment.Left, Color.Violet)
      .DrawText(0.25, 4.5, "Sans Serif Italic 18", "SansSerifItalic", 18, pdf.TextAlignment.Left, Color.Violet)
      .DrawText(0.25, 4.25, "Sans Serif Bold Italic 18", "SansSerifBoldItalic", 18, pdf.TextAlignment.Left, Color.Violet)

      .DrawText(0.25, 4, "Times New Roman Regular 18", "TimesNewRoman", 18, pdf.TextAlignment.Left, Color.DarkMagenta)
      .DrawText(0.25, 3.75, "Times New Roman Bold 18", "TimesNewRomanBold", 18, pdf.TextAlignment.Left, Color.DarkMagenta)
      .DrawText(0.25, 3.5, "Times New Roman Italic 18", "TimesNewRomanItalic", 18, pdf.TextAlignment.Left, Color.DarkMagenta)
      .DrawText(0.25, 3.25, "Times New Roman Bold Italic 18", "TimesNewRomanBoldItalic", 18, pdf.TextAlignment.Left, Color.DarkMagenta)

      .DrawText(0.25, 3, "Verdana Regular 18", "Verdana", 18, pdf.TextAlignment.Left, Color.CornflowerBlue)
      .DrawText(0.25, 2.75, "Verdana Bold 18", "VerdanaBold", 18, pdf.TextAlignment.Left, Color.CornflowerBlue)
      .DrawText(0.25, 2.5, "Verdana Italic 18", "VerdanaItalic", 18, pdf.TextAlignment.Left, Color.CornflowerBlue)
      .DrawText(0.25, 2.25, "Verdana Bold Italic 18", "VerdanaBoldItalic", 18, pdf.TextAlignment.Left, Color.CornflowerBlue)
      .EndPage()

      .ClosePDFFile()

    End With

    Try
      System.Diagnostics.Process.Start("c:\Fonts.pdf")
    Catch ex As Exception
      Me.Cursor = Cursors.Default
      MessageBox.Show("Unable to display PDF Document '" & "c:\Fonts.pdf" & "'.'" & Environment.NewLine & Environment.NewLine & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    Me.Cursor = Cursors.Default
```

----------


## Ken B

This example will show you how to draw lines.



```
Me.Cursor = Cursors.WaitCursor

    Dim i As Single = 0

    Dim cPDF As New vbPDF

    With cPDF
      .ReportFileName = "c:\Lines.pdf"
      .ScaleMode = pdf.ScaleMode.Inch
      .PaperSize = pdf.PaperSize._8_5x11
      .Author = "Ken Bradford"
      .Title = "Example 6 - Lines"

      If Not .CreatePDFFile() Then
        Me.Cursor = Cursors.Default
        Exit Sub
      End If

      .LoadFont("ArialBold", pdf.FontName.Arial, pdf.FontStyle.Bold)
      .LoadFont("X-O", pdf.FontName.CourierNew, pdf.FontStyle.Bold)

      .StartPage()

      .DrawLine(0.25, 10, 8.25, 10, 0.01, Color.DarkOliveGreen, pdf.LineCapStyle.Round)

      .DrawLine(1, 9.5, 7.5, 9.5, 0.05, Color.Red, pdf.LineCapStyle.Round)

      .DrawLine(2, 9, .PageWidthInInch - 2, 9, 0.1, Color.Tomato, pdf.LineCapStyle.ProjectingSquare)

      .DrawLine(1.5, 8.5, 1.5, 1, 0.2, Color.Blue, pdf.LineCapStyle.Squared)

      .DrawLine(3, 7, 3, 3, 0.3, Color.Green, pdf.LineCapStyle.Round)

      .DrawLine(4.5, 7, 7, 4.5, 0.3, Color.DarkMagenta, pdf.LineCapStyle.Round)
      .DrawLine(4.5, 4.5, 7, 7, 0.3, Color.DarkMagenta, pdf.LineCapStyle.Round)
      .EndPage()

      .StartPage()
      .DrawText(.PageWidthInInch / 2, .PageHeightInInch - 0.25, "1/4 Inch Grid", "ArialBold", 20, pdf.TextAlignment.Center, Color.Black)

      For i = 0.25 To .PageHeightInInch Step 0.25
        .DrawLine(0, i, .PageWidthInInch, i, 0.01, Color.Black, pdf.LineCapStyle.Squared)
      Next

      For i = 0.25 To .PageWidthInInch Step 0.25
        .DrawLine(i, 0, i, .PageHeightInInch, 0.01, Color.Blue, pdf.LineCapStyle.Squared)
      Next
      .EndPage()

      .StartPage()

      'Draw a 1/4 Inch Grid page.
      .DrawText(.PageWidthInInch / 2, .PageHeightInInch - 0.25, "1/2 Inch Grid", "ArialBold", 20, pdf.TextAlignment.Center, Color.Black)

      For i = 0.5 To .PageHeightInInch Step 0.5
        .DrawLine(0, i, .PageWidthInInch, i, 0.01, Color.Black, pdf.LineCapStyle.Squared)
      Next

      For i = 0.5 To .PageWidthInInch Step 0.5
        .DrawLine(i, 0, i, .PageHeightInInch, 0.01, Color.Blue, pdf.LineCapStyle.Squared)
      Next
      .EndPage()

      .StartPage()
      .DrawText(.PageWidthInInch / 2, .PageHeightInInch - 0.25, "Line Art", "ArialBold", 20, pdf.TextAlignment.Center, Color.Black)

      For i = 1 To 10
        .DrawLine(1, i, 7.5, .PageWidthInInch / 2, 0.02, Color.Olive, pdf.LineCapStyle.Squared)
      Next

      For i = 1 To 10
        .DrawLine(1.5, i, 7.5, .PageWidthInInch / 2, 0.02, Color.Green, pdf.LineCapStyle.Round)
      Next

      For i = 1 To 10
        .DrawLine(2, i, 7.5, .PageWidthInInch / 2, 0.02, Color.Red, pdf.LineCapStyle.Squared)
      Next

      For i = 1 To 10
        .DrawLine(2.5, i, 7.5, .PageWidthInInch / 2, 0.02, Color.Blue, pdf.LineCapStyle.Squared)
      Next

      For i = 1 To 10
        .DrawLine(3, i, 7.5, .PageWidthInInch / 2, 0.02, Color.Violet, pdf.LineCapStyle.Squared)
      Next

      For i = 1 To 10
        .DrawLine(3.5, i, 7.5, .PageWidthInInch / 2, 0.02, Color.DarkSeaGreen, pdf.LineCapStyle.Squared)
      Next

      For i = 1 To 10
        .DrawLine(4, i, 7.5, .PageWidthInInch / 2, 0.02, Color.Gold, pdf.LineCapStyle.Squared)
      Next
      .EndPage()

      'Tic Tac Toe.
      .StartPage()
      .DrawText(.PageWidthInInch / 2, .PageHeightInInch - 0.25, "Tic Tac Toe", "ArialBold", 20, pdf.TextAlignment.Center, Color.Black)

      'Lets play tic tac toe
      'Top line.
      .DrawLine(3, 8, 6, 8, 0.01, Color.Black)
      'Bottom line.
      .DrawLine(3, 7, 6, 7, 0.01, Color.Black)
      'Left line.
      .DrawLine(4, 6, 4, 9, 0.01, Color.Black)
      'Right line.
      .DrawLine(5, 6, 5, 9, 0.01, Color.Black)

      'X moves
      .DrawText(5.5, 8.2, "X", "X-O", 75, pdf.TextAlignment.Center, Color.Green)
      'O moves
      .DrawText(3.5, 8.2, "O", "X-O", 75, pdf.TextAlignment.Center, Color.Red)
      'X moves
      .DrawText(4.5, 7.2, "X", "X-O", 75, pdf.TextAlignment.Center, Color.Green)
      'O moves
      .DrawText(4.5, 6.2, "O", "X-O", 75, pdf.TextAlignment.Center, Color.Red)
      'X moves
      .DrawText(3.5, 6.2, "X", "X-O", 75, pdf.TextAlignment.Center, Color.Green)
      'X wins.
      .DrawLine(3, 6, 6, 9, 0.04, Color.Purple, pdf.LineCapStyle.Round)

      .EndPage()

      .ClosePDFFile()

    End With

    Try
      System.Diagnostics.Process.Start("c:\Lines.pdf")
    Catch ex As Exception
      Me.Cursor = Cursors.Default
      MessageBox.Show("Unable to display PDF Document 'c:\Lines.pdf" & "'.'" & Environment.NewLine & Environment.NewLine & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    Me.Cursor = Cursors.Default
```

----------


## mkinsel

Does this component allow you to load an existing .PDF file (e.g. a blank form), write text and graphics on top, and then save with a different file name (fill in a form)?

----------


## Guerrero

there is possible to change the orientation to landscape instead of portrait?
for example writing very long lines, or write in like powerpoint presentation.

including images will be great.

anyway, congrats! well done!

----------


## Mehmood Iqbal

Can you tell me please how can i insert Unicode characters in PDF file??

----------

