Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Create XpsDocument Pagination
    text
    copied!<p>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. </p> <p>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" &amp; "Column i" from being shown?</p> <p>I have included 2 screen shots to illustrate.</p> <pre><code>public class TaxCodePrintPaginator : DocumentPaginator { private int _RowsPerPage; private Size _PageSize; private int _Rows; private List&lt;TaxCode&gt; _dataList; public TaxCodePrintPaginator(List&lt;TaxCode&gt; 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 &gt; 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 &lt; 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 &lt; _CurrentRow + _Rows; i++) { dc.DrawText(MakeText(i.ToString()), curPoint); curPoint.X += ColumnWidth; for (int j = 1; j &lt; 4; j++) { dc.DrawText(MakeText(numberGen.Next().ToString()), curPoint); curPoint.X += ColumnWidth; } curPoint.Y += LineHeight; curPoint.X = 0; } } } </code></pre> <p><strong>Before</strong> <img src="https://i.stack.imgur.com/tneH7.png" alt="Before commenting out lines of code"></p> <p><strong>After</strong> <img src="https://i.stack.imgur.com/Bco8G.png" alt="After commenting out lines of code"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload