我一直在研究Muhammad Mehdi here发布的解决方案,并将名为List<Person>
的{{1}}挂接到People
中以使用打印机打印PDF。这是我到目前为止在FlowDocument
,ICommand
中所做的事情:
PrintDoc
我尝试设置void PrintDoc(object obj)
{
var People = new List<Person>();
for (int i = 0; i < 100; i++)
{
People.Add(new Person()
{
Firstname = "First Name " + i,LastName = "Last Name " + i,Age = i + 30,Salary = (i + 1) * 1000
});
}
FlowDocument fd = new FlowDocument();
fd.ColumnWidth = 816;
Paragraph para = new Paragraph(new Run("List of People"));
para.TextAlignment = TextAlignment.Center;
para.FontWeight = FontWeights.Bold;
para.Margin = new Thickness(96,96,24);
para.FontSize = 24;
fd.Blocks.Add(para);
Table table = new Table();
table.Margin = new Thickness(96,0);
TableRowGroup tableRowGroup = new TableRowGroup();
TableRow r = new TableRow();
var props = typeof(Person).GetProperties();
for (int i = 0; i < props.Length; i++)
{
var cell = new TableCell(new Paragraph(new Run(props[i].Name)));
cell.FontWeight = FontWeights.Bold;
if (props[i].Name == "Salary" || props[i].Name == "Age")
cell.TextAlignment = TextAlignment.Right;
r.Cells.Add(cell);
}
tableRowGroup.Rows.Add(r);
tableRowGroup.Background = new SolidColorBrush(Colors.Wheat);
table.RowGroups.Add(tableRowGroup);
for (int i = 0; i < People.Count; i++)
{
tableRowGroup = new TableRowGroup();
r = new TableRow();
r.Cells.Add(new TableCell(new Paragraph(new Run(People[i].Firstname))));
r.Cells.Add(new TableCell(new Paragraph(new Run(People[i].LastName))));
var salaryCell = new TableCell(new Paragraph(new Run(People[i].Salary.ToString("N0"))));
var ageCell = new TableCell(new Paragraph(new Run(People[i].Age.ToString())));
salaryCell.TextAlignment = TextAlignment.Right;
ageCell.TextAlignment = TextAlignment.Right;
r.Cells.Add(salaryCell);
r.Cells.Add(ageCell);
tableRowGroup.Rows.Add(r);
table.RowGroups.Add(tableRowGroup);
}
fd.Blocks.Add(table);
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator,"");
}
和fd.PageWidth
来确保页面是fd.PageHeight
的{{1}},但这没有用!因此,我设置了8.5"
来确保它是11.7"
!除此之外,我还想在文档周围留个fd.ColumnWidth = 816
的边距,但找不到任何有意义的方法,因此我在左侧,顶部和右侧的8.5 inch
边距设置了1 inch
并再次为左右para.Margin = new Thickness(96,24)
的边距设置1 inch
。
我不确定苏门答腊PDF中的这些代码BUT使我的页面尺寸变成什么样子,看来我只在左右两侧留有边距,顶部没有留空!在Sumatra PDF的文件属性中,它表示为table.Margin = new Thickness(96,0)
,我希望它为1 inch
。我是否必须像在代码中那样设置页面宽度和边距,或者有更简单的方法来做到这一点?
我想要做的另一件事是,当页面流过多个页面时,为所有页面设置标题和标题。在这种情况下,我要
人员名单
名字姓氏工资年龄
位于所有3页的顶部。怎么做?
在Letter (8.5" x 11")
中,我可以使用8.5" x 11.7"
;在Table
中,我可以使用一个或多个TableRow
,但在这种情况下,我使用的是{{ 1}}来保持我的行。我可以避免使用中介TableRow
吗?
在我只想打印带有某些标题和标题的列表的情况下,Cell
是最好的解决方案吗?
编辑
为了使文档周围的页边距均匀,我找到了this,因此对于页边距,我需要1行:
TableRowGroup
我可以摆脱它:
TableRowGroup
对于页面大小,我必须进行以下设置:
FlowDocument
编辑
现在的问题是fd.PagePadding = new Thickness(96);
是否是最好的,以及如何在所有页面中添加该标题和表格标题。我能想到的一种方法是用这些标题/标题预先确定页面中的最大行数,然后循环(对于每一页)添加标题,表格标题和这么多行!
那是唯一的方法吗?
编辑
它与fd.ColumnWidth = 816;
para.Margin = new Thickness(96,24);
table.Margin = new Thickness(96,0);
配合使用,可以在打印之前预览文档。我只需要像这样将fd.PageWidth = 816;
fd.PageHeight = 1123.2;
绑定到另一个FlowDocument
中:
FlowDocumentPageViewer
,在FlowDocument
中,我必须续订Preview Window
,将<FlowDocumentPageViewer Document="{Binding MyFlowDoc}" />
设置为PrintPreview ICommand
并调用Preview Window
。为了使其在DataContext
中看起来更好,我必须设置this
的长宽比,使其与previewWindow.Show()
的长宽比相匹配。真好!