Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Products
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. iText7 PDF Generation in ASP.NET Core - Dynamic Reports and Invoice Creation

iText7 PDF Generation in ASP.NET Core - Dynamic Reports and Invoice Creation

Date- May 20,2026 83
itext7 pdf

Overview

The need for dynamic PDF generation in web applications has grown significantly as businesses increasingly require automated solutions for generating reports and invoices. The iText7 library stands out as a powerful tool for creating PDF documents in .NET environments, particularly ASP.NET Core. It allows developers to produce complex documents with rich formatting, images, and interactive elements.

Real-world use cases of iText7 include generating invoices for e-commerce platforms, creating reports for data analytics applications, and producing certificates for educational institutions. This versatility solves the problem of manual document creation, saving time and reducing errors.

Prerequisites

  • ASP.NET Core: Familiarity with the ASP.NET Core framework and its MVC pattern is essential.
  • C#: Understanding C# programming language fundamentals is necessary for implementing the code examples.
  • iText7 Library: Basic knowledge of the iText7 library, including installation and setup.
  • NuGet Package Manager: Experience using NuGet to install libraries in .NET projects.

Setting Up iText7 in ASP.NET Core

To begin using iText7 for PDF generation in ASP.NET Core, the library must first be added to the project via NuGet. This process ensures that all necessary dependencies are included and ready for use in the application.

dotnet add package itext7

This command should be executed in the terminal within the project directory. Once installed, the necessary namespaces can be included in your code files to access iText7 classes and methods.

Configuring the Startup Class

After installing iText7, you may need to configure your ASP.NET Core application to support PDF generation. This can be done in the Startup.cs file by ensuring that the necessary services are registered.

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
}

This basic configuration sets up the application to use MVC, which is essential for handling requests and returning responses, including PDF documents.

Creating a Simple PDF Document

With iText7 successfully integrated into your project, the next step is to create a simple PDF document. This section covers the fundamental components required to generate a basic PDF file.

using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace PdfGenerationExample
{
    public class PdfGenerator
    {
        public void CreatePdf(string filePath)
        {
            using (PdfWriter writer = new PdfWriter(filePath))
            {
                using (PdfDocument pdf = new PdfDocument(writer))
                {
                    Document document = new Document(pdf);
                    document.Add(new Paragraph("Hello, World!"));
                    document.Close();
                }
            }
        }
    }
} 

This code creates a basic PDF containing the text "Hello, World!". Here's a breakdown of how it works:

  • PdfWriter: This class is responsible for writing the PDF file to the specified file path.
  • PdfDocument: Represents the PDF document itself. It is created using the writer instance.
  • Document: This higher-level abstraction allows adding elements like paragraphs, images, and tables.
  • Paragraph: Represents a block of text. In this case, it adds simple text to the document.

After executing this code, a file named output.pdf will be created at the specified location, containing the text "Hello, World!".

Dynamic Content Generation

iText7 allows for the dynamic generation of content based on application data. This capability is particularly useful for creating invoices or reports that change based on user input or database records.

public void CreateInvoicePdf(string filePath, Invoice invoice)
{
    using (PdfWriter writer = new PdfWriter(filePath))
    {
        using (PdfDocument pdf = new PdfDocument(writer))
        {
            Document document = new Document(pdf);
            document.Add(new Paragraph("Invoice #" + invoice.InvoiceNumber));
            document.Add(new Paragraph("Date: " + invoice.Date.ToString("d"))); 
            document.Add(new Paragraph("Total: " + invoice.Total.ToString("C")));  
            document.Close();
        }
    }
}

This method generates an invoice PDF dynamically. It accepts a file path and an Invoice object, which contains the invoice number, date, and total amount. The use of string concatenation allows for custom content based on the invoice data.

Handling Complex Layouts

iText7 also supports creating complex layouts using tables, lists, and images, which can enhance the presentation of reports and invoices.

public void CreateComplexInvoicePdf(string filePath, Invoice invoice)
{
    using (PdfWriter writer = new PdfWriter(filePath))
    {
        using (PdfDocument pdf = new PdfDocument(writer))
        {
            Document document = new Document(pdf);
            document.Add(new Paragraph("Invoice #" + invoice.InvoiceNumber));
            Table table = new Table(UnitValue.CreatePercentArray(new float[] { 1, 3, 1 }));
            table.AddHeaderCell("Item");
            table.AddHeaderCell("Description");
            table.AddHeaderCell("Price");
            foreach (var item in invoice.Items)
            {
                table.AddCell(item.Name);
                table.AddCell(item.Description);
                table.AddCell(item.Price.ToString("C"));
            }
            document.Add(table);
            document.Close();
        }
    }
}

This code snippet demonstrates creating a complex invoice PDF that includes a table for itemized billing. The use of Table and UnitValue classes allows for flexible layout customization, ensuring the invoice is well-organized and professional.

Edge Cases & Gotchas

While generating PDFs with iText7, several pitfalls can arise, particularly around resource management and document structure. One common mistake is neglecting to close the document properly, which can lead to file corruption.

// Incorrect approach
public void IncorrectPdfGeneration(string filePath)
{
    PdfWriter writer = new PdfWriter(filePath);
    PdfDocument pdf = new PdfDocument(writer);
    Document document = new Document(pdf);
    document.Add(new Paragraph("This PDF is not closed correctly!"));
    // Missing document.Close()
}

In the incorrect approach, the absence of document.Close() results in an unfinalized PDF, which may lead to errors when opening the file. Always ensure that all resources are disposed of properly.

Performance & Best Practices

Performance is critical when generating PDFs, especially in applications that handle multiple requests simultaneously. One best practice is to use asynchronous programming to avoid blocking the main thread during PDF generation.

public async Task CreatePdfAsync(string filePath)
{
    using (PdfWriter writer = new PdfWriter(filePath))
    {
        using (PdfDocument pdf = new PdfDocument(writer))
        {
            Document document = new Document(pdf);
            await document.Add(new Paragraph("Asynchronous PDF Generation"));
            document.Close();
        }
    }
}

By making the PDF generation asynchronous, the application can handle additional requests while waiting for the PDF to be generated, thereby improving overall responsiveness.

Real-World Scenario: Invoice Generation Application

To solidify understanding, let's implement a simple invoice generation application. This application will allow users to input invoice details and generate a PDF when they submit the form.

public class Invoice
{
    public string InvoiceNumber { get; set; }
    public DateTime Date { get; set; }
    public decimal Total { get; set; }
    public List Items { get; set; }
}

public class InvoiceItem
{
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
}

public class InvoiceController : Controller
{
    [HttpPost]
    public IActionResult GenerateInvoice(Invoice invoice)
    {
        string filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/invoices", invoice.InvoiceNumber + ".pdf");
        CreateComplexInvoicePdf(filePath, invoice);
        return File(System.IO.File.ReadAllBytes(filePath), "application/pdf", invoice.InvoiceNumber + ".pdf");
    }
}

This controller action handles invoice generation, receiving an Invoice object from a form submission. It then calls the PDF generation method and returns the generated file to the user for download.

Conclusion

  • iText7 is a powerful tool for generating dynamic PDFs in ASP.NET Core applications.
  • Understanding the library's fundamental classes and methods is crucial for effective PDF generation.
  • Proper resource management and asynchronous programming are key to building responsive applications.
  • Dynamic content generation allows for versatile and customized document creation.
  • Always test edge cases to ensure robust PDF generation.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Configuring NHibernate with ASP.NET Core: A Comprehensive Step-by-Step Guide
Apr 05, 2026
Implementing IP Whitelisting and Blacklisting Middleware in ASP.NET Core
Jun 10, 2026
Mastering Puppeteer Sharp for HTML to PDF Conversion in ASP.NET Core
May 20, 2026
Implementing Real-Time Communication in ASP.NET Core with Pusher
May 18, 2026
Previous in ASP.NET Core
Integrating Klaviyo Email Marketing with ASP.NET Core for E-comme…
Next in ASP.NET Core
Mastering Puppeteer Sharp for HTML to PDF Conversion in ASP.NET C…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 907 views
  • 2
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 629 views
  • 3
    How to get fcm server key 5,050 views
  • 4
    Responsive Slick Slider 23,598 views
  • 5
    Understanding CWE-312: Best Practices for Secure Data Stor… 298 views
  • 6
    Mastering Functions in C++: A Complete Guide with Examples 3,745 views
  • 7
    Integrating Cloudflare Turnstile in ASP.NET Core: A Privac… 142 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26293 views
  • Exception Handling Asp.Net Core 21063 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20449 views
  • How to implement Paypal in Asp.Net Core 19809 views
  • Task Scheduler in Asp.Net core 17816 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Products
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor