Skip to footer content
USING IRONPDF

How to Print a VB.NET Form to PDF

Let's be real: converting Windows Forms to PDF documents in VB.NET is a requirement that constantly comes up, but the .NET Framework just doesn't offer native PDF printing. You need a reliable way to generate PDF files from reports, save form data, or create printable documents.

Luckily, IronPDF offers a quick and streamlined solution. This tool lets you print forms to PDF files without the hassle of Adobe Reader installs or complex PDF printer setup. This complete article will show you how to do it in minutes.

Why Use IronPDF for Form-to-PDF File Conversion?

IronPDF is a comprehensive .NET PDF library that simplifies the process of converting Windows Forms and web forms (including ASPX pages) to PDF documents. Unlike traditional approaches that rely on PDF printers or complex drawing operations, IronPDF uses a Chrome rendering engine to generate PDF files with pixel-perfect accuracy from your VB .NET projects.

The library handles all aspects of PDF content creation, from rendering form controls to managing page layouts, making it ideal for both Windows Forms applications and ASP.NET web applications. With IronPDF's HTML to PDF conversion capabilities, developers can create professional documents efficiently. This powerful resource is key to speeding up development.

Installing IronPDF in Your VB.NET Project

Getting started with IronPDF takes just minutes. The simplest installation method uses the NuGet Package Manager in Visual Studio:

  1. Right-click your project in Solution Explorer
  2. Select "Manage NuGet Packages"
  3. Search for "IronPDF"
  4. Click Install to add the latest version

Alternatively, use the Package Manager Console with the following command:

Install-Package IronPdf

For detailed setup instructions, visit the IronPDF installation guide link. Once installed, add Imports IronPDF to start using the library's powerful features.

NuGet Install with NuGet

PM >  Install-Package IronPdf

Check out IronPDF on NuGet for quick installation. With over 10 million downloads, it’s transforming PDF development with C#. You can also download the DLL or Windows installer.

Converting a Windows Forms to PDF: Step-by-Step Code

The follow code example shows how to capture and convert a Windows Form to a new PDFDocument object:

Imports IronPdf
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
    Private Sub btnPrintToPDF_Click(sender As Object, e As EventArgs) Handles btnPrintToPDF.Click
        ' Capture the form as HTML content
        Dim htmlContent As String = GenerateFormHTML()
        ' Initialize IronPDF's ChromePdfRenderer instance
        Dim renderer As New ChromePdfRenderer()
        ' Configure rendering options for better output
        renderer.RenderingOptions.MarginTop = 10
        renderer.RenderingOptions.MarginBottom = 10
        renderer.RenderingOptions.MarginLeft = 10
        renderer.RenderingOptions.MarginRight = 10
        ' Generate PDF from HTML content
        Dim pdfDocument As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
        ' Save the PDF file
        Dim fileName As String = "FormOutput.pdf"
        pdfDocument.SaveAs(fileName)
        ' Optional: Open the generated PDF
        Process.Start(fileName)
    End Sub
    Private Function GenerateFormHTML() As String
        ' Build HTML representation of your form
        Dim html As New System.Text.StringBuilder()
        html.Append("<html><head>")
        html.Append("<style>")
        html.Append("body { font-family: Arial, sans-serif; }")
        html.Append("table { width: 100%; border-collapse: collapse; }")
        html.Append("td { padding: 8px; border: 1px solid #ddd; }")
        html.Append("</style>")
        html.Append("</head><body>")
        html.Append("<h1>Hello World</h1>")
        html.Append("<table>")
        ' Add form controls data
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                Dim textBox As TextBox = DirectCast(ctrl, TextBox)
                html.AppendFormat("<tr><td>{0}:</td><td>{1}</td></tr>", 
                                textBox.Name, textBox.Text)
            ElseIf TypeOf ctrl Is ComboBox Then
                Dim comboBox As ComboBox = DirectCast(ctrl, ComboBox)
                html.AppendFormat("<tr><td>{0}:</td><td>{1}</td></tr>", 
                                comboBox.Name, comboBox.Text)
            End If
        Next
        html.Append("</table>")
        html.Append("</body></html>")
        Return html.ToString()
    End Function
End Sub
Imports IronPdf
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form1
    Private Sub btnPrintToPDF_Click(sender As Object, e As EventArgs) Handles btnPrintToPDF.Click
        ' Capture the form as HTML content
        Dim htmlContent As String = GenerateFormHTML()
        ' Initialize IronPDF's ChromePdfRenderer instance
        Dim renderer As New ChromePdfRenderer()
        ' Configure rendering options for better output
        renderer.RenderingOptions.MarginTop = 10
        renderer.RenderingOptions.MarginBottom = 10
        renderer.RenderingOptions.MarginLeft = 10
        renderer.RenderingOptions.MarginRight = 10
        ' Generate PDF from HTML content
        Dim pdfDocument As PdfDocument = renderer.RenderHtmlAsPdf(htmlContent)
        ' Save the PDF file
        Dim fileName As String = "FormOutput.pdf"
        pdfDocument.SaveAs(fileName)
        ' Optional: Open the generated PDF
        Process.Start(fileName)
    End Sub
    Private Function GenerateFormHTML() As String
        ' Build HTML representation of your form
        Dim html As New System.Text.StringBuilder()
        html.Append("<html><head>")
        html.Append("<style>")
        html.Append("body { font-family: Arial, sans-serif; }")
        html.Append("table { width: 100%; border-collapse: collapse; }")
        html.Append("td { padding: 8px; border: 1px solid #ddd; }")
        html.Append("</style>")
        html.Append("</head><body>")
        html.Append("<h1>Hello World</h1>")
        html.Append("<table>")
        ' Add form controls data
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                Dim textBox As TextBox = DirectCast(ctrl, TextBox)
                html.AppendFormat("<tr><td>{0}:</td><td>{1}</td></tr>", 
                                textBox.Name, textBox.Text)
            ElseIf TypeOf ctrl Is ComboBox Then
                Dim comboBox As ComboBox = DirectCast(ctrl, ComboBox)
                html.AppendFormat("<tr><td>{0}:</td><td>{1}</td></tr>", 
                                comboBox.Name, comboBox.Text)
            End If
        Next
        html.Append("</table>")
        html.Append("</body></html>")
        Return html.ToString()
    End Function
End Sub
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code snippet demonstrates several key concepts. First, it captures form data by iterating through Windows Forms controls. Then, it builds an HTML representation with proper formatting using CSS styles. Finally, IronPDF's RenderUrlAsPdf method variant RenderHtmlAsPdf converts this HTML into a PDF document with professional formatting. The method handles all PDF content generation automatically, ensuring your forms are accurately represented in the output file with the specified file name. A similar approach is used when creating a new document from a web page or URL.

Example Windows Form UI

How to Print a VB.NET Form to PDF: Figure 1 - Example Windows Form output

Output PDF Document

How to Print a VB.NET Form to PDF: Figure 2 - Example PDF output

Alternative Method: Using Images for Complex Forms

For forms with complex graphics or custom drawing, you can capture the form as an image. The following code snippet shows this approach:

Private Sub PrintFormAsImage()
    ' Capture form as bitmap
    Dim bitmap As New Bitmap(Me.Width, Me.Height)
    Me.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.Width, Me.Height))
    ' Save bitmap to memory stream
    Dim ms As New System.IO.MemoryStream()
    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    ' Convert image to PDF using IronPDF
    Dim pdfDocument As PdfDocument = ImageToPdfConverter.ImageToPdf(ms.ToArray())
    pdfDocument.SaveAs("FormImage.pdf")
End Sub
Private Sub PrintFormAsImage()
    ' Capture form as bitmap
    Dim bitmap As New Bitmap(Me.Width, Me.Height)
    Me.DrawToBitmap(bitmap, New Rectangle(0, 0, Me.Width, Me.Height))
    ' Save bitmap to memory stream
    Dim ms As New System.IO.MemoryStream()
    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
    ' Convert image to PDF using IronPDF
    Dim pdfDocument As PdfDocument = ImageToPdfConverter.ImageToPdf(ms.ToArray())
    pdfDocument.SaveAs("FormImage.pdf")
End Sub
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This code provides an alternative approach for complex forms. It uses the DrawToBitmap method to capture the entire form as an image, preserving exact visual appearance including custom graphics and special controls. The ImageToPdfConverter class ensures high-quality conversion from PNG or other image formats to PDF. This provides a clear reference for complex form handling.

Printing PDF Documents Directly

Once you've generated your PDF file, IronPDF also supports direct printing:

' Print PDF to default printer
pdfDocument.Print()
' Print with specific settings
Dim printDoc As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument()
printDoc.PrinterSettings.PrinterName = "My Printer"
printDoc.Print()
' Print PDF to default printer
pdfDocument.Print()
' Print with specific settings
Dim printDoc As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument()
printDoc.PrinterSettings.PrinterName = "My Printer"
printDoc.Print()
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This sample code shows how to print PDF files directly without opening them. The first method sends the document to the default printer, while the second allows you to specify printer settings programmatically. For more details on printing PDF documents, check the IronPDF printing documentation.

Adding Professional Features

IronPDF enables you to enhance your PDF documents with professional features. This includes advanced editing options:

' Add headers and footers
renderer.RenderingOptions.TextHeader = New TextHeaderFooter() With {
    .CenterText = "Company Report",
    .DrawDividerLine = True
}
' Set page numbers on first page and beyond
renderer.RenderingOptions.TextFooter = New TextHeaderFooter() With {
    .RightText = "Page {page} of {total-pages}",
    .FontSize = 10
}
' Add headers and footers
renderer.RenderingOptions.TextHeader = New TextHeaderFooter() With {
    .CenterText = "Company Report",
    .DrawDividerLine = True
}
' Set page numbers on first page and beyond
renderer.RenderingOptions.TextFooter = New TextHeaderFooter() With {
    .RightText = "Page {page} of {total-pages}",
    .FontSize = 10
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

These features transform basic PDF files into professional documents. The library supports comprehensive customization of PDF content, from headers and footers to security settings. Learn more about advanced PDF features (note the customization options).

Output

How to Print a VB.NET Form to PDF: Figure 3 - Example PDF output with a custom header and footer

Common Troubleshooting Tips

When working with form-to-PDF conversion in .NET applications, keep these points in mind:

  • Ensure all required .NET Framework components are installed
  • For web applications (ASPX), verify IIS permissions for file system access
  • Use UTF-8 encoding for international characters in form data (argument)
  • Test rendering with different form sizes to ensure proper page layout
  • Store generated PDF files in an appropriate local copy directory. This happens on load end of the form data conversion

For additional support, consult the comprehensive IronPDF documentation or explore community solutions on Stack Overflow (post a question).

Conclusion

IronPDF transforms the complex task of printing forms to PDF into a straightforward process. Whether you're building Windows Forms applications or ASP.NET web forms, the library provides all the tools needed to generate PDF documents from your VB.NET projects. This is a powerful resource for any user.

The combination of HTML rendering capabilities and direct form capture methods gives developers flexibility in handling various form types and requirements. With support for advanced features like headers, footers, and security settings, IronPDF delivers a complete solution for PDF generation in .NET applications.

Ready to use IronPDF for your VB.NET print form to PDF tasks, or any other PDF workflows? Start with a free trial of IronPDF, or explore the comprehensive documentation and API reference to discover more features. For production deployments, licensing options start at $799.

Download IronPDF today and transform your Windows Forms into professional PDF documents with just a few lines of code.

Frequently Asked Questions

How can I convert Windows Forms to PDF using VB.NET?

You can convert Windows Forms to PDF in VB.NET by utilizing IronPDF, which offers a straightforward way to generate PDF files from your form data.

Does the .NET Framework support PDF printing natively?

No, the .NET Framework does not support native PDF printing. However, you can use IronPDF to easily convert and print PDF documents from Windows Forms.

What are the benefits of using IronPDF for printing forms?

IronPDF simplifies the process of generating PDFs from Windows Forms, offering features like code examples, an installation guide, and robust troubleshooting support to ensure smooth PDF creation.

Can IronPDF handle complex form data in VB.NET?

Yes, IronPDF is designed to handle complex form data, allowing you to generate accurate and high-quality PDF documents from your VB.NET applications.

Is there a tutorial available for learning to convert forms to PDF with VB.NET?

Yes, the VB.NET Print Form to PDF Developer Guide available on the IronPDF website provides a comprehensive tutorial, including code examples and troubleshooting tips.

What should I do if I encounter issues while converting forms to PDF using IronPDF?

The IronPDF Developer Guide includes troubleshooting tips to help you resolve common issues encountered during the conversion of forms to PDF.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More