How to Open a PDF File in C#

Introduction

PDF is now one of the most popular and widely-used formats for digital documents. PDF files are used to display output, print invoices, print bank statements, and so much more. PDF documents are easily created. They can be saved to any disk (i.e., CD, DVD, External Hard disk, USB, etc.) and prevent users from editing files. PDFs also allow users to sign documents digitally, as well as provide secure authentication. IronPDF provides a package that helps users create, read and edit PDFs easily. In this article, we are going to generate a PDF file in C# using IronPDF and read the PDF using the acrobat reader /Adobe reader. We are also going to read the PDF file in C# using IronPDF.

How to Open PDF in C#

  1. Open Visual Studio and install the NuGet Package
  2. Adding reference to the code - enabling the use of available class and functions
  3. Declare a common object for ChromePdfRenderer
  4. Using the RenderHTMLAsPdf function
  5. Using System.Diagnostics.Process.Start

1. Open Visual Studio and Install the NuGet Package

Open Visual Studio and go to the "file menu". Select "New Project", then select Console Application/Windows Forms/WPF Application. IronPDF can be used on all applications. You can also use it in apps such as Webform, MVC/MVC Core

Enter the project name and select the file path in the appropriate text box. Then click the "Create" button. Next, select the required Dot Net Framework. Now the project will generate the structure for the selected application. If you have selected the console application, it will now open the program.cs file where you can enter the code and build/run the application.

Next Install Nuget Package IronPDF

Left-click the project and a menu will pop up. Select NuGet Package manager from the menu and search for IronPDF. Select the first result in the NuGet Package dialog and click the Install/Download option.

Alternatively:

In Visual Studio go to Tools-> Nuget Package manager -> Package manager console

Enter the following code on the package manager console tab.

Install-Package IronPDF

Now the package will download/install on the current project and it is ready to use in the code.

2. Adding Reference to the Code - Enabling the use of available classes and functions

Add the reference “IronPDF” to the code as shown below. This will allow us to use the class and functions available from IronPDF in our code.

3. Declare a Common Object for ChromePdfRenderer

Declaring a common object for ChromePdfRenderer will help you to convert any web page or HTML snippet into a PDF. By creating a common object, we will be able to use it without creating any more objects of the same class. Which allows us to reuse the code more than one time. There are multiple functions that can be used to create PDF files. We can use strings, links, or HTML files and convert them into PDFs, which can then be saved to the desired location.

We can also use a static function without creating any object for the ChromePdfRenderer. The static function is as follows:

  • StaticRenderHtmlAsPdf

  • StaticRenderHTMLFileAsPdf

We can use any one of these static methods to generate a PDF file. We can also include the PDF document options such as margins, titles, DPI, headers and footers, text, etc. By using ChromePdfRenderOptions we can pass a parameter on any one of these static methods.

We are able to declare the ChromePdfRenderOptions as common or individual for every pdf document. It is very simple and easy to use. We are going to use any one of the non-static functions to generate a PDF file and save it to a default location.

4. Using the RenderHTMLAsPdf:

We can use any one of the above IronPDF functions to create a PDF. If you are using the function name RenderHtmlAsPdf then pass any string as a parameter and then use the SaveAs function to save the PDF on the desired file path. While using the SaveAs function. we need to pass the filename and location as a parameter, or if we are using a windows application, we can use the SaveAs dialog to save the PDF file to the desired location. With the help of an HTML string, we are able to format the PDF document. Also, we can use CSS for the text design, and we can use any HTML tag to design a PDF document, as IronPDF does not have any restrictions on using HTML tags.

When we are using large HTML text it is difficult to add all the HTML text to the text box, so we can use another method which we have referred to above as RenderHtmlFileAsPdf which will help us to convert all the HTML into a PDF document. With this method, we are able to add large HTML files. Also, we are able to include the external CSS file in these HTML files, as well as external images, etc.

IronPDF also helps us to print the data from any links using the RenderUrlAsPdf function. This function processes the link to generate a PDF and save the PDF files to the desired file path using the SaveAs function. This IronPDF function will include the CSS and all the images available on the site.

The following code shows an example of the IronPDF function.

ChromePdfRenderer Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderHtmlAsPdf("Hello IronPdf");
var OutputPath = "DemoIronPdf.pdf";
PDF.SaveAs(OutputPath);
System.Diagnostics.Process.Start(OutputPath);
ChromePdfRenderer Renderer = new ChromePdfRenderer();
var PDF = Renderer.RenderHtmlAsPdf("Hello IronPdf");
var OutputPath = "DemoIronPdf.pdf";
PDF.SaveAs(OutputPath);
System.Diagnostics.Process.Start(OutputPath);
Dim Renderer As New ChromePdfRenderer()
Dim PDF = Renderer.RenderHtmlAsPdf("Hello IronPdf")
Dim OutputPath = "DemoIronPdf.pdf"
PDF.SaveAs(OutputPath)
System.Diagnostics.Process.Start(OutputPath)
VB   C#

The above example shows how we can use the IronPDF function to generate the PDF using the string. Input can be typed by the user or we can gather it from the database. In the above code, we have created an instance object for the ChromePdfRender, and then by using the instance object with the help of RenderHtmlAsPdf we are generating the PDF file. Then, by using the SaveAs IronPDF function we can save the PDF file on the given path. We are passing the file name as a parameter with .pdf as the extension. The extension is mandatory when saving the file because if we do not include the extension, it will be considered an unknown format that may not be readable by a PDF reader. If we don't specify a file path, it will be saved at the exe location in the program.

5. Using System.Diagnostics.Process.Start to preview the PDF file

For this last step, we are using System.Diagnostics.Process.Start to preview a PDF file. This function invokes the command line function to open the PDF file from the path. If we have a PDF reader it will open the saved PDF file in the reader. If we do not have a PDF reader it will open with dialog, and from the dialog, we need to select the program with which to open the PDF.

We can read PDF files using IronPDF, and this will read the PDF document line-by-line. We are even able to open a password-restricted PDF file using IronPDF. The following code demonstrates how to read a PDF document.

PDF = PdfDocument.FromFile("encrypted.pdf", "password");
//Get all text to put in a search index string
AllText = PDF.ExtractAllText();
IEnumerable<System.Drawing.Image>
AllImages = PDF.ExtractAllImages();
find the precise text and images for each page in the document
for (var index = 0; index < PDF.PageCount; index++) {
    int PageNumber = index + 1;
    string Text =PDF.ExtractTextFromPage(index);
    IEnumerable<System.Drawing.Image> Images = PDF.ExtractImagesFromPage(index);
}
PDF = PdfDocument.FromFile("encrypted.pdf", "password");
//Get all text to put in a search index string
AllText = PDF.ExtractAllText();
IEnumerable<System.Drawing.Image>
AllImages = PDF.ExtractAllImages();
find the precise text and images for each page in the document
for (var index = 0; index < PDF.PageCount; index++) {
    int PageNumber = index + 1;
    string Text =PDF.ExtractTextFromPage(index);
    IEnumerable<System.Drawing.Image> Images = PDF.ExtractImagesFromPage(index);
}
PDF = PdfDocument.FromFile("encrypted.pdf", "password")
'Get all text to put in a search index string
AllText = PDF.ExtractAllText()
Dim AllImages As IEnumerable(Of System.Drawing.Image) = PDF.ExtractAllImages()
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
'find the precise text @and images for @each page in the document for(var index = 0; index < PDF.PageCount; index++)
'{
'    int PageNumber = index + 1;
'    string Text =PDF.ExtractTextFromPage(index);
'    IEnumerable<System.Drawing.Image> Images = PDF.ExtractImagesFromPage(index);
'}
VB   C#

The above code shows how we can read PDF files using IronPDF. IronPDF first reads the PDF document from the entered string filename and it also allows users to include a password if there is one. It will read all the lines. This is very useful when we need to get data from a PDF, as it reduces the amount of manual work and does not require any human supervision.

Conclusion

IronPDF provides a simple and easy way to create a PDF with straightforward steps. The IronPDF library can be used in various environments such as Windows Forms, Mobile app, and web apps using a Dot Net Framework. We don’t need a separate library for each platform. We only need IronPDF to generate the PDF.

IronPDF offers a 30-days trial key, and you can currently buy five products from Iron Software for the price of two at: https://ironpdf.com/licensing/

You can download a file project from this link.