Skip to content

Instantly share code, notes, and snippets.

@bjoerntx
Created August 8, 2023 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjoerntx/2081273cc8eb0bd258a56eceb5743c28 to your computer and use it in GitHub Desktop.
Save bjoerntx/2081273cc8eb0bd258a56eceb5743c28 to your computer and use it in GitHub Desktop.
private void TextControl1_TableCreated(out string dataStructure, object sender, TableEventArgs e) {
List<ApplicationField> applicationFields = new List<ApplicationField>();
// adapt the table size to the page width
e.Table.AdaptSize(textControl1);
// loop through all cells
foreach (TableCell cell in e.Table.Cells) {
// if the cell is not a formula, has a length and is not empty
if (cell.Formula == "" &&
cell.Length != -1 &&
cell.Text != "" &&
cell.CellFormat.TextType != TextType.Standard) {
cell.Select();
// create a new merge field
MergeField mf = new MergeField();
mf.Text = "«" + cell.Text + "»"; // set the field text
cell.Text = "";
// get the text of the previous cell in the first column
// and make it unique by adding the column number
var previousCellText = e.Table.Cells.GetItem(cell.Row, 1).Text + "_" + cell.Column;
// remove spaces from previousCellText and convert to title case
mf.Name = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ToTitleCase(previousCellText.ToLower()).Replace(" ", "");
// add the merge field to the document
textControl1.Select(cell.Start - 1, 0);
textControl1.ApplicationFields.Add(mf.ApplicationField);
applicationFields.Add(mf.ApplicationField);
}
}
dataStructure = GetMergeFields(applicationFields);
}
// function to return a json structure from an array of merge fields
private string GetMergeFields(List<ApplicationField> applicationFields) {
var fields = new List<string[]>();
foreach (ApplicationField field in applicationFields) {
fields.Add(new string[] { field.Name, field.Text });
}
return JsonConvert.SerializeObject(fields);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment