Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In today's digital era, PDF files have become an essential part of our daily routine. They are used in many fields like education, business, and personal use, etc. PDF files can contain a large amount of data, including text, images, and tables, which makes them an ideal format for sharing and presenting data.
However, displaying PDF documents within a Windows Forms application can sometimes be challenging. This article will show you how to create a simple PDF viewer application using IronPDF, a C# PDF library for .NET developers.
IronPDF is a C# library that enables developers to create, edit, and display PDF files in their .NET applications. It allows users to convert HTML, images, and SVG to PDF documents and vice versa. IronPDF is simple to use, and it provides developers with a wide range of features to manipulate PDF files.
To create a PDF viewer application, you will need the following tools and packages:
Visual Studio: A software development IDE (Integrated Development Environment) used to create Windows Forms applications.
IronPDF: A NuGet package that provides functionality to read, create, and manipulate PDF documents.
HTML to PDF
To create a new Windows Forms Application, launch Visual Studio, and click on "Create a new project." Then select "Windows Forms App (.NET Framework)" from the list of project templates.
Visual Studio Code
Next, enter a name for your project and click on the Create button. This will create a new Windows Forms Application project in Visual Studio.
To install IronPDF, right-click on your project in the solution explorer and select "Manage NuGet Packages". This will open the NuGet Package Manager, where you can search for IronPDF.
NuGet Package Manager
An alternative method is to install it via the NuGet Package Manager Console. To do so, run the following command in the console:
Install-Package IronPdf
Once you have found IronPDF, click on the "Install" button to add it to your project. This will install IronPDF and all its dependencies.
We'll add a RichTextBox in our form. This RichTextBox will be used to display PDF content. A RichTextBox is a container that enables you to display or edit rich text content, including paragraphs, hyperlinks, and more. You can also use a RichTextBox to display PDF content, although it may not preserve all the formatting found in the original PDF document.
Here's how you can add a RichTextBox to your designer CS file:
Access the RickTextBox in Form1
Now we need to add a button, which will help to select a PDF file using the Browse window.
Add a new Button to Form1
Now double-click on the button. It'll open the source code file. First, you will need to add the following using
statement to the top of your Form1.cs
file:
using IronPdf;
using IronPdf;
Imports IronPdf
The provided code is an event handler for a button click event in a Windows Forms application that uses the IronPDF library to load a PDF file and extract its text content.
private void openBtn_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
License.LicenseKey = "YourKey";
var pdf = PdfDocument.FromFile(openFileDialog.FileName);
pdfData.Text = pdf.ExtractAllText();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while processing the PDF file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void openBtn_Click(object sender, EventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
License.LicenseKey = "YourKey";
var pdf = PdfDocument.FromFile(openFileDialog.FileName);
pdfData.Text = pdf.ExtractAllText();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while processing the PDF file: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
Here's a detailed breakdown of the code:
This is the event handler for the 'Click' event of a button called 'openBtn'. Every time this button is clicked, this method gets triggered.
private void openBtn_Click(object sender, EventArgs e)
private void openBtn_Click(object sender, EventArgs e)
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'private void openBtn_Click(object sender, EventArgs e)
This line initializes a new instance of the OpenFileDialog
class, which is a standard dialog that allows the user to open a file.
var openFileDialog = new OpenFileDialog();
var openFileDialog = new OpenFileDialog();
Dim openFileDialog As New OpenFileDialog()
This line sets the Filter
property of the OpenFileDialog
to only display PDF files and all file types in the file dialog box.
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*"
The ShowDialog()
method shows the OpenFileDialog
to the user. If the user selects a file and clicks the 'Open' button on the dialog, ShowDialog()
returns DialogResult.OK
. This line checks if the user did just that.
if (openFileDialog.ShowDialog() == DialogResult.OK)
if (openFileDialog.ShowDialog() == DialogResult.OK)
If openFileDialog.ShowDialog() = DialogResult.OK Then
If the user selects a file and clicks 'Open', this line gets the selected file's path from the FileName
property of the OpenFileDialog. It then uses the FromFile
method of the PdfDocument
class in the IronPDF library to create a new PdfDocument
object that represents the selected PDF file.
var pdf = PdfDocument.FromFile(openFileDialog.FileName);
var pdf = PdfDocument.FromFile(openFileDialog.FileName);
Dim pdf = PdfDocument.FromFile(openFileDialog.FileName)
This line calls the ExtractAllText
method on the PdfDocument
object to extract all text from the PDF file. It then assigns the extracted text to the Text property of a control called 'pdfData', which is presumably a TextBox or similar control that can display text.
pdfData.Text = pdf.ExtractAllText();
pdfData.Text = pdf.ExtractAllText();
pdfData.Text = pdf.ExtractAllText()
In summary, this method opens a file dialog for the user to select a PDF file. If the user selects a file and clicks 'Open', the method loads the PDF file, extracts all text from it, and displays the extracted text in a control.
Once you have completed all the steps above, you can build and run your PDF viewer application. To do this, click on the "Build" menu in Visual Studio and select "Build Solution". After building the solution, you can run the application by pressing the "F5" key or by clicking on the "Start" button in the toolbar.
When the application starts, you should see a form with RichTextBox and a button. To open a PDF file, click on the "Open PDF" button, select a PDF file from your computer, and click the "Open" button.
Run the Application
After selecting the PDF file, its content will show up in the RichTextBox.
Display the text content after selecting a PDF file
Learn how to use PDF viewer in MAUI by visiting "View PDFs in MAUI" tutorial.
By following these steps, you can create a simple PDF viewer application that allows users to view PDF content. With the IronPDF library, you can also add more advanced features such as text searching, annotation, and printing as well as encrypting PDFs and interacting with PDF forms.
Remember that this is just a simple example of what you can do with IronPDF. You can experiment with different controls, properties, and events to create your custom PDF viewer application that meets your specific needs.
If you're interested in learning more about IronPDF, be sure to check out the IronPDF documentation, which provides detailed information on how to use the library and includes many examples and tutorials.
To use IronPDF in your application, you need to have a valid license key. You can obtain a license key by purchasing a license from the IronPDF website. License starts from $749 and you can also avail free trial.
9 .NET API products for your office documents