c# - 使用 DocumentPaginator 打印时如何打印预览?

标签 c# .net wpf printing

我正在使用从 DocumentPaginator(见下文)派生的类来打印来自 WPF 应用程序的简单(纯文本)报告。我已经知道了,所以一切都可以正确打印,但是如何让它在打印前进行打印预览?我觉得我需要使用 DocumentViewer,但我不知道如何使用.

这是我的分页器类:

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 <= 0)
                throw new InvalidOperationException("Page can't fit any rows!");
        }
    }

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

PageElementRenderer 只是一个显示数据的简单用户控件(目前只是一个行列表)。

这是我如何使用我的行分页器

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");
}

抱歉代码转储,但我不想错过一些相关的东西。

最佳答案

所以我在阅读 Pro WPF in C# 2008 后开始工作了(第 726 页)。

基本上,DocumentViewer 类需要一个 XPS 文件来呈现它的打印预览。所以我做了以下事情:

PrintDialog dialog = new PrintDialog();
var paginator = new RowPaginator(rowsToPrint) { PageSize = new Size(dialog.PrintableAreaWidth, dialog.PrintableAreaHeight) };

string tempFileName = System.IO.Path.GetTempFileName();

//GetTempFileName creates a file, the XpsDocument throws an exception if the file already
//exists, so delete it. Possible race condition if someone else calls GetTempFileName
File.Delete(tempFileName); 
using (XpsDocument xpsDocument = new XpsDocument(tempFileName, FileAccess.ReadWrite))
{
    XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
    writer.Write(paginator);

    PrintPreview previewWindow = new PrintPreview
                                     {
                                         Owner = this,
                                         Document = xpsDocument.GetFixedDocumentSequence()
                                     };
    previewWindow.ShowDialog();
}

我正在创建打印对话框以获得默认页面大小。可能有更好的方法来做到这一点。 XpsDocument 在 ReachFramework.dll (Namespace System.Windows.Xps.Packaging) 中;

这是打印预览窗口。

<Window x:Class="WPFPrintTest.PrintPreview"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="previewWindow"
    Title="PrintPreview" Height="800" Width="800">
    <Grid>
        <DocumentViewer Name="viewer" 
                        Document="{Binding ElementName=previewWindow, Path=Document}" />
    </Grid>
</Window>

后面的代码只有一个 Document 属性,如下所示:

public IDocumentPaginatorSource Document
{
    get { return viewer.Document; }
    set { viewer.Document = value; }
}

关于c# - 使用 DocumentPaginator 打印时如何打印预览?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/584551/

相关文章:

WPF ScrollViewer : tap=click, 点击并按住/拖动=滚动。如何做到这一点?

c# - Dotfuscator 是如何工作的?

c# - 为什么将数组拆箱为不正确的类型不会在 C# 中引发异常?

.net - RangeValidator 无法计数?

c# - 关于 32 位 .exe 与 64 位 .exe

c# - 线程池是否在应用程序域之间共享?

c# - App.xaml 的 EventSetter 上出现错误 CS1061

c# - 如何根据wpf toolbartray的可用空间调整宽度

c# - 如何在 WebMethod 中获取调用方的 IP 地址?

c# - 更改 iTextSharp 的默认桌面保存位置