欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[WPF 学习] 7.1 多页打印

程序员文章站 2022-11-22 09:27:24
1.自定义打印类 2.调用 ......

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");