Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In modern .NET applications, generating PDFs dynamically is a common requirement. Whether you're creating reports, invoices, or other documents, having a streamlined way to conditionally generate different PDF formats is essential. The switch statement (also known as Select Case in some languages) in C# is a powerful tool to implement such logic efficiently. The switch case will take some form of data type (char, int, string etc.), and compare it to the case values to see if any match. If they do, the code within that case block will execute, otherwise the default case will if the other case patterns don't match.
IronPDF is a robust PDF generation and manipulation library for .NET developers, enabling you to convert HTML, images, and various other content into PDFs. By leveraging C#’s switch statement, you can customize your PDFs based on different conditions such as user input or data state.
This article will walk you through how to use the C# Switch case statement with IronPDF to create dynamic, conditional PDF content with a good level of control flow within the program, which will ultimately raise the efficiency and readability of your programs.
In C#, the switch statement (not to be confused with the switch expression) provides a clean and structured way to handle conditional logic based on the value of a variable. Instead of using multiple statements, such as if-else statements, switch case statements allows for easier-to-read and more maintainable code. The switch statement will search for a pattern match for the match expression passed to the statement, before executing the code within the default case if no match is found.
Here’s the basic structure of a switch statement in C#:
switch (variable)
{
case value1:
// action for value1
break;
case value2:
// action for value2
break;
default:
// default action if no case matches
break;
}
switch (variable)
{
case value1:
// action for value1
break;
case value2:
// action for value2
break;
default:
// default action if no case matches
break;
}
Select Case variable
Case value1
' action for value1
Case value2
' action for value2
Case Else
' default action if no case matches
End Select
Each case represents a possible value for the variable, and when a match is found, the code within that block is executed. This is particularly useful when you have multiple outcomes based on a single variable, such as determining which type of document to generate in a PDF.
Imagine you’re developing a system where different types of documents need to be generated depending on user input. For instance, you might need to create a report for one user and an invoice for another. With C#'s switch statement, you can easily determine which type of PDF to generate using IronPDF.
Here’s a sample scenario: Based on a user's selection, you can use a switch statement to decide which HTML content to render into a PDF document, by matching the user's choice to the same type of case value.
switch (userChoice)
{
case "report":
// Code to generate a report PDF using IronPDF
break;
case "invoice":
// Code to generate an invoice PDF using IronPDF
break;
default:
// Code to generate a default PDF document using IronPDF
break;
}
switch (userChoice)
{
case "report":
// Code to generate a report PDF using IronPDF
break;
case "invoice":
// Code to generate an invoice PDF using IronPDF
break;
default:
// Code to generate a default PDF document using IronPDF
break;
}
Select Case userChoice
Case "report"
' Code to generate a report PDF using IronPDF
Case "invoice"
' Code to generate an invoice PDF using IronPDF
Case Else
' Code to generate a default PDF document using IronPDF
End Select
In this example, the system can generate multiple types of documents by reusing IronPDF’s powerful PDF rendering capabilities, while simplifying decision-making using the switch statement.
Let’s walk through how to integrate C#'s switch with IronPDF for generating PDFs. For this example, we’ll handle two types of documents: Reports and Invoices.
Install IronPDF: First you'll need to install IronPDF to begin using it in your projects.
Create HTML templates for your report and invoice. This allows IronPDF to render these templates into a PDF.
Based on user input (or any other variable), use the switch statement to determine which HTML template to use and pass it to IronPDF for PDF rendering.
To start using IronPDF, you will first need to install it. If it's already installed, then you can skip to the next section, otherwise, the following steps cover how to install the IronPDF library.
To install IronPDF using the NuGet Package Manager Console, open Visual Studio and navigate to the Package Manager Console. Then run the following command:
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
Opening Visual Studio, go to "tools -> NuGet Package Manager -> Manage NuGet Packages for Solution" and search for IronPDF. From here, all you need to do is select your project and click "Install" and IronPDF will be added to your project.
Once you have installed IronPDF, all you need to add to start using IronPDF is the correct using statement at the top of your code:
using IronPdf;
using IronPdf;
Imports IronPdf
Let’s take a closer look at a real-world use case. Assume you’re developing a system for a business that needs to generate both reports and invoices for its customers. Depending on what the user selects, you can render different content into a PDF.
Report Generation:
When the user selects "Report", the system generates a PDF with report-specific content. Using HTML templates, you can easily customize the content structure.
case "Report":
string reportHtml = "<h1>Monthly Report</h1><p>This report provides a detailed overview of activities.</p>";
PdfDocument reportPdf = pdfRenderer.RenderHtmlAsPdf(reportHtml);
reportPdf.SaveAs("Monthly_Report.pdf");
break;
case "Report":
string reportHtml = "<h1>Monthly Report</h1><p>This report provides a detailed overview of activities.</p>";
PdfDocument reportPdf = pdfRenderer.RenderHtmlAsPdf(reportHtml);
reportPdf.SaveAs("Monthly_Report.pdf");
break;
Case "Report"
Dim reportHtml As String = "<h1>Monthly Report</h1><p>This report provides a detailed overview of activities.</p>"
Dim reportPdf As PdfDocument = pdfRenderer.RenderHtmlAsPdf(reportHtml)
reportPdf.SaveAs("Monthly_Report.pdf")
break
Invoice Generation:
For invoices, you can include billing information and itemized lists within the HTML, which IronPDF will convert to a high-quality PDF.
case "Invoice":
string invoiceHtml = "<h1>Invoice #12345</h1><p>Billing details and itemized list here.</p>";
PdfDocument invoicePdf = pdfRenderer.RenderHtmlAsPdf(invoiceHtml);
invoicePdf.SaveAs("Invoice_12345.pdf");
break;
case "Invoice":
string invoiceHtml = "<h1>Invoice #12345</h1><p>Billing details and itemized list here.</p>";
PdfDocument invoicePdf = pdfRenderer.RenderHtmlAsPdf(invoiceHtml);
invoicePdf.SaveAs("Invoice_12345.pdf");
break;
Case "Invoice"
Dim invoiceHtml As String = "<h1>Invoice #12345</h1><p>Billing details and itemized list here.</p>"
Dim invoicePdf As PdfDocument = pdfRenderer.RenderHtmlAsPdf(invoiceHtml)
invoicePdf.SaveAs("Invoice_12345.pdf")
break
This approach ensures that you maintain flexibility and reusability in your codebase, as you can easily extend the switch statement to handle additional document types.
In the following code example, we'll take user input to pass on to the Switch statement to determine which PDF will be generated.
using IronPdf;
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("What do you want to create?");
Console.WriteLine("a. Report");
Console.WriteLine("b. Invoice");
var input = Console.ReadLine();
string docType;
if (input == "a")
{
GeneratePdf("Report");
}
else if (input == "b")
{
GeneratePdf("Invoice");
}
else
{
GeneratePdf(null);
}
}
public static void GeneratePdf(string docType)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
switch (docType)
{
case "Report":
string reportHtml = "<h1>Report</h1><p>This is a dynamically generated report.</p>";
PdfDocument reportPdf = renderer.RenderHtmlAsPdf(reportHtml);
reportPdf.SaveAs("Report.pdf");
break;
case "Invoice":
string invoiceHtml = "<h1>Invoice</h1><p>This is a dynamically generated invoice.</p>";
PdfDocument invoicePdf = renderer.RenderHtmlAsPdf(invoiceHtml);
invoicePdf.SaveAs("Invoice.pdf");
break;
default:
string defaultHtml = "<h1>Document</h1><p>This is a default PDF document.</p>";
PdfDocument defaultPdf = renderer.RenderHtmlAsPdf(defaultHtml);
defaultPdf.SaveAs("Default.pdf");
break;
}
}
}
using IronPdf;
using System;
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("What do you want to create?");
Console.WriteLine("a. Report");
Console.WriteLine("b. Invoice");
var input = Console.ReadLine();
string docType;
if (input == "a")
{
GeneratePdf("Report");
}
else if (input == "b")
{
GeneratePdf("Invoice");
}
else
{
GeneratePdf(null);
}
}
public static void GeneratePdf(string docType)
{
ChromePdfRenderer renderer = new ChromePdfRenderer();
switch (docType)
{
case "Report":
string reportHtml = "<h1>Report</h1><p>This is a dynamically generated report.</p>";
PdfDocument reportPdf = renderer.RenderHtmlAsPdf(reportHtml);
reportPdf.SaveAs("Report.pdf");
break;
case "Invoice":
string invoiceHtml = "<h1>Invoice</h1><p>This is a dynamically generated invoice.</p>";
PdfDocument invoicePdf = renderer.RenderHtmlAsPdf(invoiceHtml);
invoicePdf.SaveAs("Invoice.pdf");
break;
default:
string defaultHtml = "<h1>Document</h1><p>This is a default PDF document.</p>";
PdfDocument defaultPdf = renderer.RenderHtmlAsPdf(defaultHtml);
defaultPdf.SaveAs("Default.pdf");
break;
}
}
}
Imports IronPdf
Imports System
Public Class Program
Public Shared Sub Main(ByVal args() As String)
Console.WriteLine("What do you want to create?")
Console.WriteLine("a. Report")
Console.WriteLine("b. Invoice")
Dim input = Console.ReadLine()
Dim docType As String
If input = "a" Then
GeneratePdf("Report")
ElseIf input = "b" Then
GeneratePdf("Invoice")
Else
GeneratePdf(Nothing)
End If
End Sub
Public Shared Sub GeneratePdf(ByVal docType As String)
Dim renderer As New ChromePdfRenderer()
Select Case docType
Case "Report"
Dim reportHtml As String = "<h1>Report</h1><p>This is a dynamically generated report.</p>"
Dim reportPdf As PdfDocument = renderer.RenderHtmlAsPdf(reportHtml)
reportPdf.SaveAs("Report.pdf")
Case "Invoice"
Dim invoiceHtml As String = "<h1>Invoice</h1><p>This is a dynamically generated invoice.</p>"
Dim invoicePdf As PdfDocument = renderer.RenderHtmlAsPdf(invoiceHtml)
invoicePdf.SaveAs("Invoice.pdf")
Case Else
Dim defaultHtml As String = "<h1>Document</h1><p>This is a default PDF document.</p>"
Dim defaultPdf As PdfDocument = renderer.RenderHtmlAsPdf(defaultHtml)
defaultPdf.SaveAs("Default.pdf")
End Select
End Sub
End Class
In this example, the switch statement controls which document type is generated. If the docType is "Report", a report PDF will be created. If it's "Invoice", an invoice will be generated. If no match is found, a default PDF is created instead.
IronPDF stands out because of its ability to render HTML, CSS, JavaScript, and even dynamic C# content directly into PDFs. By integrating it with C#’s switch statement, you can streamline your document generation process, making it more efficient and maintainable.
Some key benefits of IronPDF include:
To learn more about the robust set of features IronPDF has to offer, be sure to check out its handy how-to available, you can explore all its features risk-free before committing.
By leveraging the switch case C# statement and IronPDF, you can create dynamic, customizable PDF documents with minimal effort. Whether you need to generate reports, invoices, or other types of documents, this combination offers flexibility and efficiency. Using statements such as an if statement works great if you're processing just one or two potential outcomes, but switch statements can greatly improve the clarity of your code when working with multiple outcomes.
Using the switch block to output different PDF types is a great way of leveraging IronPDF to a whole new level. With a large set of rich features, great performance, and cross-platform compatibility, IronPDF is a powerful PDF generation tool to have at your fingertips. Don't just take our Word for it—download the free trial today and see for yourself how it can streamline your PDF workflows!
9 .NET API products for your office documents