我正在尝试打印一些标记纸。我创建了WPF窗口并以这种方式打印整个窗口:
PrintDialog pd = new System.Windows.Controls.PrintDialog();
if (pd.ShowDialog() == true)
{
pd.PrintVisual(this.Content as Visual, "Report");
}
然后我想在文档中添加更多标记表:在该窗口中插入每个学生标记,然后将其添加到页面中。 然后以这种方式打印单个文件:
PrintDialog pd = new System.Windows.Controls.PrintDialog();
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new System.Windows.Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight);
FixedPage page1 = new FixedPage();
page1.Width = document.DocumentPaginator.PageSize.Width;
page1.Height = document.DocumentPaginator.PageSize.Height;
page1.Children.Add(this);
PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(page1);
document.Pages.Add(page1Content);
FixedPage page2 = new FixedPage();
page2.Width = document.DocumentPaginator.PageSize.Width;
page2.Height = document.DocumentPaginator.PageSize.Height;
page2.Children.Add(this);
PageContent page2Content = new PageContent();
((IAddChild)page2Content).AddChild(page2);
document.Pages.Add(page2Content);
if (pd.ShowDialog() == true)
{
pd.PrintDocument(document.DocumentPaginator, "My first document");
}
但是问题是当我将Window(this)添加到页面作为元素时:
page1.Children.Add(this);
我遇到Window must be the root of the tree.Cannot add Window as a child of Visual. Source=< Cannot evaluate the exception source>
错误。