How to Print PDF in VB.NET (Step-by-Step) Tutorial

This article describes the process on how to print URL page with images as PDF pages/document at runtime system in ASP.NET Web Application using VB.NET. We will use the IronPDF library for printing PDF documents through URL in the ASP.NET Web Application with Web/Windows forms or pages using default printer. 

About the IronPDF library

IronPDF is a C# and VB.NET PDF library that allows you to print URL as PDF file/document without using Adobe Acrobat Reader and default printer. This product offers the new system solution for developers who wish to print URL content into a PDF file/pages and then print PDF with print preview note. IronPDF can be used with ASP.NET Web Application or Windows forms to generate PDF documents and print PDF files in a way that HTML pages are built without using default printer. It allows programmers the ability to create PDF files and documents from scratch in ASP.NET Web Applications without having any prior knowledge of Adobe Acrobat or other third-party application tools. Let’s see how we can use it in the ASP.NET Web Application for PDF printing/drawing through URL. You can also use Windows forms in .NET application for converting URL into PDF documents and then print PDF documents using printer.

It also allows developers to generate, edit, extract, and print PDF documents content within .NET Core and Framework projects as .NET applications. Using IronPDF library, we can print URL as PDF document.

Create an ASP.NET Web Application for Generating PDF Files

Follow the following steps for creating ASP.NET Web Application.

  • Open Visual Studio and click the “Create New Project” button.
  • From the template list, select ASP.NET Web Application.
  • Give a proper name to your application.
  • Select a .NET Framework and click on ‘Create’ button.

Learn how to work with IronPDF using VB.NET by reading this tutorial.

Installing the IronPDF library

There are multiple ways to install the IronPDF library.

  • Using NuGet Command Line
  • Using NuGet GUI
  • Download from NuGet Website

The easiest way to install IronPDF is using the NuGet Command Line. Go to the NuGet Command Line, write the following statement, and press the Enter button.

Install-Package IronPrint

IronPDF installation will start. After installation, you will be able to use IronPDF file in your project. For confirmation that IronPDF file has been installed, expand the Dependencies section in Solution Explorer and click on the Packages section. You will see the IronPDF file package there.

Now, our project is ready to use IronPDF. Let’s see how we can use it in the ASP.NET Web Application with Web forms or pages.

Using IronPDF in ASP.NET Web Application with Web forms

We will add a button for printing PDF files. For example, we will write the following code snippet on the “Default.aspx” page for that purpose which is the default filename and first page or web form of application. Check the below sample code.

<div>       
 <asp:Button ID="Button1" runat="server" Text="Generate PDF" OnClick="Button1_Click" /> 
</div>
<div>       
 <asp:Button ID="Button1" runat="server" Text="Generate PDF" OnClick="Button1_Click" /> 
</div>
HTML

First, in the above code, a div is added with a asp:Button (server control) in it. The text on the button has been set to read “Generate PDF.” There is also a “OnClick event defined on the button.

Print Web Page Content as PDF

We have added a button for generating PDF document. Now, it's time to write a post-action to generate a PDF file. After adding a OnClick event in ‘Default.aspx’ page, it automatically added an event Button1_Click in ‘Default.aspx.cs’ file.

Include the following import statements at the top of the Default.aspx.cs source file:

Imports IronPDF
Imports System.Drawing.Printing
Imports IronPDF
Imports System.Drawing.Printing
VB.NET

Enter the following code snippet in the source file that we opened previously for the Button1_Click event:

Public Class _Default
    Inherits Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs)

'Creating an object named document of ChromePdfRenderer class
Dim document = New ChromePdfRenderer()

'Create new PdfDocument PDF and render URL into PDF document
        Dim PDF As PdfDocument = document.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
 PDF.SaveAs("UrlToPdf.pdf")
'Print PDF in 300 DPI without user new printdialog
        PDF.Print(300, False)

'For advance printing, you can use below 
        Dim PrintDocYouCanWorkWith As PrintDocument = PDF.GetPrintDocument()   
End Sub
End Class 
Public Class _Default
    Inherits Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub
    Protected Sub Button1_Click(sender As Object, e As EventArgs)

'Creating an object named document of ChromePdfRenderer class
Dim document = New ChromePdfRenderer()

'Create new PdfDocument PDF and render URL into PDF document
        Dim PDF As PdfDocument = document.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
 PDF.SaveAs("UrlToPdf.pdf")
'Print PDF in 300 DPI without user new printdialog
        PDF.Print(300, False)

'For advance printing, you can use below 
        Dim PrintDocYouCanWorkWith As PrintDocument = PDF.GetPrintDocument()   
End Sub
End Class 
VB.NET

We first create an instance of the ChromePdfRenderer class. After that, we use the RenderUrlAsPdf function to convert a URL to PDF pages. In the method parameter, we provide the URL of the web page that we wish to convert. In our case, we provide the URL as "https://www.nuget.org/packages/IronPdf". After generating the PDF, We save it to the computer using the SaveAs function.  Afterward, we call the Print method with a specified DPI to print the document. The third false parameter value makes it print without displaying the Print Dialog box.

Run the project. You will see the welcome page with a “Generate PDF” button.

When you click on the “Generate PDF” button, it will print PDF document via default selected printer.

You will see the hard copy of content which has been generated by the IronPDF. The content in the hard copy is the same as mentioned in URL and printed PDF documents.

Conclusion

IronPDF is a great tool for the .NET developers for manipulating and printing PDF in their .NET projects without using Adobe Reader. It provides features in multiple languages like C# and VB.NET which are handy for developers to pace up their work. You can format PDF files, delete or add pages, add PNG or graphics in PDF and many more. IronPDF is free for development.