DEV Community

IronSoftware
IronSoftware

Posted on

.NET PDF Viewer in C#

Displaying PDFs in .NET applications requires choosing the right approach for your platform. WinForms, WPF, and MAUI each have different solutions—from embedded WebBrowser controls to dedicated viewer components.

using IronPdf;
// Install via NuGet: Install-Package IronPdf.Viewer.Maui

// In MAUI ContentPage
var viewer = new IronPdfView();
viewer.Source = IronPdfViewSource.FromFile("document.pdf");
Enter fullscreen mode Exit fullscreen mode

The PDF renders natively in your application with zoom, scroll, and navigation support.

What Are My Options for Each Platform?

Platform Recommended Approach
WinForms WebBrowser control or third-party viewer
WPF WebBrowser or PDF viewer component
MAUI Dedicated PDF viewer component
Blazor PDF.js or iframe embedding
Console Not applicable (headless)

Each platform has trade-offs between simplicity and features.

How Do I Display PDFs in WinForms?

The simplest approach uses the built-in WebBrowser control:

using System.Windows.Forms;
// No additional packages needed

public class PdfViewerForm : Form
{
    private WebBrowser webBrowser;

    public PdfViewerForm()
    {
        webBrowser = new WebBrowser
        {
            Dock = DockStyle.Fill
        };
        Controls.Add(webBrowser);
    }

    public void LoadPdf(string pdfPath)
    {
        // Navigate to the PDF file
        webBrowser.Navigate(pdfPath);
    }
}

// Usage
var form = new PdfViewerForm();
form.LoadPdf(@"C:\documents\report.pdf");
form.ShowDialog();
Enter fullscreen mode Exit fullscreen mode

This relies on the system's default PDF viewer (usually Adobe Reader or Edge). If no viewer is installed, it won't work.

How Do I Display PDFs in WPF?

Similar approach with WPF's WebBrowser:

<!-- MainWindow.xaml -->
<Window x:Class="PdfViewer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        Title="PDF Viewer" Height="600" Width="800">
    <WebBrowser x:Name="pdfBrowser" />
</Window>
Enter fullscreen mode Exit fullscreen mode
// MainWindow.xaml.cs
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    public void LoadPdf(string path)
    {
        pdfBrowser.Navigate(new Uri(path));
    }
}
Enter fullscreen mode Exit fullscreen mode

For more control, consider a dedicated PDF component.

How Do I Display PDFs in MAUI?

MAUI requires a viewer component. Install the package:

Install-Package IronPdf.Viewer.Maui
Enter fullscreen mode Exit fullscreen mode

Configure in MauiProgram.cs:

using IronPdf;

public static MauiApp CreateMauiApp()
{
    var builder = MauiApp.CreateBuilder();
    builder.UseMauiApp<App>();

    // Initialize IronPDF viewer
    builder.ConfigureIronPdf(options =>
    {
        options.LicenseKey = "YOUR-LICENSE-KEY";
    });

    return builder.Build();
}
Enter fullscreen mode Exit fullscreen mode

Add the viewer to your page:

<!-- MainPage.xaml -->
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:ironpdf="clr-namespace:IronPdf.Viewer.Maui;assembly=IronPdf.Viewer.Maui">
    <ironpdf:IronPdfView x:Name="pdfViewer" />
</ContentPage>
Enter fullscreen mode Exit fullscreen mode
// MainPage.xaml.cs
public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
        LoadDocument();
    }

    private async void LoadDocument()
    {
        // From file
        pdfViewer.Source = IronPdfViewSource.FromFile("document.pdf");

        // Or from bytes
        byte[] pdfBytes = await GetPdfFromApi();
        pdfViewer.Source = IronPdfViewSource.FromBytes(pdfBytes);

        // Or from stream
        var stream = File.OpenRead("document.pdf");
        pdfViewer.Source = IronPdfViewSource.FromStream(stream);
    }
}
Enter fullscreen mode Exit fullscreen mode

The viewer handles multi-page documents, zoom, and scrolling automatically.

How Do I Generate and Display PDFs Together?

Create a PDF and immediately show it:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

public class ReportService
{
    public byte[] GenerateReport(ReportData data)
    {
        var renderer = new [ChromePdfRenderer](https://ironpdf.com/blog/videos/how-to-render-html-string-to-pdf-in-csharp-ironpdf/)();
        var html = BuildReportHtml(data);
        var pdf = renderer.RenderHtmlAsPdf(html);
        return pdf.BinaryData;
    }
}

// In your MAUI page
private async void GenerateAndView()
{
    var reportService = new ReportService();
    var pdfBytes = reportService.GenerateReport(currentData);

    pdfViewer.Source = IronPdfViewSource.FromBytes(pdfBytes);
}
Enter fullscreen mode Exit fullscreen mode

Generate on-demand, display immediately—no file saving required.

How Do I Handle Large PDFs?

Large documents need streaming:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

// For very large files, use stream loading
public void LoadLargePdf(string filePath)
{
    // Stream instead of loading entire file into memory
    using var stream = new FileStream(
        filePath,
        FileMode.Open,
        FileAccess.Read,
        FileShare.Read,
        bufferSize: 4096,
        useAsync: true);

    pdfViewer.Source = IronPdfViewSource.FromStream(stream);
}
Enter fullscreen mode Exit fullscreen mode

Streaming loads pages on-demand rather than the entire document upfront.

How Do I Add Toolbar Controls?

Build navigation around the viewer:

<!-- MAUI XAML -->
<VerticalStackLayout>
    <HorizontalStackLayout HorizontalOptions="Center" Spacing="10">
        <Button Text="Previous" Clicked="OnPrevious" />
        <Label x:Name="pageLabel" Text="Page 1 of 1" />
        <Button Text="Next" Clicked="OnNext" />
        <Button Text="Zoom In" Clicked="OnZoomIn" />
        <Button Text="Zoom Out" Clicked="OnZoomOut" />
    </HorizontalStackLayout>

    <ironpdf:IronPdfView x:Name="pdfViewer"
                         PageChanged="OnPageChanged" />
</VerticalStackLayout>
Enter fullscreen mode Exit fullscreen mode
private void OnPrevious(object sender, EventArgs e)
{
    if (pdfViewer.CurrentPage > 1)
        pdfViewer.GoToPage(pdfViewer.CurrentPage - 1);
}

private void OnNext(object sender, EventArgs e)
{
    if (pdfViewer.CurrentPage < pdfViewer.PageCount)
        pdfViewer.GoToPage(pdfViewer.CurrentPage + 1);
}

private void OnZoomIn(object sender, EventArgs e)
{
    pdfViewer.ZoomLevel *= 1.25;
}

private void OnZoomOut(object sender, EventArgs e)
{
    pdfViewer.ZoomLevel /= 1.25;
}

private void OnPageChanged(object sender, PageChangedEventArgs e)
{
    pageLabel.Text = $"Page {e.CurrentPage} of {e.TotalPages}";
}
Enter fullscreen mode Exit fullscreen mode

Custom UI gives you full control over navigation.

How Do I Use WebView as a Fallback?

When dedicated viewers aren't available, WebView with PDF.js works:

// MAUI with WebView
public void LoadPdfInWebView(string pdfPath)
{
    // Host PDF.js locally or use CDN
    var html = $@"
        <html>
        <head>
            <script src='https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.4.120/pdf.min.js'></script>
        </head>
        <body>
            <canvas id='pdf-canvas'></canvas>
            <script>
                pdfjsLib.getDocument('{pdfPath}').promise.then(pdf => {{
                    pdf.getPage(1).then(page => {{
                        var canvas = document.getElementById('pdf-canvas');
                        var context = canvas.getContext('2d');
                        var viewport = page.getViewport({{ scale: 1.5 }});
                        canvas.height = viewport.height;
                        canvas.width = viewport.width;
                        page.render({{ canvasContext: context, viewport: viewport }});
                    }});
                }});
            </script>
        </body>
        </html>";

    webView.Source = new HtmlWebViewSource { Html = html };
}
Enter fullscreen mode Exit fullscreen mode

This works cross-platform but requires more setup.

How Do I Open PDFs in External Viewer?

Sometimes the system viewer is appropriate:

using System.Diagnostics;

public void OpenInSystemViewer(string pdfPath)
{
    // Windows
    Process.Start(new ProcessStartInfo
    {
        FileName = pdfPath,
        UseShellExecute = true
    });

    // MAUI cross-platform
    // await Launcher.OpenAsync(new OpenFileRequest
    // {
    //     File = new ReadOnlyFile(pdfPath)
    // });
}
Enter fullscreen mode Exit fullscreen mode

This launches the default PDF application (Adobe Reader, Edge, Preview, etc.).

What About Print Support?

Print from your viewer:

using IronPdf;
// Install via NuGet: Install-Package IronPdf

// Direct print without viewer
var pdf = PdfDocument.FromFile("document.pdf");
pdf.Print(); // Sends to default printer

// Or with dialog
pdf.Print(300); // 300 DPI
Enter fullscreen mode Exit fullscreen mode

MAUI printing requires platform-specific code, as there's no unified print API yet.

Quick Platform Comparison

Feature WinForms WebBrowser WPF WebBrowser MAUI Viewer
Setup Built-in Built-in NuGet package
Dependencies System PDF viewer System PDF viewer Self-contained
Customization Limited Limited Full control
Performance Good Good Excellent
Cross-platform No No Yes

Choosing the Right Approach

Use WebBrowser control when:

  • Building Windows-only apps
  • Users have PDF viewers installed
  • You need quick implementation

Use dedicated viewer when:

  • Building MAUI/cross-platform apps
  • You need consistent behavior
  • You want embedded experience
  • You need custom controls

Use external viewer when:

  • Simple "open PDF" functionality
  • User prefers their PDF app
  • No embedding required

PDF viewing in .NET depends on your platform and requirements. Choose the approach that matches your deployment scenario.

For more details, see the IronPDF viewer documentation.


Written by Jacob Mellor, CTO at Iron Software. Jacob created IronPDF and leads a team of 50+ engineers building .NET document processing libraries.

Top comments (0)