How to open pdf file in browser in C#

By FoxLearn 11/27/2024 1:34:04 PM   991
To display a PDF within a Windows Forms application using C#, you can use the WebBrowser control, which is capable of rendering PDF files if Adobe Acrobat Reader or another PDF viewer plugin is installed on the user's machine.

How to display pdf file in c# windows application

Open your Visual Studio, then create a new Windows Forms Application project.

Drag and drop the WebBrowser control onto your form. Resize and position it according to your layout needs.

display pdf in form c#

In your C# code, you can load a PDF file into the WebBrowser control using its Navigate method, which is a way to c# open pdf file in browser directly within your application.

Double click on your form, then add a Form_Load event handler which allows you to display the pdf in a windows forms application.

// c# open pdf file in browser
private void Form1_Load(object sender, EventArgs e)
{
    string pdfFilePath = @"C:\sample.pdf";
    // Load the PDF file into the WebBrowser control
    webBrowser1.Navigate(pdfFilePath);// pdf viewer c#
}

The WebBrowser control relies on the system's default web browser or installed plugins to display PDF files. If the user's machine doesn’t have a PDF viewer installed, the control won’t be able to display the PDF.