gpt4 book ai didi

wpf - 定义我要打印的流程文档在页面上的位置为 'start' 和 'end'

转载 作者:行者123 更新时间:2023-12-02 18:46:04 25 4
gpt4 key购买 nike

我几乎完成了打印功能的实现,但我在完成最后一个障碍时遇到了困难。

我的问题是,我正在打印一些报告,其中包括标题(包含有关报告所涉及人员的信息)、页脚(包含页码)和中间的内容(即 FlowDocument)。由于流程文档可能相当长,因此它们很可能会跨越多个页面。

我的方法是制作一个派生自 DocumentPaginator 的自定义 FlowDocumentPaginator。

在那里我定义了我的页眉和页脚。

但是,当我打印页面时,流程文档以及页眉和页脚彼此重叠。

所以我的问题很简单 - 我如何定义页面上的流程文档部分的放置位置和放置位置?

这是我定制的分页器的代码:

public class HeaderedFlowDocumentPaginator : DocumentPaginator
{
private DocumentPaginator flowDocumentpaginator;

public HeaderedFlowDocumentPaginator(FlowDocument document)
{
flowDocumentpaginator = ((IDocumentPaginatorSource) document).DocumentPaginator;
}

public override bool IsPageCountValid
{
get { return flowDocumentpaginator.IsPageCountValid; }
}

public override int PageCount
{
get { return flowDocumentpaginator.PageCount; }
}

public override Size PageSize
{
get { return flowDocumentpaginator.PageSize; }
set { flowDocumentpaginator.PageSize = value; }
}

public override IDocumentPaginatorSource Source
{
get { return flowDocumentpaginator.Source; }
}

public override DocumentPage GetPage(int pageNumber)
{
DocumentPage page = flowDocumentpaginator.GetPage(pageNumber);

ContainerVisual newVisual = new ContainerVisual();
newVisual.Children.Add(page.Visual);

DrawingVisual header = new DrawingVisual();
using (DrawingContext dc = header.RenderOpen())
{
//Header data
}
newVisual.Children.Add(header);

DrawingVisual footer = new DrawingVisual();
using (DrawingContext dc = footer.RenderOpen())
{
Typeface typeface = new Typeface("Trebuchet MS");
FormattedText text = new FormattedText("Page " + (pageNumber + 1).ToString(), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typeface, 14, Brushes.Black);

dc.DrawText(text, new Point(page.Size.Width - 100, page.Size.Height-30));
}

newVisual.Children.Add(footer);

DocumentPage newPage = new DocumentPage(newVisual);
return newPage;
}
}

这是 printdialog 调用:

private void btnPrint_Click(object sender, RoutedEventArgs e)
{
try
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
FlowDocument fd = new FlowDocument();
MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(<My string of text - RTF formatted>));

TextRange tr = new TextRange(fd.ContentStart, fd.ContentEnd);
tr.Load(stream, DataFormats.Rtf);

stream.Close();
fd.ColumnWidth = printDialog.PrintableAreaWidth;

HeaderedFlowDocumentPaginator paginator = new HeaderedFlowDocumentPaginator(fd);

printDialog.PrintDocument(paginator, "myReport");
}
}
catch (Exception ex)
{
//Handle
}
}

最佳答案

我自己发现的 - 有一个叫做 pagepadding 的函数,我可以在其中设置距纸张四个边的距离:)

相当简单的解决方案 - 我只是不知道要寻找什么

示例:

Flowdocument fd = new FlowDocument();

fd.PagePadding = new Thickness(0.25,160,0.25,45);

关于wpf - 定义我要打印的流程文档在页面上的位置为 'start' 和 'end',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3033864/

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