Create or Generate PDF file in ASP.NET MVC
24 Jul 20264 minutes to read
The .NET PDF library is used to create, read, and edit PDF documents. This library also offers functionality to merge, split, stamp, work with forms, and secure PDF files.
To include the .NET PDF library into your ASP.NET MVC application, refer to the NuGet Package Required or Assemblies Required documentation.
Prerequisites
- Visual Studio 2017 or later.
- .NET Framework 4.6.2 or later.
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 window, name your project and click Create.

Step 3: In the template selection window, choose MVC and click Create.

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

Step 5: Register the Syncfusion® license key. A trial watermark is added to every page of the generated PDF until a valid key is registered. Include the license key in the Global.asax.cs file before creating any Syncfusion® component:
using Syncfusion.Licensing;
protected void Application_Start()
{
// Register the Syncfusion license
SyncfusionLicenseProvider.RegisterLicense("YOUR LICENSE KEY");
}Replace "YOUR LICENSE KEY" with the license key associated with your Syncfusion® account. If you do not have a license key, you can request a free 30-day trial or apply for a Community License from the Syncfusion® website. For more information about registering a license key in your application, refer to the Syncfusion® Licensing Documentation.
Step 6: A default controller named HomeController.cs is added on creation of the ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using System.Drawing;
using System.Web.Mvc;Step 7: A default action method named Index is present in HomeController.cs. Right-click this Index method and select Go To View to open the associated view page Index.cshtml. Add a new button in 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 8: 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.
public ActionResult CreatePDFDocument()
{
//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 the browser after saving it.
//HttpReadType.Save prompts the browser to save the file; use HttpReadType.Open to open it inline.
document.Save("Output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save);
}
return View();
}Step 9: Build the project by clicking Build > Build Solution or pressing Ctrl+Shift+B.
Step 10: Run the project by pressing F5. Visual Studio launches the app in IIS Express.
Step 11: Open the app in a browser, then click the Generate PDF Document button on the home page. The browser downloads Output.pdf containing the text “Hello World!!!”.
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.