Prerequires:

Step 1: Install .NET SDK:

  • Ensure that you have the .NET SDK installed on your system. You can download it from the .NET Downloads page.
    Step 2: Install Visual Studio Code:
  • Download and install Visual Studio Code from the official website.
    Step 3: 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.

Steps to create PDF document in ASP.NET Core

Step 1: Open the terminal (Ctrl+` ) and run the following command to create a new Blazor Server application

dotnet new blazorserver -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.

  • C#
  • 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.

  • C#
  • @{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.

  • C#
  • //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