NKB.DocumentGenerator
1.0.2
dotnet add package NKB.DocumentGenerator --version 1.0.2
NuGet\Install-Package NKB.DocumentGenerator -Version 1.0.2
<PackageReference Include="NKB.DocumentGenerator" Version="1.0.2" />
<PackageVersion Include="NKB.DocumentGenerator" Version="1.0.2" />
<PackageReference Include="NKB.DocumentGenerator" />
paket add NKB.DocumentGenerator --version 1.0.2
#r "nuget: NKB.DocumentGenerator, 1.0.2"
#:package NKB.DocumentGenerator@1.0.2
#addin nuget:?package=NKB.DocumentGenerator&version=1.0.2
#tool nuget:?package=NKB.DocumentGenerator&version=1.0.2
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
Install the
DocumentFormat.OpenXmlpackage if not already included:dotnet add package DocumentFormat.OpenXmlAdd this package to your project to utilize the
DocumentGeneratorServiceclass.
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.OpenXmlpackage
License
This project is licensed under the MIT License.
| Product | Versions 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. |
-
net8.0
- DocumentFormat.OpenXml (>= 3.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.