USING IRONPDF

How to View PDF Files in VB.NET

Updated March 10, 2024
Share:

This article will delve into creating a PDF viewer in VB.NET using IronPDF. It will cover multiple aspects, including the creation of PDF documents, parsing and manipulation of existing PDF files, and addition of images to PDFs.

1. Overview of IronPDF and VB.NET PDF Viewer

IronPDF is a powerful .NET library that provides comprehensive functionality for working with PDF files in VB.NET applications. It offers a range of features, including PDF creation, parsing, manipulation, and viewing. The PDF viewer component provided by IronPDF's VB.NET PDF Viewing Tutorial allows developers to seamlessly integrate a PDF viewer into their VB.NET applications, enabling users to view PDF documents without relying on external tools such as Adobe PDF Reader.

2. Creating PDF files

IronPDF simplifies the process of creating PDF documents in VB.NET. There are multiple approaches you can take to generate PDFs using IronPDF. Here are some examples:

Using HTML to Create a PDF File

IronPDF allows you to convert HTML content to PDF. By leveraging the HTML to PDF conversion capability, you can easily generate PDF documents from HTML templates or dynamically generated HTML content. The IronPDF website provides sample code and examples of how to accomplish this.

Imports IronPdf

' Instantiate Renderer
Private renderer = New ChromePdfRenderer()

' Create a PDF from a HTML string using VB.NET
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

' Export to a file or Stream
pdf.SaveAs("output.pdf")

' Advanced Example with HTML Assets
' Load external HTML assets: Images, CSS, and JavaScript.
' An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
Imports IronPdf

' Instantiate Renderer
Private renderer = New ChromePdfRenderer()

' Create a PDF from a HTML string using VB.NET
Private pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>")

' Export to a file or Stream
pdf.SaveAs("output.pdf")

' Advanced Example with HTML Assets
' Load external HTML assets: Images, CSS, and JavaScript.
' An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
Dim myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", "C:\site\assets\")
myAdvancedPdf.SaveAs("html-with-assets.pdf")
VB.NET

Converting XML to PDF

If you have XML data that needs to be converted into a PDF document, IronPDF simplifies the process. It provides methods to convert XML data to PDF, allowing you to customize the styling and layout of the resulting PDF.

string xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<html>
<style>
td{
  text-align: center;
  padding: 20px;
  border: 1px solid #CDE7F0;
}
th{
  color: white;
  padding: 20px;
}
</style>
<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>
  <table style='border-collapse: collapse;'>
  <thead>
    <tr>
      <th colspan='3'>
        <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr bgcolor='#9acd32'>
      <th bgcolor='#32ab90'>Title</th>
      <th bgcolor='#f49400'>Feature</th>
      <th bgcolor='#2a95d5'>Compatible</th>
    </tr>
    <xsl:for-each select='catalog/cd'>
    <tr>
      <td style='font-weight: bold;'><xsl:value-of select='title'/></td>
      <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>
      <td><xsl:value-of select='compatible'/></td>
    </tr>
    </xsl:for-each>
    </tbody>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
";
string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
  <cd>
    <title>IronPDF</title>
    <feature>Generate, format and manipulate PDFs</feature>
    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>
  </cd>
  <cd>
    <title>IronOCR</title>
    <feature>OCR engine, input, result</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
  <cd>
    <title>IronBarcode</title>
    <feature>Format, read and write Barcode</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
</catalog>
";
XslCompiledTransform transform = new XslCompiledTransform();
using (XmlReader reader = XmlReader.Create(new StringReader(xslt)))
{
    transform.Load(reader);
}
StringWriter results = new StringWriter();
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
    transform.Transform(reader, null, results);
}
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
// options, headers, and footers may be set here
// Render our XML as a PDF via XSLT
Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
string xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<html>
<style>
td{
  text-align: center;
  padding: 20px;
  border: 1px solid #CDE7F0;
}
th{
  color: white;
  padding: 20px;
}
</style>
<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>
  <table style='border-collapse: collapse;'>
  <thead>
    <tr>
      <th colspan='3'>
        <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr bgcolor='#9acd32'>
      <th bgcolor='#32ab90'>Title</th>
      <th bgcolor='#f49400'>Feature</th>
      <th bgcolor='#2a95d5'>Compatible</th>
    </tr>
    <xsl:for-each select='catalog/cd'>
    <tr>
      <td style='font-weight: bold;'><xsl:value-of select='title'/></td>
      <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>
      <td><xsl:value-of select='compatible'/></td>
    </tr>
    </xsl:for-each>
    </tbody>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
";
string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
  <cd>
    <title>IronPDF</title>
    <feature>Generate, format and manipulate PDFs</feature>
    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>
  </cd>
  <cd>
    <title>IronOCR</title>
    <feature>OCR engine, input, result</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
  <cd>
    <title>IronBarcode</title>
    <feature>Format, read and write Barcode</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
</catalog>
";
XslCompiledTransform transform = new XslCompiledTransform();
using (XmlReader reader = XmlReader.Create(new StringReader(xslt)))
{
    transform.Load(reader);
}
StringWriter results = new StringWriter();
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
    transform.Transform(reader, null, results);
}
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
// options, headers, and footers may be set here
// Render our XML as a PDF via XSLT
Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

How to View PDF Files in VB.NET, Figure 2: XML to PDF

Adding Images to PDFs

IronPDF enables you to add images to PDF documents. You can specify the position, size, and other properties of the image within the PDF. This can be useful when creating reports or documents that require visual elements.

Dim lstimages As List(Of String) = New List(Of String) 
lstimages.Add("test.png") lstimages.Add("demo.png") 
Dim pdfdoc = ImageToPdfConverter.ImageToPdf(lstimages).SaveAs("Resultimage.pdf")  
Dim lstimages As List(Of String) = New List(Of String) 
lstimages.Add("test.png") lstimages.Add("demo.png") 
Dim pdfdoc = ImageToPdfConverter.ImageToPdf(lstimages).SaveAs("Resultimage.pdf")  
VB.NET

How to View PDF Files in VB.NET, Figure 3: Extract images from a PDF file Extract images from a PDF file

3. Parsing and Manipulating PDF Files

IronPDF also provides capabilities for parsing and manipulating existing PDF files. You can extract text, images, and other elements from PDF documents, modify their properties, merge multiple PDFs into a single document, split a PDF into multiple files, and perform various other operations. Here's an example of how you can parse a PDF file using IronPDF in VB.NET:

Imports IronPdf
    Module Program
        Sub Main(args As String())
            Dim AllText As String
            Dim pdfdoc = PdfDocument.FromFile("result.pdf")
            AllText = pdfdoc.ExtractTextFromPage(0)
            Console.WriteLine(AllText)
        End Sub
    End Module
Imports IronPdf
    Module Program
        Sub Main(args As String())
            Dim AllText As String
            Dim pdfdoc = PdfDocument.FromFile("result.pdf")
            AllText = pdfdoc.ExtractTextFromPage(0)
            Console.WriteLine(AllText)
        End Sub
    End Module
VB.NET

Explore VB.NET PDF Viewer Example provides a comprehensive set of APIs and methods to manipulate PDF files, making it a versatile tool for working with PDF documents in your VB.NET applications.

How to View PDF Files in VB.NET, Figure 4: Extract text between pages output Extract text between pages output

4. Integrating a PDF Viewer into a VB.NET Application

Now, let's explore how to integrate a PDF viewer into a VB.NET application with PDFViewer Tools. We'll cover the necessary steps, including setting up the project, adding the PDFViewer control, loading and displaying PDF documents, implementing PDF window options, and printing PDF documents.

Setting Up the Project

To begin, create a new VB.NET Windows Forms Application project in Visual Studio. Make sure you have the IronPDF library added as a reference to your project.

Loading and Displaying PDF Documents

To load and display a PDF document in the VB.NET, you need to provide the file path or stream of the PDF document. Here's an example:

Dim Renderer As var = New IronPdf.ChromePdfRenderer
Dim PDFs As var = New List(Of PdfDocument)
PDFs.Add(PdfDocument.FromFile("A.pdf"))
PDF.PrependPdf(Renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"))
PDF.SaveAs("CoverAdded.pdf")
PDF.Dispose();
Dim Renderer As var = New IronPdf.ChromePdfRenderer
Dim PDFs As var = New List(Of PdfDocument)
PDFs.Add(PdfDocument.FromFile("A.pdf"))
PDF.PrependPdf(Renderer.RenderHtmlAsPdf("<h1>Cover Page</h1><hr>"))
PDF.SaveAs("CoverAdded.pdf")
PDF.Dispose();
VB.NET

Printing PDF Documents

IronPDF provides a convenient way to print PDF documents directly from your VB.NET application using the Print PDFs Using IronPDF method of the PDFViewer control. Here's an example:

Dim renderer = New ChromePdfRenderer()
'Create new PdfDocument PDF and render URL into PDF document
        Dim PDF As PdfDocument = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
  PDF.SaveAs("UrlToPdf.pdf")
'Print PDF in 300 DPI without user new print dialog
        PDF.Print(300, False)
'For advanced printing, you can use below 
        Dim PrintDocYouCanWorkWith As PrintDocument = PDF.GetPrintDocument()   
Dim renderer = New ChromePdfRenderer()
'Create new PdfDocument PDF and render URL into PDF document
        Dim PDF As PdfDocument = renderer.RenderUrlAsPdf("https://www.nuget.org/packages/IronPdf")
  PDF.SaveAs("UrlToPdf.pdf")
'Print PDF in 300 DPI without user new print dialog
        PDF.Print(300, False)
'For advanced printing, you can use below 
        Dim PrintDocYouCanWorkWith As PrintDocument = PDF.GetPrintDocument()   
VB.NET

By calling the GetPrintDocument Method Documented Here, you can initiate the printing process for the currently loaded PDF document.

5. Conclusion

This article explored how to create a PDF viewer in VB.NET using IronPDF. Covered various aspects, including creating PDF documents using IronPDF's HTML-to-PDF conversion, XML-to-PDF conversion, and image insertion capabilities, and parsing and manipulating existing PDF files using IronPDF. Finally, integrating the PDFViewer control into a VB.NET application, loading and displaying PDF documents, implementing PDF window options, and printing PDFs.

As you explore the various features and possibilities offered by IronPDF, you will have the flexibility to create, manipulate, and view PDF documents effortlessly within your VB.NET applications. Additionally, IronPDF is also capable of rendering charts in PDF, adding barcodes with HTML to PDF, enhancing security with passwords and metadata and watermarking documents, and even handling PDF forms programmatically.

Feel free to experiment, explore, and refer to the IronPDF documentation and code samples to gain a deeper understanding of the library's capabilities. With IronPDF by your side, you can confidently conquer PDF manipulation and viewing in your VB.NET applications.

Learn how to use the PDF viewer in MAUI by visiting the Viewing PDFs in MAUI tutorial.

< PREVIOUS
How to Create PDF Signatures in .NET
NEXT >
How to Build a .NET PDF editor application using IronPDF

Ready to get started? Version: 2024.12 just released

Free NuGet Download Total downloads: 11,781,565 View Licenses >