gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:04:00 25 4
gpt4 key购买 nike

我正在使用从 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com