Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Print Preview when using a DocumentPaginator to print?
    primarykey
    data
    text
    <p>I'm using a class I've derived from DocumentPaginator (see below) to print simple (text only) reports from a WPF application. I've got it so that everything prints correctly, <strong>But how do I get it to do a print preview before printing?</strong> I have a feeling I need to use a DocumentViewer but I can't figure out how.</p> <p>Here's my Paginator Class:</p> <pre><code>public class RowPaginator : DocumentPaginator { private int rows; private Size pageSize; private int rowsPerPage; public RowPaginator(int rows) { this.rows = rows; } public override DocumentPage GetPage(int pageNumber) { int currentRow = rowsPerPage * pageNumber; int rowsToPrint = Math.Min(rowsPerPage, rows - (rowsPerPage * pageNumber - 1)); var page = new PageElementRenderer(pageNumber + 1, PageCount, currentRow, rowsToPrint) { Width = PageSize.Width, Height = PageSize.Height }; page.Measure(PageSize); 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(this.rows / (double)this.rowsPerPage); } } public override Size PageSize { get { return this.pageSize; } set { this.pageSize = value; this.rowsPerPage = PageElementRenderer.RowsPerPage(this.pageSize.Height); if (rowsPerPage &lt;= 0) throw new InvalidOperationException("Page can't fit any rows!"); } } public override IDocumentPaginatorSource Source { get { return null; } } } </code></pre> <p>The PageElementRenderer is just a simple UserControl that displays the data (at the moment just a list of rows).</p> <p>Here's how I use my Row Paginator</p> <pre><code>PrintDialog dialog = new PrintDialog(); if (dialog.ShowDialog() == true) { var paginator = new RowPaginator(rowsToPrint) { PageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight) }; dialog.PrintDocument(paginator, "Rows Document"); } </code></pre> <p>Sorry for the code dump, but I didn't want to miss something relevant.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

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