How can I help you?
Create or Generate PDF file in ASP.NET MVC
15 Jun 20263 minutes to read
The Syncfusion® .NET PDF library is used to create, read, and edit PDF documents. This library also offers functionality to merge, split, stamp, forms and secure PDF files.
To include the .NET PDF library into your ASP.NET MVC application, please refer to the NuGet Package Required or Assemblies Required documentation.
Steps to create PDF document in ASP.NET MVC
Step 1: Create a new C# ASP.NET Web Application (.NET Framework) project.

Step 2: In the project configuration windows, name your project and click Create.


Step 3: Install the Syncfusion.Pdf.AspNet.Mvc5 NuGet package as a reference to your ASP.NET MVC applications from NuGet.org.

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 MVC project. Include the following namespaces in that HomeController.cs file.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;Step 5: A default action method named Index will be present in HomeController.cs. Right-click on this 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 follows.
@{Html.BeginForm("CreatePDFDocument", "Home", FormMethod.Get);
{
<div>
<input type="submit" value="Generate PDF Document" style="width:150px;height:27px" />
</div>
}
Html.EndForm();
}Step 6: Add a new action method named CreatePDFDocument in HomeController.cs and include the code example below to generate a PDF document using the PdfDocument class. Then use the DrawString method of the PdfGraphics object to draw text on the PDF page and download the output PDF from the ASP.NET MVC application.
//Create an instance of PdfDocument.
using (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));
//Open the document in browser after saving it.
document.Save("Output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save);
}By executing the program, you will get the PDF document as follows.

You can download a complete working sample from GitHub.
Click here to explore the rich set of Syncfusion® PDF library features.
An online sample link to create PDF document.