我正在尝试打印WPF FlowDocument。布局需要以每页4个文档的形式,布局如下:
Doc1 | Doc2
-------------
Doc3 | Doc4
(抱歉,我无法想出更好的方式来说明布局)。
页面需要填写,所以如果Doc1& 2是空白或只有一两个字符,它仍然需要打印与Doc3& 2相同的大小。 4。
我正在使用的代码如下(对不起,很长时间,我试图在可行的情况下删除):
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);
....
我遇到的问题是虽然这会打印,但它不会尝试将其放到页面上,如上所述。我尝试的是什么,如果可能,怎么样?
编辑:
从查看here我相信我真正需要的是一种计算段落高度的方法,以便我可以设置Padding属性。不幸的是,此链接中提出的解决方案不起作用!
答案 0 :(得分:2)
尝试将整个块放在网格中,以便为其提供统一的布局,然后将网格放在块中并阻塞在单个表格单元格内。看看这是否适合你 -
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions.Add(new RowDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
Label text1 = new Label();
text1.Content = "Doc1";
grid.Children.Add(text1);
Grid.SetColumn(text1, 0);
Grid.SetRow(text1, 0);
Label text2 = new Label();
text1.Content = "Doc2";
grid.Children.Add(text2);
Grid.SetColumn(text2, 1);
Grid.SetRow(text2, 0);
Label text3 = new Label();
text1.Content = "Doc3";
grid.Children.Add(text3);
Grid.SetColumn(text3, 0);
Grid.SetRow(text3, 1);
Label text4 = new Label();
text1.Content = "Doc4";
grid.Children.Add(text4);
Grid.SetColumn(text4, 1);
Grid.SetRow(text4, 1);
BlockUIContainer block = new BlockUIContainer(grid);
Table table = new Table();
TableRowGroup rg = new TableRowGroup();
TableCell cell = new TableCell();
cell.Blocks.Add(block);
TableRow row = new TableRow();
row.Cells.Add(cell);
rg.Rows.Add(row);
table.RowGroups.Add(rg);
doc.Blocks.Add(table);
答案 1 :(得分:1)
您可以创建自己的自定义DocumentPaginator。见这里:
http://www.codeproject.com/Articles/164033/WPF-Visual-Print-Component
http://www.codeproject.com/Articles/31834/FlowDocument-pagination-with-repeating-page-header
http://www.switchonthecode.com/tutorials/wpf-printing-part-2-pagination
http://www.codeproject.com/Articles/138233/Custom-Data-Grid-Document-Paginator
答案 2 :(得分:0)
这是你要找的吗?
答案 3 :(得分:0)
问题,您将单元格添加到row.cell并将行组添加到表中,但是您是否将行添加到行组?