Generating PDF documents in .NET — the simple way

Bekzat Karayev
2 min readApr 30, 2021

Hello!

Many enterprise or open-source projects require creating PDF documents from scratch or generating PDF files using data from other data sources. PDFFlow could be a good solution if such tasks need to be accomplished quickly.

PDFFlow is a cross-platform C# library, you just define required document as a set of layout rules and a sequence of page elements like paragraphs, tables, images, footers, etc. and then the library builds the document automatically. I will show you how it is done on a simple console app.

First, let’s create a “MyFirstPDF” .NET Core console app in the Microsoft Visual Studio:

Use NuGet Package Manager to add PDFFlow NuGet package to the project (select first one in the list):

Create an instance of a DocumentBuilder class to declare new document:

using Gehtsoft.PDFFlow.Builder;
using System;
namespace MyFirstPDF
{
class Program
{
static void Main(string[] args)
{
DocumentBuilder doc = DocumentBuilder.New();
}
}
}

Contents of the documents are defined in the SectionBuilder object, let's add a "Hello World!" paragraph to it:

SectionBuilder section = doc.AddSection();
section.AddParagraph("Hello World!");

Call Build method that builds the PDF document with a specified name:

doc.Build("helloworld.pdf");

Run the console app, you will find generated “helloworld.pdf” file inside .\bin\Debug\netcoreapp(version) directory of your project.

That’s all, a few lines of code and the PDF document is ready.

I am part of the support and developer team of this library so if you have any questions about the PDFFlow or need any help on your own projects regarding PDF generation, feel free to comment below or contact me.

I will share some more PDF creation tips soon.

--

--