1.自定义打印类
public class MyDocumentPaginator : DocumentPaginator
{
private int _PageCount;
public override bool IsPageCountValid => true;
public override int PageCount => _PageCount;
public override Size PageSize { get; set; }
public override IDocumentPaginatorSource Source => null;
public override DocumentPage GetPage(int pageNumber)
{
DrawingVisual visual = new DrawingVisual();
using (DrawingContext dc = visual.RenderOpen())
{
dc.DrawText(new FormattedText($"第{pageNumber}页", System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface("仿宋体"), 14, Brushes.Black, 1), new Point(96, 96));
}
return new DocumentPage(visual,PageSize, new Rect(PageSize),new Rect(PageSize));
}
public MyDocumentPaginator(Size pageSize, int pageCount)
{
this.PageSize = pageSize;
_PageCount = pageCount;
}
}
2.调用
PrintDialog pd = new PrintDialog();
MyDocumentPaginator doc = new MyDocumentPaginator(new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight), 3);
pd.PrintDocument(doc, "MyDoc");