Getting Started

Invoice Tutorial

A demonstration of the API's core concepts.

FluentWriter is a modern .NET library for Word (.docx) document generation that emphasizes clean architecture and developer productivity. In this tutorial, we'll build a professional invoice document while exploring the core concepts that make FluentWriter powerful and intuitive to use.

By the end, you'll have a fully functional, paginated invoice generator that looks like this:

INSTALLATIONBefore starting this tutorial, please familiarize yourself with the Quick Start tutorial. It will guide you through the installation process and provide a basic understanding of the library's architecture.
SOURCE CODE You can download, review, and compile the complete example from this GitHub repository.

Suggested Architecture

FluentWriter recommends a clear three-layer architecture for both maintainability and clarity:

  • Document Models - define the raw data that appears in your document, such as invoice details or report content. These classes remain free of business logic and focus solely on representing structured information.
  • Data Source - handle asynchronous data fetching, transformations, and calculations. Here, you perform database queries, map domain entities to the document models, and load external resources (e.g., images) to prepare all the information needed to render the document.
  • Template - use C# features (such as loops, conditional logic, helper methods) and FluentWriter API to design the visual layout and appearance of your document.

Document Model Layer

First, let's define the data structure for our invoice. These models capture all the information we need to display:

public class InvoiceModel
{
    public int InvoiceNumber { get; set; }
    public DateTime IssueDate { get; set; }
    public DateTime DueDate { get; set; }

    public Address SellerAddress { get; set; }
    public Address CustomerAddress { get; set; }

    public List<OrderItem> Items { get; set; }
    public string Comments { get; set; }
}

public class OrderItem
{
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

public class Address
{
    public string CompanyName { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public object Email { get; set; }
    public string Phone { get; set; }
}

Template layer

With data ready, focus on how it should appear in the final document. FluentWriter uses a fluent API to define headers, footers, and main content.

Basic Page Structure

As the first step, we'll define a single section. with a simple header, content area, and footer. The class below implements the IDocument interface and uses the Compose method to define the document’s structure.

Each fluent API call creates a container with its own style, size, alignment constraints and layout behavior — making their order important. While most elements are simple containers holding a single child, some advanced elements offer multiple slots to accommodate more complex layouts.