在页面上定义我正在打印的flowdocument将“开始”和“结束” [英] Defining where on the page the flowdocument I am printing will 'start' and 'end'
问题描述
我的问题是,我正在打印一些报告,包含一个标题(包含关于报告人的信息),一个页脚(带页码)和中间内容(这是一个FlowDocument)。由于flow文档可能相当长,所以它们很可能跨越多个页面。
我的方法是制作一个自定义的从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源
{
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 = 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);
返回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
}
}
$ c $我自己找到了 - 有一个叫做pagepadding的函数,我可以在这里设置距离的距离)
相当简单的解决方案 - 我只是不知道要找什么
例子:
Flowdocument fd = new FlowDocument();
fd.PagePadding =新厚度(0.25,160,0.25,45);
I am almost done with implementing a printing functionality, but I am having trouble getting the last hurdle done with.
My problem is, that I am printing some reports, consisting of a header (with information about the person the report is about), a footer (with a page number) and the content in the middle, which is a FlowDocument. Since the flowdocuments can be fairly long, It is very possible that they will span multiple pages.
My approach is to make a custom FlowDocumentPaginator which derives from DocumentPaginator.
In there i define my header and my footer.
However, when I print my page, the flowdocument and my header and footer are on top of eachother.
So my question is plain and simple - how do I define from where and to where the flowdocument part on the pages will be placed?
here is the code from my custommade 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;
}
}
And here is the printdialogue call:
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
}
}
解决方案 I found it myself - there is a function called pagepadding, where I can set the distance from the four sides of the paper :)
Fairly easy solution - I just didn't know what to look for
Example:
Flowdocument fd = new FlowDocument();
fd.PagePadding = new Thickness(0.25,160,0.25,45);
这篇关于在页面上定义我正在打印的flowdocument将“开始”和“结束”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!