I need to generate some reports and the selected format has been FlowDocuments. As I’m generating them thru code I find myself all the time doing things like this:
list.ListItems.Add(new ListItem(new Paragraph(new Run(string.Format("XXX XXXXXX: {0}", _result.XXX.Count)))));
list.ListItems.Add(new ListItem(new Paragraph(new Run(string.Format("XXX XXXXXX: {0}", _result.XXX())))));
list.ListItems.Add(new ListItem(new Paragraph(new Run(string.Format("XXX XXXXXX: {0}", _result.LossOperations())))));
When I have to add a single line of text I have to do something like:
Paragraph auxParagraph = new Paragraph();
auxParagraph.Inlines.Add("Executing time " + _result.ExecutingTime.ToString());
report.Blocks.Add(auxParagraph);
Looks a bit overkill to me. I’m using the API in the right way? All I see documented talks about creating the documents by hand. Maybe they are not intended to be created by code and should we check another format? Or maybe are there other higher level APIs that make the work with the FlowDocuments easier?
Thanks!
Edit: just another example working with tables:
foreach (DateTime availableDay in _result.Generator.AvailableDays)
{
Table table = new Table();
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());
table.Columns.Add(new TableColumn());
TableRowGroup headers = new TableRowGroup();
TableRow headersRow = new TableRow();
headersRow.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
headersRow.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
headersRow.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
headersRow.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
headersRow.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
headers.Rows.Add(headersRow);
table.RowGroups.Add(headers);
DateTime day = availableDay;
TableRowGroup valuesGroup = new TableRowGroup();
foreach (Operation operation in dailyOperations)
{
TableRow row = new TableRow();
row.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
row.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
row.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
row.Cells.Add(new TableCell(new Paragraph(new Run("XXX XXXX"))));
row.Cells.Add(new TableCell(CreateColoredValue(operation.Result)));
valuesGroup.Rows.Add(row);
}
table.RowGroups.Add(valuesGroup);
result.Blocks.Add(table);
}
Your list-with-numbers is kind of a special case. I would create a few overloaded methods:
And you would have
You can tame it further with few methods for ListItem, Block etc.