XAML WPF -打印数据网格内容

t3psigkw  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(109)

我环顾四周,我还没有找到一个坚实的答案,这个问题。我试图打印我的数据网格内容时,我按下一个按钮,主要问题是,我的数据网格有太多的数据,只有任何显示在屏幕上打印。我需要它来打印所有数据,如果数据不适合在当前页创建一个新的页面,并打印其余的。

qrjkbowd

qrjkbowd1#

下面是我使用System.Drawing.Printing打印数据网格视图的解决方案

using System.Drawing.Printing;

    private int PageCounter { get; set; } = 1;

    private int RowCounter { get; set; }

打印按钮

private void BtnPrint_Click(object sender, EventArgs e)
        {
            PrintDocument printDoc = new PrintDocument();

            IQueryable<PaperSize> paperSizes = printDoc.PrinterSettings.PaperSizes.Cast<PaperSize>().AsQueryable();
            printDoc.DefaultPageSettings.PaperSize = paperSizes.First(ps => ps.Kind == PaperKind.A4);
            printDoc.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
            pageCounter = 1;
            printDoc.PrintPage += PrintDoc_PrintPage;
            printDoc.Print();
        }

打印方式

private void Print_Document(object sender, PrintPageEventArgs e)
        {
            if (e.Graphics == null)
                throw new Exception("Unable to find page Graphics!");

            int left = 30;
            int cellLeft = left;
            int top = 50;
            int cellWidth = 0;
            int headerHeight = 30;
            string headerName = string.Empty;
            string cellValue = string.Empty;

            Rectangle rect = new();

            int pageWidth = e.PageSettings.PaperSize.Width - 60;
            int pageHeight = e.PageSettings.PaperSize.Height - 100;

            Graphics g = e.Graphics;

            Font font = new(FontFamily, 9);
            Pen p = new(Brushes.Black, 1f);
            Pen borderP = new(new SolidBrush(Color.FromArgb(240, 240, 240)), 1f);

            StringFormat stringFormatCenter = new()
            {
                Alignment = StringAlignment.Center,
                LineAlignment = StringAlignment.Center
            };

            StringFormat stringFormatRight = new()
            {
                Alignment = StringAlignment.Far,
                LineAlignment = StringAlignment.Center
            };

            StringFormat stringFormatLeft = new()
            {
                LineAlignment = StringAlignment.Center
            };

            if (PageCounter == 1)
            {

                g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
                top += 30;
                g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
                top += 30;
                g.DrawRectangle(p, new Rectangle(left, top, pageWidth, 30));
                top += 30;
                g.DrawRectangle(p, new Rectangle(left, top, pageWidth / 2, 30));
                g.DrawRectangle(p, new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 30));
                top += 30;

                top = 50;

                g.DrawString
                    ("Company Name"
                    , new Font(FontFamily, 14f, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 30)
                    , stringFormatCenter);
                top += 30;

                g.DrawString
                    ("Business Type"
                    , new Font(FontFamily, 12f, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 30)
                    , stringFormatCenter);
                top += 30;

                g.DrawString
                    ("Report Name"
                    , new Font(FontFamily, 12f, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 30)
                    , stringFormatCenter);
                top += 30;

                g.DrawString
                    ("User Name: " + SelectedUser.Name
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth / 2, 30)
                    , stringFormatLeft);

                g.DrawString
                    ("Report Date: " + DateTime.Now.ToString("dd-mm-yyyy hh:mm:ss")
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 30)
                    , stringFormatRight);
                top += 30;

                g.DrawString
                    ("Login Id: " + SelectedUser.LoginId
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth / 2, 30)
                    , stringFormatLeft);
                g.DrawString
                    ("Printed By: " + LoggedUser.Name
                    , new Font(FontFamily, 9, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left + (pageWidth / 2), top, pageWidth / 2, 20)
                    , stringFormatRight);
                top += 30;

                g.DrawString
                    ("Rights detail as follows:"
                    , new Font(FontFamily, 8, FontStyle.Bold)
                    , Brushes.Black
                    , new Rectangle(left, top, pageWidth, 20)
                    , stringFormatLeft);
                top += 20;
            }

            g.FillRectangle(new SolidBrush(Color.FromArgb(234, 239, 250)), new Rectangle(left, top, pageWidth, headerHeight));
            foreach (string name in PrintableRights.First().GetType().GetProperties().Select(p => p.Name))
            {
                if (name.Equals("SrNo"))
                {
                    headerName = "Sr #";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 8);
                }
                else if (name.Equals("Code"))
                {
                    headerName = "Code";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 20);
                }
                else if (name.Equals("Name"))
                {
                    headerName = "Name";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 57);
                }
                else if (name.Equals("HasRight"))
                {
                    headerName = "Right";
                    cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 15);
                }

                rect = new Rectangle(cellLeft, top, cellWidth, headerHeight);
                g.DrawString(headerName, new Font(FontFamily, 10, FontStyle.Bold), Brushes.Black, rect, stringFormatLeft);
                cellLeft += cellWidth;
            }

            top += headerHeight;
            cellLeft = left;

            while (RowCounter < PrintableRights.Count())
            {
                object row = PrintableRights.ElementAt(RowCounter);
                cellLeft = left;
                foreach (string name in row.GetType().GetProperties().Select(prop => prop.Name))
                {
                    if (name.Equals("SrNo"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 8);
                    else if (name.Equals("Code"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 20);
                    else if (name.Equals("Name"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 57);
                    else if (name.Equals("HasRight"))
                        cellWidth = Convert.ToInt32(Convert.ToDecimal(pageWidth) / 100 * 15);

                    rect = new Rectangle(cellLeft, top, cellWidth, 20);
                    g.DrawRectangle(borderP, rect);

                    PropertyInfo? prop = row.GetType().GetProperty(name);
                    if (prop != null)
                    {
                        if (prop.PropertyType == typeof(bool))
                        {
                            var val = prop.GetValue(row, null);
                            if (val != null && (bool)val)
                                g.DrawString("Yes", font, Brushes.Black, rect, stringFormatLeft);
                            else if (val != null)
                                g.DrawString("No", font, Brushes.Black, rect, stringFormatLeft);
                        }
                        else if (prop.PropertyType == typeof(int))
                        {
                            var val = prop.GetValue(row, null);
                            if (val != null)
                                g.DrawString(((int)val).ToString("N0"), font, Brushes.Black, rect, stringFormatRight);
                            else
                                g.DrawString(string.Empty, font, Brushes.Black, rect, stringFormatRight);
                        }
                        else if (prop.PropertyType == typeof(string))
                        {
                            var val = prop.GetValue(row, null);
                            if(val != null)
                                g.DrawString((string)val, font, Brushes.Black, rect, stringFormatLeft);
                            else
                                g.DrawString(string.Empty, font, Brushes.Black, rect, stringFormatLeft);
                        }
                    }
                    cellLeft += cellWidth;
                }
                top += 20;
                if (RowCounter < PrintableRights.Count() - 1)
                {
                    if (top > pageHeight - 10)
                    {
                        RowCounter++;
                        break;
                    }
                }
                RowCounter++;
            }

            if (RowCounter <= PrintableRights.Count() - 1)
            {
                if (top + 10 > pageHeight)
                {
                    g.DrawString("Continue....", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
                    g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
                    PageCounter++;
                    e.HasMorePages = true;
                }
            }
            else if (e.HasMorePages)
            {
                g.DrawString("Continue....", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
                g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
                PageCounter++;
            }
            else
            {
                g.DrawString("Last Page.", new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 200, e.PageSettings.PaperSize.Height - 60);
                g.DrawString("Page # " + PageCounter.ToString(), new Font(FontFamily, 7f), Brushes.Black, e.PageSettings.PaperSize.Width - 100, e.PageSettings.PaperSize.Height - 60);
            }
        }

相关问题