Create or Generate a PDF file in a Console application

28 Jan 20258 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, form, and secure PDF files.

To quickly get started with .NET PDF Library. Please, check this video:

Prerequisites:

  • Visual Studio 2022 or later
  • Install the .NET 8 SDK (or the latest version of .NET SDK)

Create a simple PDF document using .NET Core

The following steps illustrate creating a simple Hello World PDF document in a console application using .NET Core.

Step 1: Create a new C# Console Application project. Console sample creation

Step 1.1: Name the project. Name the application

Step 2: Install the Syncfusion.Pdf.Net.Core NuGet package as reference to your .NET Standard applications from NuGet.org. NET Core NuGet package

Step 1: Create a new C# Console App template using Visual Studio Code via Microsoft Templates.

Alternatively, create a console application by executing the following command in the terminal (Ctrl+`).

dotnet new console -o Create-PDF-document
cd Create-PDF-document

Step 2: To create a PDF document in the console app, run the following command to install the Syncfusion.PDF.Net.Core package.

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 learn about registering Syncfusion® license key in your application to use our components.

Step 3: Include the following namespaces in the Program.cs file.

  • C#
  • using Syncfusion.Pdf.Graphics;
    using Syncfusion.Pdf;
    using Syncfusion.Drawing;

    Step 4: Include the below code snippet in Program.cs to create an PDF file.

  • //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));
    //Create a fileStream.
    FileStream fileStream = new FileStream("Output.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
    //Save and close the PDF document.
    document.Save(fileStream);
    document.Close(true);

    Step 5: Build the project.

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

    Run the following command in terminal to build the project.

    dotnet build
    

    Step 6: Run the project.

    Click the Start button (green arrow) or press F5 to run the app.

    Run the following command in terminal to build the project.

    dotnet run
    

    You can download a complete working sample from GitHub.

    By executing the program, you will get the PDF document as follows.
    Console output PDF document

    Create a simple PDF document using .NET Framework

    The following steps illustrates creating a simple Hello world PDF document in console application using .NET Framework.

    Step 1: Create a new C# Console Application (.NET Framework) project. Console Application creation

    Step 1.1: Name the project. Name the application

    Step 2: Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Standard applications from NuGet.org. NET Framework NuGet package

    Step 1: Create a new C# Console App (.NET Framework) template using Visual Studio Code via Microsoft Templates.

    Alternatively, create a console application (.NET Framework) by executing the following command in the terminal (Ctrl+`).

    dotnet new console -o Create-PDF-document
    
    cd Create-PDF-document
    

    Step 2: To create a PDF document in the console app (.NET Framework), run the following command to install the Syncfusion.Pdf.WinForms package.

    dotnet add package Syncfusion.Pdf.WinForms
    

    NOTE

    The Syncfusion.Pdf.WinForms NuGet package is dependent package for Syncfusion® Windows Forms GUI controls, so named with suffix “WinForms”. It has a platform-independent .NET framework (4.0, 4.5, 4.5.1, 4.6) assemblies of the PDF library and doesn’t contain any Windows Forms-related references or code. Hence, we recommend this package for the .NET framework Console application.

    Step 3: Include the following namespaces in the Program.cs.

  • C#
  • using Syncfusion.Pdf.Graphics;
    using Syncfusion.Pdf;
    using System.Drawing;

    Step 4: Include the following code sample in Program.cs to create a PDF file.

  • C#
  • //Create a new PDF document. 
    using (PdfDocument document = new PdfDocument()) {
        //Add a page to the document.
        PdfPage page = document.Pages.Add();
        //Create PDF graphics for a 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));
        //Save the document.
        document.Save("Output.pdf");
    }

    Step 5: Build the project.

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

    Run the following command in terminal to build the project.

    dotnet build
    

    Step 6: Run the project.

    Click the Start button (green arrow) or press F5 to run the app.

    Run the following command in terminal to build the project.

    dotnet run
    

    You can download a complete working sample from GitHub.

    By executing the program, you will get the PDF document as follows.
    Console 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.