trycatch-21
trycatch-21

Reputation: 1

Why it throw an error when integrating a table in the FlowDocument?

In my method, I cannot integrate the table in my 1st page, even if I've a lot of space available.

private void Print_Click(object sender, RoutedEventArgs e)
{
    PrintDialog printDialog = new PrintDialog();

    if (printDialog.ShowDialog() == true)
    {
        try
        {
            FlowDocument flowDocument = new FlowDocument();
            flowDocument.PageWidth = 8.27 * 96;
            flowDocument.PageHeight = 11.69 * 96;

            Grid grid = new Grid();
            grid.Width = flowDocument.PageWidth;
            grid.Height = flowDocument.PageHeight;

            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(100, GridUnitType.Pixel) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(8, GridUnitType.Pixel) });
            grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(78, GridUnitType.Pixel) });

            // Replace the first section with a StackPanel containing 3 TextBlocks
            StackPanel stackPanel = new StackPanel();
            stackPanel.HorizontalAlignment = HorizontalAlignment.Center;
            stackPanel.VerticalAlignment = VerticalAlignment.Center;
            Grid.SetRow(stackPanel, 0);
            grid.Children.Add(stackPanel);

            // TextBlock 1: Company Name
            TextBlock textBlockCompany = new TextBlock(new Run("ABC Company"));
            textBlockCompany.FontSize = 22;
            //textBlockCompany.FontFamily = FontFamily.Source.;
            textBlockCompany.HorizontalAlignment = HorizontalAlignment.Center;
            stackPanel.Children.Add(textBlockCompany);

            // TextBlock 2: Telephone Number
            TextBlock textBlockTel = new TextBlock(new Run("Tel: 555-8888 - Email: [email protected]"));
            textBlockTel.HorizontalAlignment = HorizontalAlignment.Center;
            stackPanel.Children.Add(textBlockTel);

            // Add horizontal line under the first section
            Line horizontalLine = new Line();
            horizontalLine.Stroke = Brushes.Black;
            horizontalLine.X1 = 20;
            horizontalLine.X2 = grid.Width - 60;
            Grid.SetRow(horizontalLine, 1);
            grid.Children.Add(horizontalLine);

            //  ****   Section - 2 -   **** //  

            //Grid section_2 = new Grid();
            StackPanel section_2 = new StackPanel();

            section_2.Width = flowDocument.PageWidth;
            section_2.Margin = new Thickness(20, 10, 20 ,10);

            //section_2.RowDefinitions.Add(new RowDefinition { Height = new GridLength(58, GridUnitType.Pixel) });
            //section_2.RowDefinitions.Add(new RowDefinition { Height = new GridLength(10, GridUnitType.Star) });

            Grid section_title = new Grid();
            section_title.Width = section_2.Width;
            section_title.HorizontalAlignment = HorizontalAlignment.Center;
            section_title.VerticalAlignment = VerticalAlignment.Center;

            section_title.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            section_title.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(84, GridUnitType.Pixel) });

            Grid.SetRow(section_title, 0);
            section_2.Children.Add(section_title);

            TextBlock title_category = new TextBlock(new Run("Liste de Toutes les Catégories."));
            title_category.VerticalAlignment = VerticalAlignment.Center;
            title_category.HorizontalAlignment = HorizontalAlignment.Left;
            title_category.TextAlignment = TextAlignment.Left;

            Grid.SetColumn(section_title, 0);
            section_title.Children.Add(title_category);

            StackPanel stackPanel_date = new StackPanel();
            stackPanel_date.Orientation = Orientation.Horizontal;
            stackPanel_date.HorizontalAlignment = HorizontalAlignment.Right;

            TextBlock date_category = new TextBlock(new Run($"Date : {DateTime.Now.ToShortDateString()}"));
            date_category.VerticalAlignment = VerticalAlignment.Center;
            date_category.HorizontalAlignment = HorizontalAlignment.Right;
            date_category.TextAlignment = TextAlignment.Right;
            stackPanel_date.Children.Add(date_category);

            Grid.SetColumn(section_title, 1);
            section_title.Children.Add(stackPanel_date);

            // Add a table to section_2
            Table table = new Table();
            table.CellSpacing = 0;
            table.FontFamily = new FontFamily("Arial");
            table.FontSize = 12;

            // Add a table row for the header
            TableRow headerRow = new TableRow();
            headerRow.FontWeight = FontWeights.Bold;

            // Add table cells for the header
            TableCell headerCell1 = new TableCell(new Paragraph(new Run("Column 1")));
            headerCell1.FontWeight = FontWeights.Bold;
            headerCell1.BorderBrush = Brushes.Black;
            headerCell1.BorderThickness = new Thickness(1);
            headerRow.Cells.Add(headerCell1);

            TableCell headerCell2 = new TableCell(new Paragraph(new Run("Column 2")));
            headerCell2.FontWeight = FontWeights.Bold;
            headerCell2.BorderBrush = Brushes.Black;
            headerCell2.BorderThickness = new Thickness(1);
            headerRow.Cells.Add(headerCell2);

            TableCell headerCell3 = new TableCell(new Paragraph(new Run("Column 3")));
            headerCell3.FontWeight = FontWeights.Bold;
            headerCell3.BorderBrush = Brushes.Black;
            headerCell3.BorderThickness = new Thickness(1);
            headerRow.Cells.Add(headerCell3);

            table.RowGroups.Add(new TableRowGroup());
            table.RowGroups[0].Rows.Add(headerRow);

            // Add table rows for the data
            for (int i = 0; i < 3; i++)
            {
                TableRow dataRow = new TableRow();

                TableCell dataCell1 = new TableCell(new Paragraph(new Run($"Data Row {i + 1} - Column 1")));
                dataCell1.BorderBrush = Brushes.Black;
                dataCell1.BorderThickness = new Thickness(1);
                dataRow.Cells.Add(dataCell1);

                TableCell dataCell2 = new TableCell(new Paragraph(new Run($"Data Row {i + 1} - Column 2")));
                dataCell2.BorderBrush = Brushes.Black;
                dataCell2.BorderThickness = new Thickness(1);
                dataRow.Cells.Add(dataCell2);

                TableCell dataCell3 = new TableCell(new Paragraph(new Run($"Data Row {i + 1} - Column 3")));
                dataCell3.BorderBrush = Brushes.Black;
                dataCell3.BorderThickness = new Thickness(1);
                dataRow.Cells.Add(dataCell3);

                table.RowGroups[0].Rows.Add(dataRow);
            }

            //Grid.SetRow(table, 1);
            //section_2.Children.Add(table);

            //section_2.Children.Add(new BlockUIContainer(table));

            Grid.SetRow(section_2, 2);
            grid.Children.Add(section_2);

            flowDocument.Blocks.Add(new BlockUIContainer(grid));
            flowDocument.Blocks.Add(table);

            flowDocument.PageWidth = printDialog.PrintableAreaWidth;
            flowDocument.PageHeight = printDialog.PrintableAreaHeight;
            flowDocument.ColumnWidth = printDialog.PrintableAreaWidth;
            IDocumentPaginatorSource idocumentPaginatorSource = flowDocument;
            printDialog.PrintDocument(idocumentPaginatorSource.DocumentPaginator, "Liste de tous les Marques");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

I've tried a lot of solutions suggested from the AIs, but with no result, like

section_2.Children.Add(new BlockUIContainer(table));

Basically, I have a grid add to flowDocument, the grid has 3 rows, the 1st has a stackPanel, the 2nd has a line, and the 3rd has a grid. Finally, the table is added to flowDocument.

The best solution for me is to add the table to the grid after adding a 4th row. However, doing something like that it is not permitted. It results as an error :

Conversion impossible from 'System.Windows.Documents.Table' to 'System.Windows.UIElement'

Upvotes: 0

Views: 64

Answers (0)

Related Questions