private PageHelper CreateNewPageHelper()
        {
            _printProcessor.CurrentPage++;
            _printProcessor.OnPageBreak();

            Brush  borderBrush;
            double gridTableHeight;
            var    table = _printProcessor.GetTable(out gridTableHeight, out borderBrush);

            var itemsControl = new ItemsControl();

            if (_printProcessor.ColorPrintPartsForDebug)
            {
                itemsControl.Background = ComputeBackGround(PrintAppendixes.All);
            }

            itemsControl.Height            = _printProcessor.PrintDimension.GetHeightForBodyGrid(CurrentPageNumber, false);
            itemsControl.Width             = _printProcessor.PrintDimension.PrintablePageSize.Width;
            itemsControl.VerticalAlignment = VerticalAlignment.Top;
            itemsControl.Items.Add(table);

            var pageHelper = new PageHelper();

            pageHelper.RemoveRemainingSpace(gridTableHeight);

            pageHelper.PrintingDimension = _printProcessor.PrintDimension;
            pageHelper.BodyGrid          = itemsControl;
            pageHelper.BodyGrid.Measure(new Size(double.MaxValue, double.MaxValue));
            pageHelper.BorderBrush = borderBrush;
            return(pageHelper);
        }
        private void AddLineItemPlaceContentItem(IPrintContent item, bool isLast)
        {
            var content = item.Content;

            content.Measure(new Size(_pageHelper.BodyGrid.DesiredSize.Width, double.MaxValue));
            var lineHeight = content.DesiredSize.Height;

            if (lineHeight >= _pageHelper.PrintingDimension.GetHeightForBodyGrid(CurrentPageNumber, isLast))
            {
                LogHeightWarning();
            }

            if (isLast)
            {
                PlaceLastItem(lineHeight, content);
                return;
            }

            if (_pageHelper.HasSpace(lineHeight, CurrentPageNumber, true))
            {
                AddLineData(content);
                _pageHelper.RemoveRemainingSpace(lineHeight);
            }
            else if (_pageHelper.HasSpace(lineHeight, CurrentPageNumber, false))
            {
                Debug.WriteLine("PRINTING: Second chance because item has no space");
                AddLineData(content);
                _pageHelper.RemoveRemainingSpace(lineHeight);
            }
            else
            {
                ConcludeDocumentPage(false);

                _pageHelper = CreateNewPageHelper();

                AddLineData(content);
                _pageHelper.RemoveRemainingSpace(lineHeight);
            }
        private void AddLineItem(IPrintContent item, bool isLast)
        {
            if (item is PageBreak)
            {
                ConcludeDocumentPage(_pageHelper, false);
                _pageHelper = CreateNewPageHelper();
            }
            else if (item is IPageBreakAware)
            {
                AddLineItem((IPageBreakAware)item, isLast);
            }
            else
            {
                var content = item.Content;

                content.Measure(new Size(_pageHelper.BodyGrid.DesiredSize.Width, double.MaxValue));
                var lineHeiht = content.DesiredSize.Height;

                if (lineHeiht < _pageHelper.PrintingDimension.GetHeightForBodyGrid(CurrentPageNumber, isLast))
                {
                    // OK
                }
                else
                {
                    var formattableString = $"Either reduce size of the line or consider deriving {item.GetType()} form {nameof(IPageBreakAware)}";

                    if (lineHeiht > _pageHelper.PrintingDimension.PageSize.Height)
                    {
                        Trace.TraceWarning($"{Description} page-size. {formattableString}");
                    }
                    else if (lineHeiht > _pageHelper.PrintingDimension.PrintablePageSize.Height)
                    {
                        Trace.TraceWarning($"{Description} printable-page-size. {formattableString}");
                    }
                    else if (lineHeiht > _pageHelper.PrintingDimension.GetHeightForBodyGrid(CurrentPageNumber, isLast))
                    {
                        Trace.TraceWarning($"{Description} body grid. {formattableString}");
                    }
                }

                if (isLast)
                {
                    // otherwise the last item is put on a new pageContent if desired, or it is left on the current pageContent and the PrintAppendixes that have no space would be print on the next pageContent
                    // should occur only if there are PrintAppendixes that have to be print on the last pageContent
                    Action concludePage = () =>
                    {
                        ConcludeDocumentPage(_pageHelper, false);
                        _pageHelper = CreateNewPageHelper();
                    };
                    Action addLastLineData = () =>
                    {
                        AddLineData(content);
                        Debug.WriteLine("PRINTING: Last item print");
                    };

                    Action <Action, Action> doAction = (first, second) =>
                    {
                        first();
                        second();
                        ConcludeDocument();
                    };

                    if (_pageHelper.HasSpace(lineHeiht, CurrentPageNumber, true))
                    {
                        // if (_printProcessor.BreakLastItemIfLastPageWouldBeEmpty)
                        // {
                        //     doAction(concludePage, addLastLineData);
                        // }
                        // else
                        {
                            AddLineData(content);
                            ConcludeDocumentPage(_pageHelper, true);
                        }
                    }
                    else
                    {
                        doAction(concludePage, addLastLineData);
                    }
                    return;
                }

                if (_pageHelper.HasSpace(lineHeiht, CurrentPageNumber, true))
                {
                    AddLineData(content);
                    _pageHelper.RemoveRemainingSpace(lineHeiht);
                }
                else if (_pageHelper.HasSpace(lineHeiht, CurrentPageNumber, false))
                {
                    Debug.WriteLine("PRINTING: Second chance because item has no space");
                    AddLineData(content);
                    _pageHelper.RemoveRemainingSpace(lineHeiht);
                }
                else
                {
                    ConcludeDocumentPage(_pageHelper, false);

                    _pageHelper = CreateNewPageHelper();

                    AddLineData(content);
                    _pageHelper.RemoveRemainingSpace(lineHeiht);
                }
            }
        }
        private void AddLineItem(IPrintContent item, bool isLast)
        {
            if (item is PageBreak)
            {
                ConcludeDocumentPage(_pageHelper, CurrentPageNumber, false);
                _pageHelper = CreateNewPageHelper();
                return;
            }

            var content = item.Content;

            content.Measure(new Size(_pageHelper.BodyGrid.DesiredSize.Width, double.MaxValue));
            var lineHeiht = content.DesiredSize.Height;

            if (isLast)
            {
                // otherwise the last item is put on a new pageContent if desired, or it is left on the current pageContent and the PrintAppendixes that have no space would be print on the next pageContent
                // should occur only if there are PrintAppendixes that have to be print on the last pageContent
                Action concludePage = () =>
                {
                    ConcludeDocumentPage(_pageHelper, CurrentPageNumber, false);
                    _pageHelper = CreateNewPageHelper();
                };
                Action addLastLineData = () =>
                {
                    AddLineData(content);
                    Trace.TraceInformation("Last item print");
                };

                Action <Action, Action> doAction = (first, second) =>
                {
                    first();
                    second();
                    ConcludeDocument();
                };

                if (_pageHelper.HasSpace(lineHeiht, CurrentPageNumber, true))
                {
                    // if (_printProcessor.BreakLastItemIfLastPageWouldBeEmpty)
                    // {
                    //     doAction(concludePage, addLastLineData);
                    // }
                    // else
                    {
                        AddLineData(content);
                        ConcludeDocumentPage(_pageHelper, CurrentPageNumber, true);
                    }
                }
                else
                {
                    doAction(concludePage, addLastLineData);
                }
                return;
            }

            if (_pageHelper.HasSpace(lineHeiht, CurrentPageNumber, true))
            {
                AddLineData(content);
                _pageHelper.RemoveRemainingSpace(lineHeiht);
            }
            else if (_pageHelper.HasSpace(lineHeiht, CurrentPageNumber, false))
            {
                Trace.TraceInformation("Second chance because item has no space");
                AddLineData(content);
                _pageHelper.RemoveRemainingSpace(lineHeiht);
            }
            else
            {
                ConcludeDocumentPage(_pageHelper, CurrentPageNumber, false);

                _pageHelper = CreateNewPageHelper();

                AddLineData(content);
                _pageHelper.RemoveRemainingSpace(lineHeiht);
            }
        }
        private void AddLineItem(IPrintContent item, bool isLast)
        {
            if (item is PageBreak)
            {
                ConcludeDocumentPage(_pageHelper, false);
                _pageHelper = CreateNewPageHelper();
            }
            else if (item is IPageBreakAware pageBreakAware)
            {
                AddLineItem(pageBreakAware, isLast);
            }
            else if (item is IDirectPrintContent directPrintContent)
            {
                var position = new Point(directPrintContent.Position.X + _pageHelper.PrintingDimension.Margin.Left, directPrintContent.Position.Y + _pageHelper.PrintingDimension.Margin.Top);
                PositionUiElement(_pageHelper.PageContent, item.Content, position);
            }
            else
            {
                var content = item.Content;

                content.Measure(new Size(_pageHelper.BodyGrid.DesiredSize.Width, double.MaxValue));
                var lineHeight = content.DesiredSize.Height;

                if (lineHeight < _pageHelper.PrintingDimension.GetHeightForBodyGrid(CurrentPageNumber, isLast))
                {
                    // OK
                }
                else
                {
                    var helpText = $"Either reduce size of the line or consider deriving {item.GetType()} form {nameof(IPageBreakAware)}";

                    if (lineHeight > _pageHelper.PrintingDimension.PageSize.Height)
                    {
                        Trace.TraceWarning($"{Description} page-size. {helpText}");
                    }
                    else if (lineHeight > _pageHelper.PrintingDimension.PrintablePageSize.Height)
                    {
                        Trace.TraceWarning($"{Description} printable-page-size. {helpText}");
                    }
                    else if (lineHeight > _pageHelper.PrintingDimension.GetHeightForBodyGrid(CurrentPageNumber, isLast))
                    {
                        Trace.TraceWarning($"{Description} body grid. {helpText}");
                    }
                }

                if (isLast)
                {
                    // otherwise the last item is put on a new pageContent if desired, or it is left on the current
                    // pageContent and the PrintAppendixes that have no space would be print on the next pageContent
                    // should occur only if there are PrintAppendixes that have to be print on the last pageContent
                    if (_pageHelper.HasSpace(lineHeight, CurrentPageNumber, true))
                    {
                        AddLineData(content);
                        ConcludeDocumentPage(_pageHelper, true);
                    }
                    else
                    {
                        ConcludePage();
                        AddLastLineData(content);
                        ConcludeDocument();
                    }
                    return;
                }

                if (_pageHelper.HasSpace(lineHeight, CurrentPageNumber, true))
                {
                    AddLineData(content);
                    _pageHelper.RemoveRemainingSpace(lineHeight);
                }
                else if (_pageHelper.HasSpace(lineHeight, CurrentPageNumber, false))
                {
                    Debug.WriteLine("PRINTING: Second chance because item has no space");
                    AddLineData(content);
                    _pageHelper.RemoveRemainingSpace(lineHeight);
                }
                else
                {
                    ConcludeDocumentPage(_pageHelper, false);

                    _pageHelper = CreateNewPageHelper();

                    AddLineData(content);
                    _pageHelper.RemoveRemainingSpace(lineHeight);
                }
            }
        }