0

I am trying to create a Paginator that will in turn create an XpsDocument that I can preview with the and then print. I have the following code that I found on the web but do not understand how it is working.

The issue I am having is that if I run this as is, the pages are generated successfully. If I comment out the lines within OnRender() that generate the actual values of data (all the lines after Random...) I get pages that are about one row high and no text on them but they appear to be the correct length. What is it that is keeping the values of "Row Number" & "Column i" from being shown?

I have included 2 screen shots to illustrate.

public class TaxCodePrintPaginator : DocumentPaginator
{
    private int _RowsPerPage;
    private Size _PageSize;
    private int _Rows;
    private List<TaxCode> _dataList;

    public TaxCodePrintPaginator(List<TaxCode> dt, int rows, Size pageSize)
    {
        _dataList = dt;
        _Rows = rows;
        PageSize = pageSize;
    }

    public override DocumentPage GetPage(int pageNumber)
    {
        int currentRow = _RowsPerPage * pageNumber;
        var page = new PageElement(currentRow, Math.Min(_RowsPerPage, _Rows - currentRow))
        {
            Width = PageSize.Width,
            Height = PageSize.Height
        };

        page.Arrange(new Rect(new Point(0, 0), PageSize));     

        return new DocumentPage(page);
    }

    public override bool IsPageCountValid
    { get { return true; } }

    public override int PageCount
    { get { return (int)Math.Ceiling(_Rows / (double)_RowsPerPage); } }

    public override Size PageSize
    {
        get { return _PageSize; }
        set
        {
            _PageSize = value;

            _RowsPerPage = 40;

            //Can't print anything if you can't fit a row on a page
            Debug.Assert(_RowsPerPage > 0);
        }
    }

    public override IDocumentPaginatorSource Source
    { get { return null; } }
}


public class PageElement : UserControl
{
    private const int PageMargin = 75;
    private const int HeaderHeight = 25;
    private const int LineHeight = 20;
    private const int ColumnWidth = 140;

    private int _CurrentRow;
    private int _Rows;

    public PageElement(int currentRow, int rows)
    {
        Margin = new Thickness(PageMargin);
        _CurrentRow = currentRow;
        _Rows = rows;
    }

    private static FormattedText MakeText(string text)
    {
        return new FormattedText(text, CultureInfo.CurrentCulture,
          FlowDirection.LeftToRight,
          new Typeface("Tahoma"), 14, Brushes.Black);
    }

    public static int RowsPerPage(double height)
    {
        return (int)Math.Floor((height - (2 * PageMargin)
          - HeaderHeight) / LineHeight);
    }

    protected override void OnRender(DrawingContext dc)
    {

        Point curPoint = new Point(0, 0);
        dc.DrawText(MakeText("Row Number"), curPoint);


        curPoint.X += ColumnWidth;
        for (int i = 1; i < 4; i++)
        {
            dc.DrawText(MakeText("Column " + i), curPoint);
            curPoint.X += ColumnWidth;
        }

        curPoint.X = 0;
        curPoint.Y += LineHeight;

        dc.DrawRectangle(Brushes.Black, null, new Rect(curPoint, new Size(Width, 2)));
        curPoint.Y += HeaderHeight - LineHeight;

        Random numberGen = new Random();
        for (int i = _CurrentRow; i < _CurrentRow + _Rows; i++)
        {
            dc.DrawText(MakeText(i.ToString()), curPoint);
            curPoint.X += ColumnWidth;
            for (int j = 1; j < 4; j++)
            {
                dc.DrawText(MakeText(numberGen.Next().ToString()), curPoint);
                curPoint.X += ColumnWidth;
            }
            curPoint.Y += LineHeight;
            curPoint.X = 0;
        }
    }
}

Before Before commenting out lines of code

After After commenting out lines of code

BrianKE
  • 4,035
  • 13
  • 65
  • 115
  • Where are the screenshots? – Jon Nov 14 '13 at 18:06
  • @Jon I think SO was having an issue when I posted this originally. I have added them to the OP. – BrianKE Nov 14 '13 at 18:15
  • I don't see anything obviously wrong with the code. Have you tried unzipping the resultant xps document and looking to see if the text was actually added? My best guess right now is that the text was added, but something went wrong with the page size. – Jon Nov 14 '13 at 22:10

0 Answers0