How to show an image inside a DocumentPaginator in WPF

31 Views Asked by At

I want to create a report in WPF to show a list of information and I want to put a logo image on top of the page. The page can be repetead if the list is over a page height. So i Try to create a header, a footer and a body. Into the header I want to insert the title and the logo image. My problem is that I can't show the image in the preview page document, I see only the text of the page. The logo is a bmp file in the same directory of the program. Here is my render function code

                public override DocumentPage GetPage(int pageNumber)
        {

            //Create a test string for the purposes of measurement.
            FormattedText text = GetFormattedText("A");

            double col1_X = margin;
            double col2_X = col1_X + text.Width * 15;

            //Retreive the columns name
            string colName1 = dt.Columns[0].ColumnName;
            string colName2 = dt.Columns[1].ColumnName;

            //Calculate the range of rows that fits on this page
            int minRow = pageNumber * rowsPerPage;
            int maxRow = minRow + rowsPerPage;

            //Create the visual for the page
            DrawingVisual visual = new DrawingVisual();

            //Set the position to the top-left corner of the principal area.
            Point point = new Point(margin, margin);

            using (DrawingContext dc = visual.RenderOpen())
            {
                string Percorso = Environment.CurrentDirectory + "\\logo.bmp";
                BitmapImage bitmap = new BitmapImage(new Uri(Percorso, UriKind.Relative));
                bitmap.CacheOption = BitmapCacheOption.OnLoad;
                bitmap.Freeze();

                //Draw the column header
                Typeface columnHeaderTypeface = new Typeface(typeface.FontFamily, FontStyles.Normal, FontWeights.Bold, FontStretches.Normal);
                point.X = margin;
                int contaColonne = 0;
                if (dt.Columns.Count > widths.Length)
                {
                    throw new Exception("Numero di colonne diverso dal numero delle larghezze");
                }
                foreach (DataColumn datacolumn in dt.Columns)
                {
                    text = GetFormattedText(datacolumn.ColumnName, columnHeaderTypeface);
                    dc.DrawText(text, point);
                    point.X += widths[contaColonne];
                    contaColonne++;
                }

                //Draw header
                dc.DrawImage(bitmap, new Rect(100, 40, 60, 40));
                Point HeaderPoint = new Point(200, 40);
                text = GetFormattedText("Resoconto pesate", 40, columnHeaderTypeface);
                dc.DrawText(text, HeaderPoint);
                HeaderPoint.X += 450;
                HeaderPoint.Y -= 30;
                text = GetFormattedText(DateTime.Today.ToShortDateString(), columnHeaderTypeface);
                dc.DrawText(text, HeaderPoint);

                //Draw footer
                text = GetFormattedText((pageNumber + 1).ToString(), columnHeaderTypeface);
                dc.DrawText(text, new Point((pageSize.Width - text.Width) / 2, PageSize.Height - (margin / 2) + text.Height / 2));
                dc.DrawLine(new Pen(Brushes.Black, 2), new Point(margin, pageSize.Height - (margin / 2)), new Point(pageSize.Width - margin, pageSize.Height - (margin / 2)));

                //Draw the line underneath.
                dc.DrawLine(new Pen(Brushes.Black, 2), new Point(margin, margin + text.Height), new Point(pageSize.Width - margin, margin + text.Height));

                point.Y += text.Height;

                //Draw the column values.
                for (int i = minRow; i < maxRow; i++)
                {
                    //Check for the end of the last (half-filled) page.
                    if (i > (dt.Rows.Count - 1)) break;

                    point.X = margin;
                    contaColonne = 0;
                    foreach (DataColumn datacolumn in dt.Columns)
                    {
                        text = GetFormattedText(dt.Rows[i][datacolumn.ColumnName].ToString());
                        dc.DrawText(text, point);
                        point.X += widths[contaColonne];
                        contaColonne++;
                    }
                    point.Y += text.Height;

                }

                dc.Close();
            }

            return new DocumentPage(visual, pageSize, new Rect(pageSize), new Rect(pageSize));
        }

I also tried to move the load image code outside the function and I pass a bitmapImage to the paginator class, but both fails. What is the error I make? Thanks to who can help me.

0

There are 0 best solutions below