NKB.DocumentGenerator 1.0.2

dotnet add package NKB.DocumentGenerator --version 1.0.2
                    
NuGet\Install-Package NKB.DocumentGenerator -Version 1.0.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="NKB.DocumentGenerator" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NKB.DocumentGenerator" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="NKB.DocumentGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NKB.DocumentGenerator --version 1.0.2
                    
#r "nuget: NKB.DocumentGenerator, 1.0.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package NKB.DocumentGenerator@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NKB.DocumentGenerator&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=NKB.DocumentGenerator&version=1.0.2
                    
Install as a Cake Tool

DocumentGeneratorService

This C# package provides a service for generating Word documents with customizable text, image, and repeating section replacements based on a provided template. The service uses the DocumentFormat.OpenXml library to manipulate Word documents.

Features

  • Replace text placeholders within a document.
  • Replace images by placeholders with base64-encoded images.
  • Repeat sections (foreach) based on data arrays, useful for repeating tables or lists within the document.

Installation

  1. Install the DocumentFormat.OpenXml package if not already included:

    dotnet add package DocumentFormat.OpenXml
    
  2. Add this package to your project to utilize the DocumentGeneratorService class.

Usage

Interface

The IDocumentGeneratorService interface defines the contract for the DocumentGeneratorService:

namespace DocumentGenerator.Interfaces
{
    public interface IDocumentGeneratorService
    {
        byte[] GenerateDocument(
            string base64Template, 
            Dictionary<string, string> textReplacements, 
            Dictionary<string, string> imageReplacements, 
            Dictionary<string, List<Dictionary<string, string>>> foreachReplacements
        );
    }
}

Code Example

The following example demonstrates how to use DocumentGeneratorService:

using DocumentGenerator.Services;
using System.Collections.Generic;

var generator = new DocumentGeneratorService();
var base64Template = "your_base64_encoded_template_here";
var textReplacements = new Dictionary<string, string>
{
    { "{Name}", "John Doe" },
    { "{Date}", "2024-11-11" }
};
var imageReplacements = new Dictionary<string, string>
{
    { "{ImagePlaceholder}", "base64_encoded_image_here" }
};
var foreachReplacements = new Dictionary<string, List<Dictionary<string, string>>>
{
    { "{Items}", new List<Dictionary<string, string>> {
        new Dictionary<string, string> { { "{ItemName}", "Item 1" }, { "{Price}", "$10" } },
        new Dictionary<string, string> { { "{ItemName}", "Item 2" }, { "{Price}", "$15" } }
    }}
};

byte[] resultDocument = generator.GenerateDocument(base64Template, textReplacements, imageReplacements, foreachReplacements);

Parameters

  • base64Template: The base64-encoded string of the Word document template.
  • textReplacements: A dictionary of text placeholders to be replaced within the document.
  • imageReplacements: A dictionary of image placeholders and their corresponding base64-encoded images.
  • foreachReplacements: A dictionary of repeating sections. Each item contains lists of dictionaries, allowing for multiple rows or items to be inserted based on the template's placeholders.

Methods

GenerateDocument

Main method for generating the document with specified replacements.

ReplaceText

Replaces text placeholders in the document.

ReplaceImage

Replaces image placeholders with images provided as base64 strings.

ReplaceForeach

Handles repeating sections in the document by replacing blocks based on list data.

Requirements

  • .NET 5.0 or higher
  • DocumentFormat.OpenXml package

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.2 274 11/11/2024
1.0.1 240 11/11/2024
1.0.0 246 11/11/2024