Create or Generate a PDF file in a Console application

20 Feb 20259 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:

Steps to create PDF document in Console 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: Download and install Visual Studio Code from the official website.

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

Step 2: Name the project. Name the application

Step 3: Install the Syncfusion.Pdf.Net.Core NuGet package as reference to your .NET Standard applications from NuGet.org. 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 learn about registering Syncfusion® license key in your application to use our components.

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

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

    Step 5: 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 6: Build the project.

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

    Step 7: 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 new .NET Core console application project.

    dotnet new console -n CreatePdfConsoleApp
    

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

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

    cd CreatePdfConsoleApp
    

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

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

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

    Step 6: 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 7: Build the project.

    Run the following command in terminal to build the project.

    dotnet build
    

    Step 8: 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 .NET Core console application project.

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

    Launch JetBrains Rider

    • In the new Solution dialog, select Project Type as Console.
    • Enter a project name and specify the location.
    • Select the target framework (e.g., .NET 8.0, .NET 9.0).
    • 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 learn about registering Syncfusion® license key in your application to use our components.

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

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

    Step 5: 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 6: Build the project.

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

    Step 7: 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.
    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.

    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 Code from the official website.

    Steps to create PDF document using .NET Framework

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

    Step 2: Name the project.
    Name the application

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

    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 4: Include the following namespaces in the Program.cs.

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

    Step 5: 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 6: Build the project.

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

    Step 7: Run the project.

    Click the Start button (green arrow) 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.
    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.