Syncfusion AI Assistant

How can I help you?

Create or Generate PDF file in ASP.NET Core

11 Jun 202611 minutes to read

The Syncfusion® .NET PDF library is a powerful and versatile solution for creating, reading, and editing PDF documents in .NET applications. It also provides advanced features such as merging and splitting PDFs, adding stamps, working with form fields, and securing PDF files with encryption and permissions.

To integrate the .NET PDF library into your ASP.NET Core application, refer to the official documentation sections on NuGet Package Required or Assemblies Required for step-by-step guidance.

NOTE

Beginning with our Volume 2, 2023 release, we have eliminated the dependency on the System.Drawing.Common package from our Syncfusion.Pdf.Imaging.Net.Core package. Instead, we have introduced SkiaSharp as the alternative library.

Steps to create PDF document in ASP.NET Core

Prerequisites:

  • Install .NET SDK: Ensure that you have the .NET SDK installed on your system. You can download it from the .NET Downloads page.
  • Install Visual Studio: Download and install Visual Studio from the official website.

Step 1: Create a new C# ASP.NET Core Web Application project. Create ASP.NET Core Web application in Visual Studio

Step 2: In configuration windows, name your project and click Next. Select Web Application pattern

Step 3: Install the Syncfusion.Pdf.Net.Core package as reference to your ASP.NET Core applications from NuGet.org. Install PDF .NET Core NuGet package

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.

Step 4: A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using System.IO;

Step 5: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml. Add a new button in the Index.cshtml as shown below.

@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get);
      {
         <div>
            <input type="submit" value="Create PDF Document" style="width:200px;height:27px" />
         </div>
      }
      Html.EndForm();
}

Step 6: Add a new action method named CreatePDFDocument in HomeController.cs file and include the below code example to generate a PDF document using the PdfDocument class. Then use the DrawString method of the PdfGraphics object to draw the text on the PDF page.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Saving the PDF to the MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Set the position as '0'.
stream.Position = 0;
//Download the PDF document in the browser.
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
fileStreamResult.FileDownloadName = "Sample.pdf";
return fileStreamResult;

Step 7: Build the project. Click on Build > Build Solution or press Ctrl + Shift + B to build the project.

Step 8: Run the project. Click the Start button (green arrow) or press F5 to run the app.

Prerequisites:

  • Install .NET SDK: Ensure that you have the .NET SDK installed on your system. You can download it from the .NET Downloads page.
  • Install Visual Studio Code: Download and install Visual Studio Code from the official website.
  • Install C# Extension for VS Code: Open Visual Studio Code, go to the Extensions view (Ctrl+Shift+X), and search for ‘C#’. Install the official C# extension provided by Microsoft.

Step 1: Open the terminal (Ctrl+` ) and run the following command to create a C# ASP.NET Core Web Application project.

dotnet new mvc -n CreatePdfASPNETCoreAPP

Step 2: Replace **CreatePdfASPNETCoreAPP with your desired project name.

Step 3: Navigate to the project directory using the following command

cd CreatePdfASPNETCoreAPP

Step 4: Use the following command in the terminal to add the Syncfusion.Pdf.Net.Core package to your project.

dotnet add package Syncfusion.Pdf.Net.Core

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.

Step 5: A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using System.IO;

Step 6: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml. Add a new button in the Index.cshtml as shown below.

@{Html.BeginForm("CreateDocument", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Create PDF Document" style="width:200px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 7: Add a new action method named CreatePDFDocument in HomeController.cs file and include the below code example to generate a PDF document using the PdfDocument class. Then use the DrawString method of the PdfGraphics object to draw the text on the PDF page.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Saving the PDF to the MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Set the position as '0'.
stream.Position = 0;
//Download the PDF document in the browser.
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
fileStreamResult.FileDownloadName = "Sample.pdf";
return fileStreamResult;

Step 8: Build the project.

Run the following command in terminal to build the project.

dotnet build

Step 9: Run the project.

Run the following command in terminal to build the project.

dotnet run

Prerequisites:

  • JetBrains Rider.
  • Install .NET 8 SDK or later.

Step 1. Open JetBrains Rider and create a new ASP.NET Core Web application project.

  • Launch JetBrains Rider.
  • Click new solution on the welcome screen.

Launch JetBrains Rider

  • In the new Solution dialog, select Project Type as Web.
  • Select the target framework (e.g., .NET 8.0, .NET 9.0) and template as Web App(Model-View-Controller).
  • Enter a project name and specify the location.
  • Click create.

Creating a new .NET Core console application in JetBrains Rider

Step 2: Install the NuGet package from NuGet.org.

  • Click the NuGet icon in the Rider toolbar and type Syncfusion.Pdf.Net.Core in the search bar.
  • Ensure that “nuget.org” is selected as the package source.
  • Select the latest Syncfusion.Pdf.Net.Core NuGet package from the list.
  • Click the + (Add) button to add the package.

Select the Syncfusion.Pdf.Net.Core package

  • Click the Install button to complete the installation.

Install the package

NOTE

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, you also have to add “Syncfusion.Licensing” assembly reference and include a license key in your projects. Please refer to this link to know about registering Syncfusion® license key in your application to use our components.

Step 4: A default controller with name HomeController.cs gets added on creation of ASP.NET Core project. Include the following namespaces in that HomeController.cs file.

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Drawing;
using System.IO;

Step 5: A default action method named Index will be present in HomeController.cs. Right click on Index method and select Go To View where you will be directed to its associated view page Index.cshtml. Add a new button in the Index.cshtml as shown below.

@{
    Html.BeginForm("CreateDocument", "Home", FormMethod.Get);
    {
        <div>
            <input type="submit" value="Create PDF Document" style="width:200px;height:27px" />
        </div>
    }
    Html.EndForm();
}

Step 6: Add a new action method named CreatePDFDocument in HomeController.cs file and include the below code example to generate a PDF document using the PdfDocument class. Then use the DrawString method of the PdfGraphics object to draw the text on the PDF page.

//Create a new PDF document.
PdfDocument document = new PdfDocument();
//Add a page to the document.
PdfPage page = document.Pages.Add();
//Create PDF graphics for the page.
PdfGraphics graphics = page.Graphics;
//Set the standard font.
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 20);
//Draw the text.
graphics.DrawString("Hello World!!!", font, PdfBrushes.Black, new PointF(0, 0));
//Saving the PDF to the MemoryStream.
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Set the position as '0'.
stream.Position = 0;
//Download the PDF document in the browser.
FileStreamResult fileStreamResult = new FileStreamResult(stream, "application/pdf");
fileStreamResult.FileDownloadName = "Sample.pdf";
return fileStreamResult;

Step 7: Build the project.

Click the Build button in the toolbar or press Ctrl+Shift+B to build the project.

Step 8: Run the project.

Click the Run button (green arrow) in the toolbar or press F5 to run the app.

You can download a complete working sample from GitHub.

By executing the program, you will get the PDF document as follows.
ASP.Net Core output PDF document

Click here to explore the rich set of Syncfusion® PDF library features.

An online sample link to create PDF document in ASP.NET Core.