Generate Your First Document from .NET Object

Overview

This guide walks you through creating your first document with GroupDocs.Assembly for .NET. You’ll generate a simple document from an in-memory .NET object, demonstrating the basic workflow of template-based document assembly.

The main classes involved are:

Here are the steps to generate your first document:

  • Create a simple .NET class with properties to represent your data
  • Instantiate the class and populate it with sample data
  • Create a template document (DOCX) with expression tags referencing the object properties
  • Use DocumentAssembler to assemble the template with your data object
  • Save the assembled document to a file

Define a Simple Data Class

First, create a simple class to hold your data:

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public string Email { get; set; }
}

Generate Document from Object

Now, create a method to generate a document from this object:

using GroupDocs.Assembly;
using System;

public static void GenerateFirstDocument()
{
    // Create sample data
    Person person = new Person
    {
        Name = "John Doe",
        Age = 30,
        Email = "john.doe@example.com"
    };
    
    // Paths for template and output
    string templatePath = "Template.docx";
    string outputPath = "Output.docx";
    
    // Assemble the document
    DocumentAssembler assembler = new DocumentAssembler();
    assembler.AssembleDocument(templatePath, outputPath, 
        new DataSourceInfo(person, "person"));
    
    Console.WriteLine($"Document generated successfully: {outputPath}");
}

Template Document Example

Your template document (Template.docx) should contain expression tags like this:

Name: <<[person.Name]>>
Age: <<[person.Age]>>
Email: <<[person.Email]>>

After assembly, the output will be:

Name: John Doe
Age: 30
Email: john.doe@example.com

Using Contextual Object Member Access

You can also use contextual member access, which allows you to omit the data source name in simple scenarios:

public static void GenerateDocumentWithContextualAccess()
{
    Person person = new Person
    {
        Name = "Jane Smith",
        Age = 28,
        Email = "jane.smith@example.com"
    };
    
    DocumentAssembler assembler = new DocumentAssembler();
    // When using contextual access, you can omit the data source name
    assembler.AssembleDocument("Template.docx", "Output.docx", 
        new DataSourceInfo(person));
}

With contextual access, your template can use:

Name: <<[Name]>>
Age: <<[Age]>>
Email: <<[Email]>>
Note
When using contextual object member access, the template expressions can reference properties directly without the data source name prefix. This is useful for simple, single-object scenarios.

Advanced Usage Topics

To learn more about working with complex data structures, multiple data sources, and advanced template syntax, please refer to the advanced usage section.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online Apps

Along with the full-featured .NET library, we provide simple but powerful free online apps.

To assemble documents from templates and data sources, you can use the online apps from the GroupDocs.Assembly App Product Family.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.