1

I have a Blazor app that connects to an existing backend. The Blazor app needs to print data from a List<T>. What is the correct way of doing this?

In the WPF world, I would use a DocumentPaginator, however, since a DocumentPaginator GetPage method returns a DocumentPage which is a WPF Visual element this is out.

I have some luck creating a FlowDocument Something like this:

var doc = new FlowDocument();
var table1 = new Table();
doc.Blocks.Add(table1);
for (int x = 0; x < numberOfFields; x++)
{
    table1.Columns.Add(new TableColumn());
}
table1.RowGroups.Add(new TableRowGroup());
int i = 0;
foreach (var x in MyList)
{
    table1.RowGroups[0].Rows.Add(new TableRow());   
    TableRow currentRow = table1.RowGroups[0].Rows[i];
    AddFields(currentRow, i, x);
    i++;
}

That prints out a document however, I need page headers and footers. Placing headers, footers, and page breaks can be done with Paragraph and Section are simple enough. The problem is calculating when to apply the page break. Everything I have seen to find out the height of something in FlowDocument uses a FrameworkElement to find it.

Very new to web, so if there is some simple solution I am sorry for asking.

Xaphann
  • 3,195
  • 10
  • 42
  • 70
  • 3
    Are you sure this is a Blazor question? – Peter Morris Jun 13 '20 at 09:52
  • 1
    Maybe look to create a pdf. I did a little tutorial on that : https://github.com/tossnet/Blazor-PDF – Toss Net Jun 13 '20 at 10:30
  • 1
    Using the FlowDocument model is only making extra work. You can make up a page with HTML and blazor, a much richer toolkit. In the end you wil have to accomplish the paging with CSS anyway. See [this](https://stackoverflow.com/q/1360869/60761) and [this](https://medium.com/@Idan_Co/the-ultimate-print-html-template-with-header-footer-568f415f6d2a) and goole for much more. – H H Jun 14 '20 at 07:10

1 Answers1

6

In combination with TossNet to generate the .pdf file you can use Append.Blazor.Printing to show the print dialog.

The repository can be found here: https://github.com/Append-IT/Blazor.Printing

enter image description here

Verbe
  • 634
  • 7
  • 13