Create or Generate a PDF file in a Console application

20 Dec 20244 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:

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 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);

    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 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");
    }

    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.