VB.NET PDF Creator (Code Example Tutorial)

This tutorial will guide you step-by-step how to create and edit PDF files in VB.NET. This technique is equally valid for use in ASP.NET web apps as well as console applications, Windows Services , and desktop programs. We will use VB.NET to create PDF projects targeting .NET Framework 4 or .NET Core 2. All you need is a Visual Basic .NET development environment, such as Microsoft Visual Studio Community.

To see how to use IronPDF with C# see this guide.

To see how to use IronPDF with F# see this guide.


Overview

VB .NET Codes for PDF Creating and Editing with IronPDF

Render HTML to PDF with VB.NET, apply styling, utilize dynamic content, and edit your files easily. Creating PDFs is straightforward and compatible with .NET Framework 4, .NET Core 3.1, .NET 6 & 5. And no need for proprietary file formats or pulling different API's.

This tutorial provides the documentation to walk you through each task step-by-step, all using the free for development IronPDF software favored by developers. VB.NET code examples are specific to your use cases so you can see the steps easily in a familiar environment. This VB dot NET PDF Library has comprehensive creation and settings capabilities for every project, whether in ASP.NET applications, console, or desktop.

Included with IronPDF:

  • Ticket support direct from our .NET PDF Library development team (real humans!)
  • Works with HTML, ASPX forms, MVC views, images, and all the document formats you already use
  • Microsoft Visual Studio installation gets you up and running fast
  • Unlimited free development, and licenses to go live starting at $749

Step 1

1. Download the VB .NET PDF Library FREE from IronPDF

C# NuGet Library for PDF

Install with NuGet

Install-Package IronPdf
or
C# PDF DLL

Download DLL

Download DLL

Manually install into your project

Install via NuGet

In Visual Studio, right click on your project solution explorer and select "Manage NuGet Packages...". From there simply search for IronPDF and install the latest version... click ok to any dialog boxes that come up.

This will work in any C# .NET Framework project from Framework 4 and above, or .NET Core 2 and above. It will also work just as well in VB.NET projects.

Install-Package IronPdf

https://www.nuget.org/packages/IronPdf

Install via DLL

Alternatively, the IronPDF DLL can be downloaded and manually installed to the project or GAC from https://ironpdf.com/packages/IronPdf.zip

Remember to add this statement to the top of any cs class file using IronPDF:

  using IronPdf;

How to Tutorials

2. Create a PDF with VB.NET

Using Visual Basic ASP.NET to create a PDF file for the first time is surprising easy using IronPDF, as compared to libraries with proprietary design API’s such as iTextSharp.

We can use HTML (with a pixel perfect rending engine based on Google Chromium) to define the content of our PDF and simply render it to a file.

Here is our simplest code to create a PDF in VB.NET:

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-1.cs
Module Module1
    Sub Main()
        Dim renderer = New ChromePdfRenderer()
        Dim document = renderer.RenderHtmlAsPdf("<h1> My First PDF in VB.NET</h1>")
        document.SaveAs("MyFirst.pdf")
    End Sub
End Module
VB.NET

This will produce a .NET generated PDF file containing your exact text, albeit lacking some design at this point.

We can improve upon this code by adding the header line Imports IronPdf. By adding the last line of code System.Diagnostics.Process.Start, we open the PDF in the operating system's default PDF viewer to make the project more meaningful.

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-2.cs
Imports IronPdf

Module Module1
    Sub Main()
        Dim renderer = New ChromePdfRenderer()
        Dim document = renderer.RenderHtmlAsPdf("<h1> My First PDF in VB.NET</h1>")
        document.SaveAs("MyFirst.pdf")
        System.Diagnostics.Process.Start("MyFirst.pdf")
    End Sub
End Module
VB.NET

An alternative method would be to render any existing web page from a URL to a PDF by using the elegant “RenderUrlAsPdf” method from IronPDF.

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-3.cs
Imports IronPdf

Module Module1
    Sub Main()
        Dim renderer = New ChromePdfRenderer()
        Dim document = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf/")
        document.SaveAs("UrlToPdf.pdf")
        System.Diagnostics.Process.Start("UrlToPdf.pdf")
    End Sub
End Module
VB.NET

If you'd like to generate your PDF in PDF/A format, you'll need to render in IronPDF first, then use Ghostscript to convert to PDF/A.


3. Apply Styling to VB.NET PDF

To style our PDF content in VB.NET, we can make full use of CSS, JavaScript and images. We may link to local assets, or even to remote or CDN based assets such as Google Fonts. We can even use DataURIs to embed images and assets as a string into your HTML.

For advanced design, we can use a 2 stage process:

  1. First we develop and design our HTML perfectly. This task may involve in-house design staff, splitting the work load.
  2. Render that file as a PDF using VB.NET and our PDF Library

The VB.NET Code to render the HTML file as a PDF:

This method renders an HTML document as if it were opened as a file (file:// protocol).

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-4.cs
Imports IronPdf

Module Module1
    Sub Main()
        Dim renderer = New ChromePdfRenderer()
        renderer.RenderingOptions.CssMediaType = Rendering.PdfCssMediaType.Print
        renderer.RenderingOptions.PrintHtmlBackgrounds = False
        renderer.RenderingOptions.PaperOrientation = Rendering.PdfPaperOrientation.Landscape
        renderer.RenderingOptions.WaitFor.RenderDelay(150)
        Dim document = renderer.RenderHtmlFileAsPdf("C:\Users\jacob\Dropbox\Visual Studio\Tutorials\VB.Net.Pdf.Tutorial\VB.Net.Pdf.Tutorial\slideshow\index.html")
        document.SaveAs("Html5.pdf")
        System.Diagnostics.Process.Start("Html5.pdf")
    End Sub
End Module
VB.NET

We might also shorten that URL by adding a project relative file path such as:

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-5.cs
Dim document = renderer.RenderHtmlFileAsPdf("..\..\slideshow\index.html")
VB.NET

You can see that the HtmlToPdf renderer has a RenderingOptions property which we can use in this example to:

  • Set the CSS media type to 'print' so we see no screen-only CSS3 styles
  • Ignore HTML backgrounds
  • Set the PDF's virtual paper to Landscape orientation
  • Add a small delay in rendering for the JavaScript to finish processing

Our example HTML File uses JavaScript, CSS3 and images. This HTML creates a dynamic, mobile-aware slideshow and was found at https://github.com/leemark/better-simple-slideshow

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-6.cs
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>A simple DIY responsive slideshow made with HTML5, CSS3, and JavaScript</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link href='http://fonts.googleapis.com/css?family=Open+Sans|Open+Sans+Condensed:700' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="demo/css/demostyles.css">
        <link rel="stylesheet" href="css/simple-slideshow-styles.css">
    </head>
    <body>
        <!--[if lt IE 8]>
            <p class="browsehappy">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p>
        <![endif]-->
        <header>
            <h1>A Better Simple Slideshow</h1>
            <p><span class="desc">A simple DIY responsive JavaScript slideshow.</span> [<a href="https://github.com/leemark/better-simple-slideshow">GitHub<span> repo</span></a>]</p>
        </header>
        <div class="bss-slides num1" tabindex="1" autofocus="autofocus">
            <figure>
              <img src="demo/img/medium.jpg" width="100%" /><figcaption>"Medium" by <a href="https://www.flickr.com/photos/thomashawk/14586158819/">Thomas Hawk</a>.</figcaption>
            </figure>
            <figure>
              <img src="demo/img/colorado.jpg" width="100%" /><figcaption>"Colorado" by <a href="https://www.flickr.com/photos/stuckincustoms/88370744">Trey Ratcliff</a>.</figcaption>
            </figure>
            <figure>
              <img src="demo/img/monte-vista.jpg" width="100%" /><figcaption>"Early Morning at the Monte Vista Wildlife Refuge, Colorado" by <a href="https://www.flickr.com/photos/davesoldano/8572429635">Dave Soldano</a>.</figcaption>
            </figure>
            <figure>
              <img src="demo/img/sunrise.jpg" width="100%" /><figcaption>"Sunrise in Eastern Colorado" by <a href="https://www.flickr.com/photos/35528040@N04/6673031153">Pam Morris</a>.</figcaption>
            </figure>
            <figure>
              <img src="demo/img/colorado-colors.jpg" width="100%" /><figcaption>"colorado colors" by <a href="https://www.flickr.com/photos/cptspock/2857543585">Jasen Miller</a>.</figcaption>
            </figure>
        </div> <!-- // bss-slides -->
<div class="content">
<h2>What is it?</h2>
<p>It's a fairly basic slideshow, written in javascript. This is a dual-purpose project, it's meant to be something you can drop right into your page and use if you so choose, but it's also meant as an example/tutorial script showing how to build a simple DIY slideshow from scratch on your own. <a href="http://themarklee.com/2014/10/05/better-simple-slideshow/">Here is a tutorial/walkthrough</a>.</p>
<h2>Features</h2>
<ul>
    <li>fully responsive</li>
    <li>option for auto-advancing slides, or manually advancing by user</li>
    <li>multiple slideshows per-page</li>
    <li>supports arrow-key navigation</li>
    <li>full-screen toggle using HTML5 fullscreen api</li>
    <li>swipe events supported on touch devices (requires <a href="https://github.com/hammerjs/hammer.js">hammer.js</a>)</li>
    <li>written in vanilla JS--this means no jQuery dependency (much &hearts; for <a href="https://github.com/jquery/jquery">jQuery</a> though!)</li>
</ul>
<h2>Getting Started</h2>
<ol>
<li><p>HTML markup for the slideshow should look basically like this, with a container element wrapping the whole thing (doesn't have to be a <span class="code">&lt;div&gt;</span>) and each slide is a <span class="code">&lt;figure&gt;</span>.</p>
<script src="https://gist.github.com/leemark/83571d9f8f0e3ad853a8.js"></script> </li>
<li>Include the script: <span class="code">js/better-simple-slideshow.min.js</span> or <span class="code">js/better-simple-slideshow.js</span></li>
<li>Include the stylesheet <span class="code">css/simple-slideshow-styles.css</span></li>
<li>Initialize the slideshow:
<script src="https://gist.github.com/leemark/479d4ecc4df38fba500c.js"></script>
</li>
</ol>
<h2>Options</h2>
To customize functionality, create an options object, then pass it into <span class="code">makeBSS()</span> as the second argument, as seen below:
<script src="https://gist.github.com/leemark/c6e0f5c47acb7bf9be16.js"></script>
<h2>Demo/Examples</h2>
    <h3>Example #1 (slideshow at top of this page)</h3>
    <p>HTML markup:</p>
    <script src="https://gist.github.com/leemark/19bafdb1abf8f6b4e147.js"></script>
    <p>JavaScript code:</p>
    <script src="https://gist.github.com/leemark/a09d2726b5bfc92ea68c.js"></script>
    <h3>Example #2 (below)</h3>
        <div class="bss-slides num2" tabindex="2">
           <figure>
              <img src="http://themarklee.com/wp-content/uploads/2013/12/snowying.jpg" width="100%" /><figcaption>"Snowying" by <a href="http://www.flickr.com/photos/fiddleoak/8511209344/">fiddleoak</a>.</figcaption>
           </figure>
            <figure>
                <img src="http://themarklee.com/wp-content/uploads/2013/12/starlight.jpg" width="100%" /><figcaption>"Starlight" by <a href="http://www.flickr.com/photos/chaoticmind75/10738494123/in/set-72157626146319517">ChaoticMind75</a>.</figcaption>
           </figure>
           <figure>
              <img src="http://themarklee.com/wp-content/uploads/2013/12/snowstorm.jpg" width="100%" /><figcaption>"Snowstorm" by <a href="http://www.flickr.com/photos/tylerbeaulawrence/8539457508/">Beaulawrence</a>.</figcaption>
           </figure>
            <figure>
              <img src="http://themarklee.com/wp-content/uploads/2013/12/misty-winter-afternoon.jpg" width="100%" /><figcaption>"Misty winter afternoon" by <a href="http://www.flickr.com/photos/22746515@N02/5277611659/">Bert Kaufmann</a>.</figcaption>
           </figure>
            <figure>
              <img src="http://themarklee.com/wp-content/uploads/2013/12/good-morning.jpg" width="100%" /><figcaption>"Good Morning!" by <a href="http://www.flickr.com/photos/frank_wuestefeld/4306107546/">Frank Wuestefeld</a>.</figcaption>
           </figure>
        </div> <!-- // bss-slides -->
<p>HTML markup:</p>
<script src="https://gist.github.com/leemark/de90c78cb73673650a5a.js"></script>
<p>JavaScript code:</p>
<script src="https://gist.github.com/leemark/046103061c89cdf07e4a.js"></script>
</div> <!-- // content -->
<footer>Example photos are property of their respective owners, all code is <a href="https://github.com/leemark/better-simple-slideshow/blob/gh-pages/LICENSE">freely licensed for your use</a>. <br>Made especially for you by <a href="http://themarklee.com">Mark Lee</a> aka <a href="http://twitter.com/@therealmarklee">@therealmarklee</a> <br><span>&#9774; + &hearts;</span></footer>
<script src="demo/js/hammer.min.js"></script><!-- for swipe support on touch interfaces -->
<script src="js/better-simple-slideshow.min.js"></script>
<script>
var opts = {
    auto : {
        speed : 3500,
        pauseOnHover : true
    },
    fullScreen : false,
    swipe : true
};
makeBSS('.num1', opts);
var opts2 = {
    auto : false,
    fullScreen : true,
    swipe : true
};
makeBSS('.num2', opts2);
</script>
</body>
</html>
HTML

As you can see, the full 'kitchen sink' of HTML web page capabilities are used in this example. The rendering is performed internally by IronPDF using the Chromium HTML engine and v8 javascript engine from Google. They do not need to be installed in your system, the entire package is automatically added to your project when you use IronPDF.

3.1. Add Headers and Footers

As we have a beautiful PDF render working, we may now wish to add attractive headers and footers.

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-7.cs
Imports IronPdf
Imports IronSoftware.Drawing

Module Module1
    Sub Main()
        Dim renderer = New ChromePdfRenderer()
        renderer.RenderingOptions.CssMediaType = Rendering.PdfCssMediaType.Print
        renderer.RenderingOptions.PrintHtmlBackgrounds = False
        renderer.RenderingOptions.PaperOrientation = Rendering.PdfPaperOrientation.Landscape
        renderer.RenderingOptions.WaitFor.RenderDelay(150)
        renderer.RenderingOptions.TextHeader.CenterText = "VB.NET PDF Slideshow"
        renderer.RenderingOptions.TextHeader.DrawDividerLine = True
        renderer.RenderingOptions.TextHeader.FontSize = "13"
        renderer.RenderingOptions.TextFooter.RightText = "page {page} of {total-pages}"
        renderer.RenderingOptions.TextFooter.Font = FontTypes.Arial
        renderer.RenderingOptions.TextFooter.FontSize = "9"
        Dim document = renderer.RenderHtmlFileAsPdf("..\..\slideshow\index.html")
        document.SaveAs("Html5WithHeader.pdf")
        System.Diagnostics.Process.Start("Html5WithHeader.pdf")
    End Sub
End Module
VB.NET

There is support for logical headers and footers as shown. You may also add HTML based headers and footers as described in the VB.NET PDF developer API reference online

You can download and explore the source code for this "VB.NET HTML to PDF" project as a VB.NET Visual Studio project


4. Create PDF w/ Dynamic Content : 2 Methods

Historically, PDF 'templating' has been an overwhelming task for Software Engineers. Stamping content into PDF templates rarely works. This is because each case or report will contain content of varying types and length. Fortunately, HTML is exceptionally good at handling Dynamic Data.

For this we have 2 ways forward:

  1. String Templating of HTML then conversion to PDF using .NET
  2. Rendering out content as an ASP.NET Web Page and then rendering the page as a PDF

4.1. Method 1 - ASP.NET - ASPX to PDF using VB.NET Web Forms

Fortunately this solution is surprisingly simple. Any flavor of .NET Web Form (including Razor) can be rendered into a PDF document using this VB.NET code in the Page_Load subroutine in the VB.NET code behind.

The PDF document may be set with a content-disposition to display in-browser, or to act as a file download.

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-8.cs
Imports IronPdf

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
    Dim PdfOptions = New IronPdf.ChromePdfRenderOptions()
    IronPdf.AspxToPdf.RenderThisPageAsPDF(AspxToPdf.FileBehavior.Attachment, "MyPdf.pdf", PdfOptions)
End Sub
VB.NET

4.2. Method 2 - HTML to PDF with String Templating

To create dynamic PDF documents that include instance specific data, we simply create a HTML string to match the data we wish to render as a PDF.

This is probably the largest advantage of the HTML-to-PDF solution in VB.NET - the ability to easily and intuitively create dynamic PDF documents and reports by creating HTML 'on the fly.'

The simplest version of this is the String.Format method from VB.NET

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-9.cs
Imports IronPdf

Module Module1
    Sub Main()
        Dim renderer = New ChromePdfRenderer()
        Dim Html = "Hello {0}"
        String.Format(Html, "World")
        Dim document = renderer.RenderHtmlAsPdf(Html)
        document.SaveAs("HtmlTemplate.pdf")
        System.Diagnostics.Process.Start("HtmlTemplate.pdf")
    End Sub
End Module
VB.NET

As PDFs get more complicated, the String will get more complicated. We might consider using a String Builder, or even a templating framework such as HandleBars.Net or Razor https://github.com/rexm/Handlebars.Net


5. Edit PDF Files with VB.NET

IronPDF for VB.NET also allows PDF documents to be edited, encrypted, watermarked or even turned back into plain text:

5.1. Merging Multiple PDF Files into One Document in VB

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-10.cs
Dim pdfs = New List(Of PdfDocument)
pdfs.Add(PdfDocument.FromFile("A.pdf"))
pdfs.Add(PdfDocument.FromFile("B.pdf"))
pdfs.Add(PdfDocument.FromFile("C.pdf"))
Dim mergedPdf As PdfDocument = PdfDocument.Merge(pdfs)
mergedPdf.SaveAs("merged.pdf")
mergedPdf.Dispose()
For Each pdf As PdfDocument In pdfs
    pdf.Dispose()
Next
VB.NET

5.2. Add a Cover Page to the PDF

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-11.cs
pdf.PrependPdf(renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"))
VB.NET

5.3. Remove the last page from the PDF

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-12.cs
pdf.RemovePage((pdf.PageCount - 1))
VB.NET

5.4. Encrypt a PDF using 128 Bit Encryption

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-13.cs
// Save with a strong encryption password.
pdf.Password = "my.secure.password";
pdf.SaveAs("secured.pdf")
VB.NET

5.5. Stamp Additional HTML Content Onto a Page in VB

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-14.cs
Imports IronPdf
Imports IronPdf.Editing

Module Module1
    Sub Main()
        Dim renderer = New ChromePdfRenderer
        Dim pdf = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
        Dim stamp = New HtmlStamper()
        stamp.Html = "<h2>Completed</h2>"
        stamp.Opacity = 50
        stamp.Rotation = -45
        stamp.VerticalAlignment = VerticalAlignment.Top
        stamp.VerticalOffset = New Length(10)
        pdf.ApplyStamp(stamp)
        pdf.SaveAs("C:\Path\To\Stamped.pdf")
    End Sub
End Module
VB.NET

5.6. Add Page Break to PDF Using HTML

The easiest way to do this is with HTML and CSS

:path=/static-assets/pdf/content-code-examples/how-to/vb-net-pdf-15.cs
<div style='page-break-after: always;'>&nbsp;</div>
HTML

6. More .NET PDF Tutorials

You may also be interested in:


Conclusion

In this tutorial we discovered 6 ways to achieve VB.NET to PDF results using VB.NET as our programming language of choice.

  • HTML string to PDF
  • Creating a PDF in VB.NET using an HTML string to define its content
  • Rendering existing URLs as PDF files
  • Generating PDF from HTML files
  • HTML templating in VB.NET and conversion to dynamic PDFs
  • Converting ASP.NET pages with live data, such as ASPX to PDF files

For each we used the popular IronPDF VB.NET library to allow us to turn HTML directly into PDF documents within .NET projects


Tutorial Quick Access

Download this Tutorial as Source Code

The full free VB.NET HTML to PDF Source Code for this tutorial is available to download as a zipped Visual Studio project file.

Download

Explore this Tutorial on GitHub

You may also be interested in our extensive library of VB.NET PDF generation and manipulation examples on GitHub. Exploring source code is the fastest way to learn, and Github is the definitive way to do so online. I hope these examples help you get to grips with PDF related functionality in your VB projects.

Creating PDFS in ASP.NET with VB.NET and C# Source A Simple Hello World Project to Render HTML to PDF in VB.NET using IronPDF Exploring HTML To PDF in-depth with VB.NET

Download C# PDF Quickstart guide

To make developing PDFs in your .NET applications easier, we have compiled a quick-start guide as a PDF document. This "Cheat-Sheet" provides quick access to common functions and examples for generating and editing PDFs in C# and VB.NET - and will save time getting started using IronPDF in your .NET project.

Download

View the API Reference

Explore the API Reference for IronPDF, outlining the details of all of IronPDF’s features, namespaces, classes, methods fields and enums.

View the API Reference