Exemplo n.º 1
0
        PrintDocument GetPrintDocument()
        {
            var document = new PrintDocument();

            document.PrintSettings = settings;
            var font      = Fonts.Serif(16);
            var printTime = DateTime.Now;

            document.PrintPage += (sender, e) => {
                Size pageSize = Size.Round(e.PageSize);

                // draw a border around the printable area
                var rect = new Rectangle(pageSize);
                rect.Inflate(-1, -1);
                e.Graphics.DrawRectangle(Pens.Silver(), rect);

                // draw title
                e.Graphics.DrawText(font, Colors.Black, new Point(50, 20), document.Name);

                // draw page number
                var text     = string.Format("page {0} of {1}", e.CurrentPage + 1, document.PageCount);
                var textSize = Size.Round(e.Graphics.MeasureString(font, text));
                e.Graphics.DrawText(font, Colors.Black, new Point(pageSize.Width - textSize.Width - 50, 20), text);

                // draw date
                text     = string.Format("Printed on {0:f}", printTime);
                textSize = Size.Round(e.Graphics.MeasureString(font, text));
                e.Graphics.DrawText(font, Colors.Black, new Point(pageSize.Width - textSize.Width - 50, pageSize.Height - textSize.Height - 20), text);

                // draw some rectangles
                switch (e.CurrentPage)
                {
                case 0:
                    e.Graphics.DrawRectangle(Pens.Blue(), new Rectangle(50, 50, 100, 100));
                    e.Graphics.DrawRectangle(Pens.Green(), new Rectangle(new Point(pageSize) - new Size(150, 150), new Size(100, 100)));
                    break;

                case 1:
                    e.Graphics.DrawRectangle(Pens.Blue(), new Rectangle(pageSize.Width - 150, 50, 100, 100));
                    e.Graphics.DrawRectangle(Pens.Green(), new Rectangle(50, pageSize.Height - 150, 100, 100));
                    break;
                }
            };
            document.Name      = "Name Of Document";
            document.PageCount = 2;
            return(document);
        }