How to Open PDFs in Default Viewer in C#
Opening a PDF in the default viewer is a common task. You’ll often need this when generating PDFs in a .NET application. After creating and editing PDF files programmatically with IronPDF, you often need to display them to users immediately in their chosen default application (such as Adobe Acrobat or Microsoft Edge). This guide will walk you through the steps on how to generate PDF files using IronPDF and how they can be automatically opened in Windows using System.Diagnostics.Process.Start.
The combination of IronPDF’s powerful HTML-to-PDF conversion capabilities with the simple Process launch method creates a seamless workflow for creating and displaying professional PDF files in your default apps settings.
How do you generate and open a PDF File with IronPDF?
The most straightforward approach involves three steps:
- Create a PDF document with IronPDF.
- Save the file to a directory.
- Open the PDF in the user’s default app.
Here’s a complete working example you can try in Visual Studio with a new Console App project:
using IronPdf;
using System.Diagnostics;
// Create a new PDF renderer
var renderer = new ChromePdfRenderer();
// Generate PDF from HTML content
var pdf = renderer.RenderHtmlAsPdf(@"
<html>
<body>
<h1>Invoice #12345</h1>
<p>Generated on: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p>
<table>
<tr><td>Product</td><td>Price</td></tr>
<tr><td>IronPDF License</td><td>$749</td></tr>
</table>
</body>
</html>");
// Save the PDF to a file
string outputPath = "invoice.pdf";
pdf.SaveAs(outputPath);
// Open the PDF in the default viewer
Process.Start(new ProcessStartInfo
{
FileName = outputPath,
UseShellExecute = true
});
using IronPdf;
using System.Diagnostics;
// Create a new PDF renderer
var renderer = new ChromePdfRenderer();
// Generate PDF from HTML content
var pdf = renderer.RenderHtmlAsPdf(@"
<html>
<body>
<h1>Invoice #12345</h1>
<p>Generated on: " + DateTime.Now.ToString("yyyy-MM-dd") + @"</p>
<table>
<tr><td>Product</td><td>Price</td></tr>
<tr><td>IronPDF License</td><td>$749</td></tr>
</table>
</body>
</html>");
// Save the PDF to a file
string outputPath = "invoice.pdf";
pdf.SaveAs(outputPath);
// Open the PDF in the default viewer
Process.Start(new ProcessStartInfo
{
FileName = outputPath,
UseShellExecute = true
});
IRON VB CONVERTER ERROR developers@ironsoftware.com
This code first creates a ChromePdfRenderer
instance, which is IronPDF's primary class for converting HTML to PDF. The RenderHtmlAsPdf
method converts the HTML string into a PDF document object.
After saving the PDF using SaveAs, the code uses Process.Start with ProcessStartInfo to open the file in the default PDF viewer. The key setting here is UseShellExecute = true, which tells Windows to open the PDF file with its default application.
Output
As you can see in the image below, IronPDF was able to successfully generate the PDF file and display it using the default viewer. You'll notice in my case, this is Opera GX.
Why does UseShellExecute matter when opening PDF documents?
In .NET Core and modern .NET versions, the UseShellExecute parameter defaults to false. Without explicitly setting it to true, your app will throw an error when trying to launch a PDF.
using IronPdf;
using System.Diagnostics;
using System.IO;
// Generate a report with IronPDF
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 50;
renderer.RenderingOptions.MarginBottom = 50;
var pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Save to temp directory for immediate viewing
string tempPath = Path.Combine(Path.GetTempPath(), $"URL_{Guid.NewGuid()}.pdf");
pdf.SaveAs(tempPath);
// IMPORTANT: Set UseShellExecute = true for .NET Core/5+
var startInfo = new ProcessStartInfo
{
FileName = tempPath,
UseShellExecute = true // Required in .NET Core/5+ to open PDF in default viewer
};
Process.Start(startInfo);
using IronPdf;
using System.Diagnostics;
using System.IO;
// Generate a report with IronPDF
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 50;
renderer.RenderingOptions.MarginBottom = 50;
var pdf = renderer.RenderUrlAsPdf("https://en.wikipedia.org/wiki/Main_Page");
// Save to temp directory for immediate viewing
string tempPath = Path.Combine(Path.GetTempPath(), $"URL_{Guid.NewGuid()}.pdf");
pdf.SaveAs(tempPath);
// IMPORTANT: Set UseShellExecute = true for .NET Core/5+
var startInfo = new ProcessStartInfo
{
FileName = tempPath,
UseShellExecute = true // Required in .NET Core/5+ to open PDF in default viewer
};
Process.Start(startInfo);
IRON VB CONVERTER ERROR developers@ironsoftware.com
The UseShellExecute property determines whether to use the operating system shell to start the process. When set to true, Windows uses the file association registry to determine which default PDF reader should open the file. Without this setting in modern .NET versions, you'll encounter an error stating the file cannot be opened. Using a temporary directory with a unique filename (via Guid.NewGuid()) prevents file conflicts when generating multiple PDFs. For more details on rendering URLs to PDF, see IronPDF's documentation.
Output
How do you handle file paths correctly?
File paths with spaces and special characters require proper handling. Here's a robust approach that includes error checking:
using IronPdf;
using System.Diagnostics;
using System.IO;
// Generate PDF from HTML file
var renderer = new ChromePdfRenderer();
var htmlContent = File.ReadAllText("template.html");
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Create output directory if it doesn't exist
string outputDir = @"C:\PDF Reports\Monthly";
Directory.CreateDirectory(outputDir);
// Build file path with timestamp
string fileName = $"Report_{DateTime.Now:yyyyMMdd_HHmmss}.pdf";
string fullPath = Path.Combine(outputDir, fileName);
// Save the PDF
pdf.SaveAs(fullPath);
// Verify file exists before opening in default PDF viewer
if (File.Exists(fullPath))
{
Process.Start(new ProcessStartInfo
{
FileName = fullPath,
UseShellExecute = true
});
}
else
{
Console.WriteLine($"Error: PDF file not found at {fullPath}");
}
using IronPdf;
using System.Diagnostics;
using System.IO;
// Generate PDF from HTML file
var renderer = new ChromePdfRenderer();
var htmlContent = File.ReadAllText("template.html");
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Create output directory if it doesn't exist
string outputDir = @"C:\PDF Reports\Monthly";
Directory.CreateDirectory(outputDir);
// Build file path with timestamp
string fileName = $"Report_{DateTime.Now:yyyyMMdd_HHmmss}.pdf";
string fullPath = Path.Combine(outputDir, fileName);
// Save the PDF
pdf.SaveAs(fullPath);
// Verify file exists before opening in default PDF viewer
if (File.Exists(fullPath))
{
Process.Start(new ProcessStartInfo
{
FileName = fullPath,
UseShellExecute = true
});
}
else
{
Console.WriteLine($"Error: PDF file not found at {fullPath}");
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
This code shows several best practices: using Path.Combine to build file paths correctly regardless of the operating system, creating directories as needed with Directory.CreateDirectory, and verifying file existence before attempting to open the PDF in the default viewer.
The timestamp in the filename ensures uniqueness and provides a clear record of when each PDF was generated. For more advanced PDF manipulation options, explore IronPDF's editing capabilities.
What are the best practices for opening generated PDFs?
For production applications, consider implementing a more complete workflow that handles the PDF lifecycle correctly:
using IronPdf;
using IronPdf.Rendering;
using System.Diagnostics;
using System.IO;
public static void GenerateAndDisplayPdf(string htmlContent, string documentName)
{
try
{
// Configure IronPDF renderer with production settings
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
PaperSize =PdfPaperSize.A4,
PrintHtmlBackgrounds = true,
CreatePdfFormsFromHtml = true
}
};
// Generate the PDF
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Use user's Documents folder for better accessibility
string documentsPath = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments);
string pdfFolder = Path.Combine(documentsPath, "Generated PDFs");
Directory.CreateDirectory(pdfFolder);
string outputPath = Path.Combine(pdfFolder, $"{documentName}.pdf");
pdf.SaveAs(outputPath);
// Open PDF in default viewer and don't wait for it to close
var process = Process.Start(new ProcessStartInfo
{
FileName = outputPath,
UseShellExecute = true // Essential for opening PDF in default application
});
Console.WriteLine($"PDF opened: {outputPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error generating or opening PDF: {ex.Message}");
}
}
using IronPdf;
using IronPdf.Rendering;
using System.Diagnostics;
using System.IO;
public static void GenerateAndDisplayPdf(string htmlContent, string documentName)
{
try
{
// Configure IronPDF renderer with production settings
var renderer = new ChromePdfRenderer
{
RenderingOptions = new ChromePdfRenderOptions
{
PaperSize =PdfPaperSize.A4,
PrintHtmlBackgrounds = true,
CreatePdfFormsFromHtml = true
}
};
// Generate the PDF
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
// Use user's Documents folder for better accessibility
string documentsPath = Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments);
string pdfFolder = Path.Combine(documentsPath, "Generated PDFs");
Directory.CreateDirectory(pdfFolder);
string outputPath = Path.Combine(pdfFolder, $"{documentName}.pdf");
pdf.SaveAs(outputPath);
// Open PDF in default viewer and don't wait for it to close
var process = Process.Start(new ProcessStartInfo
{
FileName = outputPath,
UseShellExecute = true // Essential for opening PDF in default application
});
Console.WriteLine($"PDF opened: {outputPath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error generating or opening PDF: {ex.Message}");
}
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
This example includes error handling and saves PDFs in the user’s Documents folder, which works better in real-world apps. It also configures IronPDF with appropriate rendering options to give your PDF documents a truly customized feel when you render them.
The method doesn't wait for the PDF viewer to close, allowing your application to continue running while the user views the document, according to Microsoft's documentation on Process.Start, this approach ensures proper resource management.
Conclusion
Opening a PDF file in the default application from a .NET program is easy with IronPDF and System.Diagnostics.Process. By setting the correct parameters (UseShellExecute = true) and providing a valid path, your .NET project will successfully launch the PDF in the default PDF reader.
Whether you’re working in Visual Studio, building a Windows desktop exe, or experimenting with a web app, this approach works consistently. IronPDF handles PDF creation, while Windows handles preview and display through the default app installed on the computer.
This tutorial provides a comprehensive solution and code for managing PDF files in your .NET applications, covering creation and viewing without the need for additional software or dependencies beyond IronPDF and the standard Microsoft .NET runtime.
Ready to implement PDF generation and viewing in your application? Start with a free trial to find the perfect fit for your needs.