我几乎完成了实现打印功能,但我无法完成最后一道障碍。
我的问题是,我正在打印一些报告,包括标题(包含报告所涉人员的信息),页脚(带页码)和中间的内容,即FlowDocument。由于流文档可能相当长,因此它们很可能会跨越多个页面。
我的方法是创建一个派生自DocumentPaginator的自定义FlowDocumentPaginator。
在那里我定义了我的页眉和页脚。
但是,当我打印我的页面时,flowdocument和我的页眉和页脚都在彼此的顶部。
所以我的问题简单明了 - 如何定义页面上的flowdocument部分的放置位置和位置?
这是我的自定义Paginator的代码:
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;
}
}
这是printdialogue电话:
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
}
}
答案 0 :(得分:6)
我自己发现了 - 有一个叫做pagepadding的功能,我可以在这里设置纸张四边的距离:)
相当简单的解决方案 - 我只是不知道该找什么
示例:
Flowdocument fd = new FlowDocument();
fd.PagePadding = new Thickness(0.25,160,0.25,45);