Photo by Arif Riyanto
Originally Posted On: https://ironpdf.com/tutorials/vb-net-pdf/
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.
Code Examples
Create PDF in VB
- Module Module1
- Sub Main()
- Dim renderer = New IronPdf.HtmlToPdf()
- Dim document = renderer.RenderHtmlAsPdf(“
My First PDF in VB.Net
”)
- document.SaveAs(“MyFirst.pdf”)
- End Sub
- End Module
Jump to Article Try IronPDF free for development
How to Generate and Edit PDF Files in VB.NET:
- Download the IronPDF C# PDF Library
- Create a PDF with VB.NET
- Apply Styling to a VB.NET PDF Doc
- Create a PDF with Dynamic Content
- Edit PDF Files, Add a Cover Page, Encrypt, Use Stamping, and more
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 or .NET Core 2. 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 $399
Step 1
1. Download the VB .NET PDF Library FREE from IronPDF
Download DLL
Manually install into your project
or
Install with NuGet
Install-Package IronPdf nuget.org/packages/IronPdf/
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.
PM > 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 http://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:
- Module Module1
- Sub Main()
- Dim renderer = New IronPdf.HtmlToPdf()
- Dim document = renderer.RenderHtmlAsPdf(“
My First PDF in VB.Net
”)
- 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.
- Imports IronPdf
- Module Module1
- Sub Main()
- Dim renderer = New HtmlToPdf()
- Dim document = renderer.RenderHtmlAsPdf(“
My First PDF in VB.Net
”)
- 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.
- Imports IronPdf
- Module Module1
- Sub Main()
- Dim renderer = New HtmlToPdf()
- 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:
- First we develop and design our HTML perfectly. This task may involve in-house design staff, splitting the work load.
- 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).
- Imports IronPdf
- Module Module1
- Sub Main()
- Dim renderer = New HtmlToPdf()
- renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print
- renderer.PrintOptions.PrintHtmlBackgrounds = False
- renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Landscape
- renderer.PrintOptions.RenderDelay = 150
- Dim document = renderer.RenderHTMLFileAsPdf(“C:UsersjacobDropboxVisual StudioTutorialsVB.Net.Pdf.TutorialVB.Net.Pdf.Tutorialslideshow”)
- 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:
- Dim document = renderer.RenderHTMLFileAsPdf(“….slideshow”)
VB.NET
You can see that the HtmlToPdf renderer has a PrintOptions object 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
A simple DIY responsive slideshow made with HTML5, CSS3, and JavaScript -
You are using an outdated browser. Please upgrade your browser to improve your experience.
-
-
A Better Simple Slideshow
-
A simple DIY responsive JavaScript slideshow. [GitHub repo]
-
-
What is it?
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. Here is a tutorial/walkthrough.
Features
- fully responsive
- option for auto-advancing slides, or manually advancing by user
- multiple slideshows per-page
- supports arrow-key navigation
- full-screen toggle using HTML5 fullscreen api
- swipe events supported on touch devices (requires hammer.js)
- written in vanilla JS–this means no jQuery dependency (much ♥ for jQuery though!)
Getting Started
HTML markup for the slideshow should look basically like this, with a container element wrapping the whole thing (doesn’t have to be a <div>) and each slide is a <figure>.
- Include the script: js/better-simple-slideshow.min.js or js/better-simple-slideshow.js
- Include the stylesheet css/simple-slideshow-styles.css
- Initialize the slideshow:
Options
- To customize functionality, create an options object, then pass it into makeBSS() as the second argument, as seen below:
Demo/Examples
Example #1 (slideshow at top of this page)
HTML markup:
JavaScript code:
Example #2 (below)
HTML markup:
JavaScript code:
- 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);
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.
- Imports IronPdf
- Module Module1
- Sub Main()
- Dim renderer = New HtmlToPdf()
- renderer.PrintOptions.CssMediaType = PdfPrintOptions.PdfCssMediaType.Print
- renderer.PrintOptions.PrintHtmlBackgrounds = False
- renderer.PrintOptions.PaperOrientation = PdfPrintOptions.PdfPaperOrientation.Landscape
- renderer.PrintOptions.RenderDelay = 150
- renderer.PrintOptions.Header.CenterText = “VB.Net PDF Slideshow”
- renderer.PrintOptions.Header.DrawDividerLine = True
- renderer.PrintOptions.Header.FontSize = “13”
- renderer.PrintOptions.Footer.RightText = “page {page} of {total-pages}”
- renderer.PrintOptions.Footer.FontFamily = “Arial”
- renderer.PrintOptions.Footer.FontSize = “9”
- Dim document = renderer.RenderHTMLFileAsPdf(“….slideshow”)
- 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 object 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:
- String Templating of HTML then conversion to PDF using .NET
- 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.
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
- Dim PdfOptions As PdfPrintOptions = New IronPdf.PdfPrintOptions
- PdfPrintOptions.
- IronPdf.AspxToPdf.RenderThisPageAsPDF(AspxToPdf.FileBehaviour.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
- Imports IronPdf
- Module Module1
- Sub Main()
- Dim renderer = New HtmlToPdf()
- 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
- Dim Renderer As var = New IronPdf.HtmlToPdf
- Dim PDFs As var = New List(Of PdfDocument)
- PDFs.Add(PdfDocument.FromFile(“A.pdf”))
- PDFs.Add(PdfDocument.FromFile(“B.pdf”))
- PDFs.Add(PdfDocument.FromFile(“C.pdf”))
- Dim PDF As PdfDocument = PdfDocument.Merge(PDFs)
- PDF.SaveAs(“merged.pdf”)
VB.NET
5.2. Add a Cover Page to the PDF
- PDF.PrependPdf(Renderer.RenderHtmlAsPdf(“
Cover Page
”))
VB.NET
5.3. Remove the last page from the PDF
- PDF.RemovePage((PDF.PageCount – 1))
VB.NET
5.4. Encrypt a PDF using 128 Bit Encryption
- // 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
- Imports IronPdf
- Module Module1
- Sub Main()
- Dim Renderer As IronPdf.HtmlToPdf = New IronPdf.HtmlToPdf
- Dim pdf = Renderer.RenderUrlAsPdf(“https://www.nuget.org/packages/IronPdf”)
- Dim stamp = New HtmlStamp()
- stamp.Html = “
Completed
”
- stamp.Opacity = 50
- stamp.Rotation = -45
- stamp.Top = 10
- pdf.StampHTML(stamp)
- pdf.SaveAs(“C:PathToStamped.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
HTML
6. More .NET PDF Tutorials
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 library for VB.NET to allow us to turn HTML directly into PDF documents within .NET projects