I’m trying to print a WPF FlowDocument. The layout needs to be in the form of 4 documents per page, laid out as follows:
Doc1 | Doc2
-------------
Doc3 | Doc4
(Sorry, I couldn’t come up with a better way of illustrating the layout).
The page needs to fill, so if Doc1 & 2 are blank or just one or two characters, it still needs to print them the same size as Doc3 & 4.
The code I’m using is as follows (sorry it’s long, I’ve tried to abridge where feasible):
PrintDialog printDialog = new PrintDialog();
if ((bool)printDialog.ShowDialog().GetValueOrDefault())
{
FlowDocument flowDocument = new FlowDocument();
flowDocument.PageHeight = printDialog.PrintableAreaHeight;
flowDocument.PageWidth = printDialog.PrintableAreaWidth;
flowDocument.PagePadding = new Thickness(25);
flowDocument.ColumnGap = 0;
flowDocument.ColumnWidth = (flowDocument.PageWidth -
flowDocument.ColumnGap -
flowDocument.PagePadding.Left -
flowDocument.PagePadding.Right);
Table myTable = new Table();
myTable.BorderThickness = new Thickness(3);
AddCols(myTable); // Add 2 cols
TableRowGroup rg = new TableRowGroup();
TableRow row = new TableRow();
AddRows(myTable); // Adds 2 rows
TableCell cell = new TableCell(new Paragraph(new Run("Doc1")));
cell.BorderThickness = new Thickness(1);
cell.BorderBrush = Brushes.Black;
// Repeat 4 times
row.Cells.Add(cell);
myTable.RowGroups.Add(rg);
doc.Blocks.Add(myTable);
....
The problem that I have is that although this does print, it doesn’t try to fit it to the page as described above. Is what I am attempting possible and if so, how?
EDIT:
From looking here I believe what I actually need is a way to calculate the height of the paragraph, so that I can set the Padding property. Unfortunately the solution proposed in this link doesn’t work!
Try placing the entire block in a grid so as to give it the uniform layout and then place the grid in the block and block inside your single table cell. See if this works for you –