Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created April 10, 2025 14:30
Show Gist options
  • Save bjoerntx/0cba331e3cb36245f78077f9d6342389 to your computer and use it in GitHub Desktop.
Save bjoerntx/0cba331e3cb36245f78077f9d6342389 to your computer and use it in GitHub Desktop.
using TXTextControl;
class LineItem
{
public string Name { get; set; }
public string Description { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}
class DocumentGenerator
{
const int RowCount = 4;
const int ColumnCount = 5;
public void CreateDocument()
{
using (ServerTextControl tx = new ServerTextControl())
{
tx.Create();
// Add table
tx.Tables.Add(RowCount, ColumnCount);
Table table = tx.Tables[1];
// Define formats
var currencyFormat = new TableCellFormat
{
TextType = TextType.Number,
NumberFormat = "$#,##0.00"
};
// Set column formats
table.Columns[4].CellFormat = currencyFormat;
table.Columns[5].CellFormat = currencyFormat;
// Add line item
var lineItem = new LineItem
{
Name = "Sample Item",
Description = "This is a sample item description.",
Quantity = 10,
Price = 19.99m
};
// Fill rows with data and formulas
for (int row = 1; row < RowCount; row++)
{
table.Cells[row, 1].Text = lineItem.Name;
table.Cells[row, 2].Text = lineItem.Description;
table.Cells[row, 3].Text = lineItem.Quantity.ToString();
table.Cells[row, 4].Text = lineItem.Price.ToString();
table.Cells[row, 5].Formula = $"SUM(C{row}*D{row})";
}
// Set total formula in last row, last column
table.Cells[RowCount, ColumnCount].Formula = "SUM(above)";
// Save document
tx.Save("results.docx", StreamType.WordprocessingML);
}
}
}
class Program
{
static void Main(string[] args)
{
DocumentGenerator generator = new DocumentGenerator();
generator.CreateDocument();
Console.WriteLine("Document created successfully.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment