如何使用DocumentPaginator打印时打印预览?

Ray*_*Ray 14 .net c# printing wpf

我正在使用我从DocumentPaginator派生的类(见下文)来打印来自WPF应用程序的简单(仅文本)报告.我已经准备好了所有内容都能正确打印,但如何在打印之前进行打印预览?我有一种感觉,我需要使用DocumentViewer,但我无法弄清楚如何.

这是我的Paginator类:

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; } }
}
Run Code Online (Sandbox Code Playgroud)

PageElementRenderer只是一个显示数据的简单UserControl(目前只是一个行列表).

这是我使用Row Paginator的方式

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");
}
Run Code Online (Sandbox Code Playgroud)

对不起代码转储,但我不想错过相关的东西.

Ray*_*Ray 18

所以我在C#2008上阅读Pro WPF之后就开始工作了(页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();
}
Run Code Online (Sandbox Code Playgroud)

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

这是PrintPreview窗口.

<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>
Run Code Online (Sandbox Code Playgroud)

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

public IDocumentPaginatorSource Document
{
    get { return viewer.Document; }
    set { viewer.Document = value; }
}
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,您可以使用System.IO.Path.GetRandomFileName()(http://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename.aspx)代替Path.GetTempFileName()获取文件名而不创建文件. (4认同)

Bob*_*sum 5

以下代码使用MemoryStream进行打印预览.

MemoryStream stream = new MemoryStream();

Package package = Package.Open(stream, FileMode.Create, FileAccess.ReadWrite);

var uri = new Uri(@"memorystream://myXps.xps");
PackageStore.AddPackage(uri, package);
var xpsDoc = new XpsDocument(package);

xpsDoc.Uri = uri;
XpsDocument.CreateXpsDocumentWriter(xpsDoc).Write(paginator);

documentViewer.Document = xpsDoc.GetFixedDocumentSequence();
Run Code Online (Sandbox Code Playgroud)

记得在关闭打印预览时使用close()并从PackageStore中删除包.