[WPF 学习] 7.1 多页打印

生来就可爱ヽ(ⅴ<●) 提交于 2020-03-16 12:28:06

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");
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!