这个错误有很多点击,但没有一个能够解决我的问题。 我有一个数据网格运行到多个页面,我想将其组合成一个固定文档,而不是单独打印每一页。 我的代码来自已发布的来源,但尽管尝试从其现有父级中删除 uiElement,但我仍然收到此错误。 我正在打印的网格不仅包含数据网格(它被连接起来,以便只有选定的行出现在相关页面上),还包含包含组合框、文本框或文本块的页眉和页脚,其中大多数是折叠的,其中一些是激活的(例如页码文本框),用于打印。
我的代码(调用 PrintDialog 后)是:
//Create FixedDocument
FixedDocument document = new FixedDocument();
document.DocumentPaginator.PageSize = new Size(printDlg.PrintableAreaWidth, printDlg.PrintableAreaHeight);
//Add pages
ObservableCollection<FixedPage> pages = new ObservableCollection<FixedPage>();
for (pageNumber = 1; pageNumber <= lengthPages; pageNumber++)
{
pages.Add(new FixedPage());
//Expand PrintingGrid so that it includes all necessary datagrid rows
grdTransactions.Height = (pageNumber == lengthPages && totalsLine) ? bdrReconciliation.Visibility == Visibility.Visible? 520: 660 : 710;//Compensate for summary box
if (reconciliation)
{
bdrReconciliation.Visibility = (totalsLine && pageNumber == lengthPages) ? Visibility.Visible : Visibility.Collapsed; //Prints summary panel on last page
}
else
{
bdrTotalsLine.Visibility = (totalsLine && pageNumber == lengthPages) ? Visibility.Visible : Visibility.Collapsed; //Prints summary panel on last page
}
grdPastTransactions.UpdateLayout();
collapseHeaders(true, pageNumber, lengthPages, reconciliation);
if (pageNumber == 1)
{
dgdTransactions.ItemsSource = displayTransactions.Take(firstPageLines);
dgdTransactions.UpdateLayout();
}
else
{
dgdTransactions.ItemsSource = displayTransactions.Skip(firstPageLines + (firstPageLines * (pageNumber - 2))).Take(firstPageLines);
dgdTransactions.UpdateLayout();
}
grdPastTransactions.UpdateLayout();
if (pageRowCount < actualRowCount || (pageRowCount >= actualRowCount && totalsLine))
{
Grid thisGrid = new Grid();
thisGrid = grdPastTransactions;
UIElement uiElement = thisGrid;
var parent = VisualTreeHelper.GetParent(thisGrid) as Grid;
if (parent != null)
{
parent.Children.Remove(uiElement);
}
pages[pageNumber - 1].Width = document.DocumentPaginator.PageSize.Width;
pages[pageNumber - 1].Height = document.DocumentPaginator.PageSize.Height;
pages[pageNumber - 1].Children.Add(thisGrid);
//Add page to document
PageContent page1Content = new PageContent();
((IAddChild)page1Content).AddChild(pages[pageNumber - 1]);
document.Pages.Add(page1Content);
}
pageRowCount += pageNumber == 1 ? Math.Min(actualRowCount, firstPageLines) : 28;
}
//Print document
PrintMethods.PrintHelper.PrintNoPreview(printDlg, document);
和
public static void PrintNoPreview(PrintDialog printDialog, FixedDocument fixedDoc)
{
try
{
printDialog.PrintDocument(fixedDoc.DocumentPaginator, "PMM Report");
}
catch (System.Runtime.CompilerServices.RuntimeWrappedException)
{
MessageBox.Show("You are trying to over-write an existing PDF document of the same name which is open." +
"\n\nPlease close the old document and try again, or save the new document with a new name.", "Save to PDF failed", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
我尝试仅将数据网格本身添加到页面中,而不添加任何其他元素,但得到了相同的结果。我还尝试在创建固定文档之前使用 grdPastTransactions.Visibility=Visibility.Collapsed 从视图中删除网格(grdPastTransactions),但这也不起作用。
为什么我无法将页面添加到固定文档而不生成此错误? 我是否需要创建一个全新的网格(包含所有新元素)用于打印?
我已经解决了“指定的子元素是另一个元素的逻辑子元素”错误的问题。 与我原来的单页打印一样,需要将网格添加到 VisualBrush,然后创建图像的 Canvas。 可以迭代调用此方法以将页面组合成单个固定文档。
不幸的是,我仍然遇到问题 - 我创建的文档包含最后一页的 3 个副本,而不是第 1、2 和 3 页,因此每次迭代都会覆盖前一页。 由于这是一个新问题,我已在 Stackoverflow here